Order By Clause在SQL中占用太多时间

时间:2012-01-09 13:18:04

标签: database join

SELECT     dteRun,                 
CASE WHEN coalesce(nPriorityCode,0) <= 0 THEN 3
               ELSE nPriorityCode
               END AS nPriorityCode,
               CASE WHEN sCommand IN ('DiaryWF','XC_Reminder') THEN '*'
               ELSE ''
               END as Alert,
               sParentRef,
               nWorkflowTypeCode,
               sSubjectName,
               sDescription,
               sUniqueRef,
               sUserInfo,
               sUserInfo2   
FROM    AuroraTasksDiaryView ad
    INNER JOIN   UserAuthority
         ON UserAuthority.UserName = ad.sOwningUser
         AND ad.sOwningUser  = 'ammonsd'  AND  ad. nErrorCode = -1
         AND ad.sExcludedUser <>   ad.sOwningUser
         AND UserAuthority.FunctionCode = ad.sFunctionCode
        AND ( (UserAuthority.LowerBound <=  ad.nTaskValue
        AND UserAuthority.UpperBound >=  ad.nTaskValue)
        OR ad.sFunctionCode = 'RTS')
        AND  RowNum <= 100
        ORDER BY dteRun

当我省略“Order By dteRun”时,查询以毫秒为单位运行但是使用Order By子句需要花费超过一分钟。 Order By Clause有什么问题?

2 个答案:

答案 0 :(得分:4)

dteRun列上可能没有索引。如果您想对列执行快速ORDER BY,则需要索引。

答案 1 :(得分:1)

它需要Order By子句中的列的索引,最好采用相同的顺序。