SQL:慢查询日期>操作者

时间:2018-01-10 18:05:05

标签: sql sql-server optimization operator-keyword

我已使用此查询查找出生日期大于" x"

的人
SELECT ISNULL(matriculationNro, 0) 'matriculationNro'
FROM    matriculation m
INNER JOIN pupilFinance a ON a.pupilCode = m.pupilCode
INNER JOIN  OtherDB.dbo.pupil p ON RIGHT(REPLICATE('0',7) + p.pupilCode,7) END = m.pupilCode COLLATE Latin1_General_CI_AS   
WHERE   period = '2015-1'
AND     ISNULL(matriculationNro, 0) = 0
AND     ISNULL(a.anotherCode, '') <> 111
AND     ISNULL(a.anotherFlag, '') <> 'S'
AND     a.status = 'A'
AND     p.pupilBirth > (SELECT TOP 1 myBirthvariable FROM OtherDB.dbo.sysvariable)

问题是,&gt;需要6秒或&gt; =运算符

enter image description here

我尝试过使用OUTER APPLY但结果相同:

SELECT ISNULL(matriculationNro, 0) 'matriculationNro'
FROM    matriculation m
INNER JOIN pupilFinance a ON a.pupilCode = m.pupilCode
INNER JOIN  OtherDB.dbo.pupil p ON RIGHT(REPLICATE('0',7) + p.pupilCode,7) END = m.pupilCode COLLATE Latin1_General_CI_AS   
OUTER APPLY (
    SELECT TOP 1 myBirthvariable FROM OtherDB.dbo.sysvariable
) v
WHERE   period = '2015-1'
AND     ISNULL(matriculationNro, 0) = 0
AND     ISNULL(a.anotherCode, '') <> 111
AND     ISNULL(a.anotherFlag, '') <> 'S'
AND     a.status = 'A'
AND     p.pupilBirth > v.myBirthvariable

是否可以对其进行优化?我需要一个非聚集索引?

更新

另一种方式,但需要9秒!!

SELECT ISNULL(matriculationNro, 0) 'matriculationNro'
FROM    matriculation m
INNER JOIN pupilFinance a ON a.pupilCode = m.pupilCode
INNER JOIN (
    select  RIGHT(REPLICATE('0',7) + p.Pu_Compno,7) 'Code', pupilBirth
    from    OtherDB.dbo.pupil p
) x ON x.Code = m.pupilCode COLLATE Latin1_General_CI_AS
WHERE   period = '2015-1'
AND     ISNULL(matriculationNro, 0) = 0
AND     ISNULL(a.anotherCode, '') <> 111
AND     ISNULL(a.anotherFlag, '') <> 'S'
AND     a.status = 'A'
AND     x.pupilBirth > v.myBirthvariable

1 个答案:

答案 0 :(得分:-5)

根据您需要数据的频率,您可以随时创建具有指定资格的视图。执行已经包含您正在查找的数据的select * from个vw,并且只根据我的经验证明该数据更有效。