为什么SQL Server排序运算符会减少行估计?

时间:2014-09-16 05:24:17

标签: sql sql-execution-plan

以下查询的执行计划(由nhibernate生成)显示sort运算符正在减少排序操作之后的行数。什么可能导致这个?通过对左外连接使用散列连接提示来改进查询性能,但是由于错误估计SQL正在使用嵌套循环。我想知道排序运算符是否导致了这一点。

SELECT TOP (26)  
col_0_0_,  
col_1_0_,  
col_2_0_,  
col_3_0_  
FROM (  
    select   
    table0.table_id as col_0_0_,  
    propertyst2_.dataTypeString as col_1_0_,  
    propertyst3_.dataTypeString as col_2_0_,  
    propertyst4_.dataTypeString as col_3_0_ ,  
    ROW_NUMBER() OVER(ORDER BY propertyst2_.dataTypeString) as __hibernate_sort_row  
    from dbo.Tables table0   
    left outer join dbo.PropertyDataString propertyst2_ on table0.table_id=propertyst2_.parent_id and (propertyst2_.propertyType_id='p0') 
    left outer join dbo.PropertyDataString propertyst3_ on table0.table_id=propertyst3_.parent_id and (propertyst3_.propertyType_id='p1') 
    left outer join dbo.PropertyDataString propertyst4_ on table0.table_id=propertyst4_.parent_id and (propertyst4_.propertyType_id='p2') 
    where table0.tableType_id='p3' 
) as query 
WHERE query.__hibernate_sort_row > 221052 ORDER BY query.__hibernate_sort_row

enter image description here

1 个答案:

答案 0 :(得分:0)

我在查询中遇到同样的问题,我确认这是由于TOP N. SQL Server希望在Sort操作中仅使用k * N个第一行,但是下一个过滤器显示这些行被排除,因此它需要更多行,这就是为什么我们看到实际行数远高于预期的行数排序操作后。