【发布时间】:2019-06-10 13:18:24
【问题描述】:
在我的项目中,用于搜索的存储过程如下
create proc CommentGet
(
@Type int,
@ParentID int
)
as
begin
select * from CommentTable where (@Type is null or CommentTable.Type=@Type) and (@ParentID is null or CommentTable.ParentID=@ParentID)
end
在这种情况下,我引入了 Type 和 ParentID 作为 CommentTable 的非集群索引。当我运行代码时,我看到执行计划使用索引扫描而不是索引搜索来获取导致 SQL 需要读取整个页面的结果。显然我不能使用非集群索引的好处。
到目前为止我已经弄清楚,如果在搜索词中使用了变量,就会出现这个问题,如果没有使用where线索的直接量,则不会出现问题并且可以正常工作(索引Seek )。
请告知您如何使用非集群索引的好处来处理它 Here exception plane as example, as you see written index scan instead of index Seek
【问题讨论】:
-
将
OPTION(RECOMPILE);添加到SELECT语句的末尾。 -
这就是所谓的“包罗万象的查询”。我建议阅读 Gail 关于这些的文章:Catch-all Queries 和 Revisiting Catch-all Queries。
-
您在名为
CommentTable的表中有一个名为CommentTable的列?我猜这不是您的实际 SQL;这使得它很难说什么。 -
与其向我们描述计划,您可以paste the plan 代替吗?
-
@DanGuzman 这个选项到底是做什么的?是否会降低 SP 的性能?
标签: .net sql-server database performance indexing