2

我有以下查询,它需要 2 秒才能执行,因为两个表中有大量行(每行 100 万+),我想知道是否还有什么可以做的进一步优化查询。

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)

执行计划 在此处输入图像描述

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

4

2 回答 2

1

我唯一能想到的是

    SELECT  i.ID FROM 
    (select TOP 1 id from tblInspection 
     WHERE IsPassedFirstTime = 1) i
                    INNER JOIN tblInspectionFailures ON 
i.ID = tblInspectionFailures.InspectionID 
于 2012-11-26T17:09:16.877 回答
0

尝试

SET ROWCOUNT 1

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

这基本上做同样的事情,但告诉 sql 在第一个之后停止返回行

于 2012-11-26T18:07:32.470 回答