Sql Server 查询走错索引引发的性能故障分析、处理


      开发人员丢过来一段sql,逻辑很简单,说是执行非常缓慢,跑不出结果,让看下是怎么回事。

      看了下sql,就一个where 过滤条件和order by 排序:

select top 1 Ppid from M_InteriorPpid_t  WITH (nolock) where moid='11000302285' order by intime asc

首先看moid、intime 列是否存在索引:

该表有2个索引,intime 单列索引、moid和intime以及其他字段组合索引,查看表行数大约1亿多行,于是分析该查询的执行计划:

由于存在intime 排序,查询使用了intime 索引 全扫描,开销比较大,应该使用moid 字段的多列索引比较合理,添加索引表提示后sql 如下:

select top 1 Ppid from M_InteriorPpid_t  WITH (nolock,index(M_InteriorPpid_t_0808_index)) where moid='11000302285' order by intime asc

执行后速度非常快,性能故障解决,执行计划如下,这次选择是index seek:

相关