TSQL优化

时间:2012-11-26 17:04:35

标签: sql-server tsql sql-server-2005 query-optimization

我有下面的查询需要2秒才能执行,因为两个表中有大量的行(每个100万+),并且想知道我是否还可以做更多的事情来优化查询。 / p>

表格

tblInspection.ID bigint(主键)

tblInspection.IsPassedFirstTime位(非聚集索引)

tblInspectionFailures.ID bigint(主键)

tblInspectionFailures.InspectionID bigint(非聚集索引)

查询

SELECT TOP 1 tblInspection.ID FROM tblInspection 
                INNER JOIN tblInspectionFailures ON tblInspection.ID = tblInspectionFailures.InspectionID 
                WHERE (tblInspection.IsPassedFirstTime = 1)

执行计划 enter image description here

我可以看到我正在对索引进行聚类搜索,但它仍然需要一些时间

2 个答案:

答案 0 :(得分:1)

我唯一能想到的是

    SELECT  i.ID FROM 
    (select TOP 1 id from tblInspection 
     WHERE IsPassedFirstTime = 1) i
                    INNER JOIN tblInspectionFailures ON 
i.ID = tblInspectionFailures.InspectionID 

答案 1 :(得分:0)

SET ROWCOUNT 1

SELECT tblInspection.ID FROM tblInspection 
            INNER JOIN tblInspectionFailures ON tblInspection.ID = tblInspectionFailures.InspectionID 
            WHERE (tblInspection.IsPassedFirstTime = 1)

这基本上是一样的但是告诉sql在第一个之后停止返回行