我有一个与索引和执行计划相关的查询。以下是我正在使用的几张表
表1:TestReviewResult
Column_name | Type | Length
TestReviewResultId | int | 4
TestNumber | int | 4
ReviewerUserId | int | 4
当前指数
index_name index_description index_keys
TestReviewResult_PK位于PRIMARY上的集群唯一主键TestReviewResultId
表2:TestReviewFinding
Column_name类型长度
TestReviewFindingId int 4
TestReviewResultId int 4
ScreenCode varchar 100
ReviewComments varchar 8000
CURRENTINDEX
index_name index_description index_keys
TestReviewFinding_PK位于PRIMARY TestReviewFindingId上的集群唯一主键
表3:TestReviewResultComment
Column_name类型长度
TestReviewResultCommentId Int 4
TestReviewResultId Int 4
TestReviewComment varchar 8000
CURRENTINDEX
index_name index_description index_keys
TestReviewResultComment_CI集群位于PRIMARY TestReviewResultId
TestReviewResultComment_PK位于PRIMARY上的非聚集,唯一主键TestReviewResultCommentId
表4:TestReviewFindingElement
Column_name类型长度
TestReviewFindingElementId Int 4
TestReviewFindingId Int 4
ElementCode varchar 25
CURRENTINDEX
index_name index_description index_keys
TestReviewFindingElementType_PK位于PRIMARY上的集群唯一主键TestReviewFindingElementId
以下是正在使用的查询
Select columnnames…. from
From TestReviewResult(NOLOCK) cr
left outer join TestReviewResultComment(NOLOCK) crc
on cr.TestReviewResultId = crc.TestReviewResultId
left outer join TestReviewFinding(NOLOCK) cf
on cf.TestReviewResultId = cr.TestReviewResultId
left outer join TestReviewFindingElement(NOLOCK) crf
on cf.TestReviewFindingId = crf.TestReviewFindingId
where cr.TestReviewResultId = @TestReviewNumber -- Test Review number is
being passed in the stored procedure
当我运行执行计划时,它建议添加下面提到的2个索引
CREATE NONCLUSTERED INDEX IX_TestReviewResultId
ON [TestReviewFinding] (TestReviewResultId)
CREATE NONCLUSTERED INDEX IX_TestReviewFindingId
ON [TestReviewFindingElement] (TestReviewFindingId)
有没有其他方法可以在不添加索引的情况下调整上述查询,因为有很多写操作可以执行
下面是您可以在SQL Server中执行的ddl,并检查查询执行计划
Create table TestReviewResult(
TestReviewResultId int NOT NULL PRIMARY KEY ,
TestNumber int ,
ReviewerUserId int )
insert into TestReviewResult values(1,1,1)
insert into TestReviewResult values(2,2,2)
insert into TestReviewResult values(3,3,3)
Create table TestReviewFinding(
TestReviewFindingId int not null primary key,
TestReviewResultId int ,
ScreenCode varchar(100))
insert into TestReviewFinding values(1,1,'A')
insert into TestReviewFinding values(2,2,'B')
insert into TestReviewFinding values(3,3,'C')
Create table TestReviewResultComment(
TestReviewResultCommentId int not null primary key nonclustered,
TestReviewResultId int ,
TestReviewComment varchar( 8000)
)
CREATE CLUSTERED INDEX [CI_TestReviewResultId]
ON TestReviewResultComment (TestReviewResultId);
insert into TestReviewResultComment values(1,1,'A')
insert into TestReviewResultComment values(2,2,'B')
insert into TestReviewResultComment values(3,3,'C')
Create table TestReviewFindingElement(
TestReviewFindingElementId int not null primary key,
TestReviewFindingId int ,
ElementCode varchar(25))
insert into TestReviewFindingElement values(1,1,'A')
insert into TestReviewFindingElement values(2,3,'B')
insert into TestReviewFindingElement values(3,3,'C')
当我运行以下查询时,我在2个表上获得索引扫描 TestReviewFindingElement和TestReviewFinding
Select *
From TestReviewResult(NOLOCK) cr
left outer join TestReviewResultComment(NOLOCK) crc
on cr.TestReviewResultId = crc.TestReviewResultId
left outer join TestReviewFinding(NOLOCK) cf
on cf.TestReviewResultId = cr.TestReviewResultId
left outer join TestReviewFindingElement(NOLOCK) crf
on cf.TestReviewFindingId = crf.TestReviewFindingId
where cr.TestReviewResultId = @TestReviewNumber -- Test Review number is
being passed in the stored procedure.You can use --> 1
有没有办法修改select查询以避免扫描而不添加索引