I'm having a problem joining local tables and linked server tables. I can do it just by using inner joins, but it's taking me too long to execute the query. I know there's a way to do it with OPENQUERY, but I can't get it.
Here's what I was doing in the beginning:
SELECT DISTINCT
local.L_ID
FROM dbo.local_table AS local
INNER JOIN [server].[db].[dbo].[TB_TEST] as ts
on local.L_ID = ts.L_ID
LEFT JOIN [server].[db].[dbo].[TB_EXE] as ex
on ts.A_ID = ex.T_ID
Now I'm trying to do this:
SELECT DISTINCT
local.L_ID
FROM dbo.local_table AS local
INNER JOIN (
SELECT *
FROM OPENQUERY(SERVER,'SELECT L_ID FROM TB_TEST'
) ts
on local.L_ID = ts.L_ID
left join OPENQUERY(SERVER,'SELECT T_ID FROM TB_EXE') ex
on ts.A_ID = ex.T_ID
Can you help me doing this the right way so the query runs quicker?