简体   繁体   English

SQL Server使用order by子句可显着提高选择性能

[英]SQL Server using order by clause significantly improves select performance

I am executing the following query directly in SQL Server: 我直接在SQL Server中执行以下查询:

   SELECT *
     FROM TableA
LEFT JOIN TableB 
       ON TableB.field1 = TableA.field1
LEFT JOIN TableC 
       ON TableC.field2 = TableA.field2
LEFT JOIN TableD 
       ON TableD.field3 = TableA.field3
LEFT JOIN TableE 
       ON TableE.field4 = TableA.field4
LEFT JOIN TableF 
       ON TableF.field5 = TableA.field5
LEFT JOIN 
          (SELECT *
             FROM 
                (SELECT 
                       Id1, Id2,
                       UpdateDate,
                       ROW_NUMBER() OVER(PARTITION BY Id1, Id2, 
               ORDER BY UpdateDate DESC) AS RN 
                   FROM TableG) AS G 
           WHERE G.RN = 1) TableH 
     ON TableA.Id1 = TableH.Id2 
    AND TableA.Id1 = TableH.Id2

For point of reference, Table AF and G are about 1000 rows, and Table G is about 10000 rows. 作为参考,表AF和G大约为1000行,表G为大约10000行。

For a particular input, this query takes about 1 minute to run. 对于特定输入,此查询大约需要1分钟才能运行。

I then add a 然后我添加一个

ORDER BY Id1 ASC

at the end of the statement, and now it takes about 6 seconds to run. 在该语句的末尾,现在大约需要6秒钟才能运行。 How can adding a sort significantly improve performance like this? 如何添加排序可以显着改善这样的性能?

Run a showplan on both versions of your query. 在两个版本的查询上运行显示计划。

Probably what's happening is the sort forces a different query plan, which uses a more efficient for your particular data join strategy (probably in-memory), but which has a higher estimated cost. 可能发生的情况是排序强制执行不同的查询计划,该查询计划对您的特定数据连接策略(可能是内存中的)使用了更高效的方法,但是估计成本较高。

After examining the execution plan, it seems that the issue was with the JOIN on Table A and Table G. Initially the optimizer was trying to use a nested loop join which was very inefficient for tables of their size. 在检查了执行计划之后,似乎问题出在表A和表G上的JOIN上。最初,优化器正在尝试使用嵌套循环联接,这对于其大小的表而言效率非常低。 Adding the ORDER BY clause hinted to the optimizer to use a merge join instead, which was much faster. 向优化器添加ORDER BY子句会提示优化器改用合并联接,这要快得多。 Thanks for the answers! 感谢您的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM