sql – 我应该在索引中包含select中的所有列吗?

我有一个查询,需要一些运行它看起来像这样.

Select a.ColumnA,a.ColumnB,a.ColumnC,a.CoulmnD,a.columnE,...b.ColumnH
from Table A a
  inner join Table B b
       on a.columnB = b.ColumnB
  Where a.columnA = @VariableA

现在,它在表A上有一个聚集索引

Clustered Index on ColumnA

它在表A上也有这样的非聚集索引

NonClustered Index on (ColumnA,ColumnB) include (ColumnC,ColumnD)

我应该将ColumnsE-G添加到索引中吗?

最佳答案

Verify the data type of compared columns and parameters are the same when you see an index scan instead of the expected seek in the execution plan.

当数据类型不同时,SQL Server必须首先将具有较低优先级的操作数转换为较高优先级数据类型(例如varchar to nvarchar).如果是必须转换的列值,则转换会阻止对列的索引进行有效使用,因为必须先转换每行的列值,然后才能进行比较.这被称为非sargable表达式,并阻止更有效的索引搜索.

点赞