【问题标题】:index scan in MS SQL execution plan when using non cluster index in where clause instead of index seek在 where 子句中使用非聚集索引而不是索引查找时,MS SQL 执行计划中的索引扫描
【发布时间】: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 QueriesRevisiting Catch-all Queries
  • 您在名为CommentTable 的表中有一个名为CommentTable 的列?我猜这不是您的实际 SQL;这使得它很难说什么。
  • 与其向我们描述计划,您可以paste the plan 代替吗?
  • @DanGuzman 这个选项到底是做什么的?是否会降低 SP 的性能?

标签: .net sql-server database performance indexing


【解决方案1】:

你有什么索引?每个索引下的列是什么?我会假设您没有正确索引或以最适合您查询的方式建立索引,因为 sql 在运行时会自动使用最佳和最有效的搜索。

【讨论】:

  • 正如我所说,在 CommentTable 中类型和专利 ID 设置为非集群索引
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
相关资源
最近更新 更多