奇怪的SQL标准过滤器问题

时间:2017-11-01 12:43:14

标签: sql sql-server

我正在运行一个运行大量查询的SQL服务器。但我的所有查询都表现良好。但我有这个奇怪的问题。这是查询:

  SELECT  _PATIENT_ORDER_LIST.*
    FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY SRH_SearchResultOrientedOrders.SendDate DESC ) AS RowIndex ,
                        ( SELECT    COUNT(SRH_SearchResultOrientedOrders.PatientID)
                          FROM      SRH_SearchResultOrientedOrders
                          WHERE     SRH_SearchResultOrientedOrders.PatientID <> 0
                                    AND SRH_SearchResultOrientedOrders.IsReceived = 'True'
                                    AND PatientName LIKE '%%'
                                    AND Category = 'Hematology'
                                    AND ( IsSpacementToDesk = 'true' )
                                    AND ( OrderStatus = 1 )
                                    AND ( IsOrderDone = 'true' )
                                    AND ( ResultApprovedBy IS NULL )
                                    AND OrderTo = 'Laboratory'
                        ) AS NumberOfRows ,
                        SRH_SearchResultOrientedOrders.*
              FROM      SRH_SearchResultOrientedOrders
              WHERE     SRH_SearchResultOrientedOrders.PatientID <> 0
                        AND SRH_SearchResultOrientedOrders.IsReceived = 'True'
                        AND PatientName LIKE '%%'
                        AND Category = 'Hematology'
                        AND ( IsSpacementToDesk = 'true' )
                        AND ( OrderStatus = 1 )
                        AND ( IsOrderDone = 'true' )
                        AND ( ResultApprovedBy IS NULL )
                        AND OrderTo = 'Laboratory'
            ) AS _PATIENT_ORDER_LIST
    WHERE   _PATIENT_ORDER_LIST.RowIndex > 0
            AND _PATIENT_ORDER_LIST.RowIndex <= 20

如果我把化学&#39;在类别列过滤中,显示记录需要4秒钟,但如果我将血液学&#39;在化学过程中发现的记录数量为195分,血液学过滤的结果为69分,需要1分30秒。我无法弄清楚为什么查询速度慢。请帮忙

2 个答案:

答案 0 :(得分:1)

&LT;长评&gt;

子查询未链接到外部查询。您是否意识到每行子查询将返回相同的值? COUNT(SRH_SearchResultOrientedOrders.PatientID) - 它是满足过滤条件的所有行的总数。

_PATIENT_ORDER_LIST.RowIndex <= 20 - TOP 20 ... ORDER BY可能会更快。因为无需对所有行进行编号(因为您可以执行row_number,所以不需要ORDER BY SendDate ASC)。

此代码:

IsReceived = 'True'
AND IsSpacementToDesk = 'true'
AND OrderStatus = 1
AND IsOrderDone = 'true'
AND ResultApprovedBy IS NULL

说您可能需要重新设计状态列。所有这些标准可能意味着一个status_id。这可以是计算列或持久列。

将布尔值存储为字符串绝对是一个坏主意。请改用BIT

要了解性能问题,应该调查实际的执行计划和统计计数器。虽然,我怀疑它会显示除了&#34之外的任何东西;一切都非常糟糕&#34;。

具有不同过滤条件的特殊查询将被服务器视为两个完全不同的查询,可能具有不同的执行计划 - 因为字符串文字是查询的一部分,使它们在符号上不同。

答案 1 :(得分:1)

您的NumberOfRows计算与COUNT(*) OVER ()

相同
SELECT  _PATIENT_ORDER_LIST.*
FROM    ( SELECT    Row_Number() Over ( ORDER BY SRH_SearchResultOrientedOrders.SendDate DESC ) AS RowIndex ,
                    Count(*) Over () AS NumberOfRows ,
                    SRH_SearchResultOrientedOrders.*
          FROM      SRH_SearchResultOrientedOrders
          WHERE     SRH_SearchResultOrientedOrders.PatientID <> 0
                    AND PatientName LIKE '%%'
                    AND Category = 'Hematology'
                    AND ( IsSpacementToDesk = 'true' )
                    AND ( OrderStatus = 1 )
                    AND ( IsOrderDone = 't.rue' )
                    AND ( ResultApprovedBy IS NULL )
                    AND OrderTo = 'Laboratory'
        ) AS _PATIENT_ORDER_LIST
WHERE   _PATIENT_ORDER_LIST.RowIndex > 0
        AND _PATIENT_ORDER_LIST.RowIndex <= 20

如果它没有提高性能,至少它提高了可读性