以下查询非常快:
createOrGetUser
以下查询需要很长时间才能执行:
select top 100 table1.Name,table2.Name
from table1 WITH (NOLOCK) left join table2 on table1.ID2= table2.ID
order by table1.ID desc'
这是执行计划:
如何用select top 100 table1.Name,table2.Name
from table1 WITH (NOLOCK) left join table2 on table1.ID2= table2.ID
order by table1.ID desc,table2.ID desc'
来执行SQL?
答案 0 :(得分:0)
左连接意味着表1中的每一行都将确保为结果贡献至少一行,并且您从该表中获取的行永远不会超过100行。
所以我会尝试
WITH T1Top100
AS (SELECT TOP 100 *
FROM table1
ORDER BY ID DESC)
SELECT TOP 100 table1.NAME,
table2.NAME
FROM T1Top100 table1
LEFT JOIN table2
ON table1.ID2 = table2.ID
ORDER BY table1.ID DESC,
table2.ID DESC