SQL server 系统优化--通过执行计划优化索引(3)
2008-09-11 19:36
330 查看
刚刚在客户优化监控系统中,通过DMVs发现一条(通过HQL语句生成)sql语句,执行时间要7205ms,语句如下:
select top 30 this_.id as id10_0_, this_.objid as objid10_0_, this_.objname
as objname10_0_, this_.mid as mid10_0_, this_.logtype as logtype10_0_, this_.logdesc as logdesc10_0_, this_.submitor as submitor10_0_, this_.submitdate as submitdate10_0_,
this_.submittime as submittime10_0_, this_.submitip as submitip10_0_, this_.col1 as col11_10_0_,this_.col2 as col12_10_0_, this_.col3 as col13_10_0_ from log this_ where not exists (select 'X' from Delobj del where del.objid=this_.id and del.objtable='Log') order by this_.submitdate desc, this_.submittime desc
执行sql语句的执行计划:
![](http://images.cnblogs.com/cnblogs_com/zping/145752/o_index31.jpg)
分析:这里发现整个sql语句最占用资源的地方就是排序了,占了90%,而且表log中大约有超过46万行数据。排序的成本很高,我们发现是通过:
[dbo].[log].submitdate 降序, [dbo].[log].submittime 降序
排序是在submitdate和submittime的排序。由于我们在log表中列id建立了聚集索引,这时由于排序是用到了时间列,所以成本很高,这时我们需要重新重构聚集索引。
是必须在(submitdate,submittime)建立聚集索引,由于前期在设计表时的不合理,造成了时间字段教长,有18个字节,大大超过了8个字节的datetime类型,不太适合建立聚集索引。
这时为保证查询性能,在submitdate字段建立聚集索引,而不是建立(submitdate,submittime)复合聚集索引,适当提高速度。
2,对与聚集索引选择一般在需要排序,进行范围选择和group by等字段上建立
select top 30 this_.id as id10_0_, this_.objid as objid10_0_, this_.objname
as objname10_0_, this_.mid as mid10_0_, this_.logtype as logtype10_0_, this_.logdesc as logdesc10_0_, this_.submitor as submitor10_0_, this_.submitdate as submitdate10_0_,
this_.submittime as submittime10_0_, this_.submitip as submitip10_0_, this_.col1 as col11_10_0_,this_.col2 as col12_10_0_, this_.col3 as col13_10_0_ from log this_ where not exists (select 'X' from Delobj del where del.objid=this_.id and del.objtable='Log') order by this_.submitdate desc, this_.submittime desc
执行sql语句的执行计划:
![](http://images.cnblogs.com/cnblogs_com/zping/145752/o_index31.jpg)
分析:这里发现整个sql语句最占用资源的地方就是排序了,占了90%,而且表log中大约有超过46万行数据。排序的成本很高,我们发现是通过:
[dbo].[log].submitdate 降序, [dbo].[log].submittime 降序
排序是在submitdate和submittime的排序。由于我们在log表中列id建立了聚集索引,这时由于排序是用到了时间列,所以成本很高,这时我们需要重新重构聚集索引。
是必须在(submitdate,submittime)建立聚集索引,由于前期在设计表时的不合理,造成了时间字段教长,有18个字节,大大超过了8个字节的datetime类型,不太适合建立聚集索引。
这时为保证查询性能,在submitdate字段建立聚集索引,而不是建立(submitdate,submittime)复合聚集索引,适当提高速度。
总结:
1,字段类型和长短的是很重要,不然在后续修改和优化是很困难。2,对与聚集索引选择一般在需要排序,进行范围选择和group by等字段上建立
相关文章推荐
- SQL server 系统优化--通过执行计划优化索引(1)
- SQL server 系统优化--通过执行计划优化索引(2)
- SQL server 系统优化--通过执行计划优化索引(1) (转)
- 优化 SQL Server 查询性能----分析执行计划,索引与索引视图,如何识别要优化的查询
- 优化 SQL Server 查询性能----分析执行计划,索引与索引视图,如何识别要优化的查询
- SQL Server 强大的分区技术优化执行计划索引实例详解(使用语句检测和优化数据库 (MSSQL个人笔记之数据库优化之路 四)
- sql server 数据库优化--显示执行计划 你真的知道索引使用???
- 通过分析SQL语句的执行计划优化SQL(总结)
- mysql查缺补漏(二)mysql5.6性能优化(explain执行计划术语,索引,优化查询)
- SQL Server现场优化纪实(一):通过重建索引提高性能
- SQL Server 查询优化(测试02)参数嗅探-执行计划选择
- 第六章——根据执行计划优化性能(2)——查找表/索引扫描
- 通过分析SQL语句的执行计划优化SQL (五)
- 通过分析SQL语句的执行计划优化SQL(总结)
- 通过分析SQL语句的执行计划优化SQL(三)
- SQL Server索引的执行计划
- 通过分析SQL语句的执行计划优化SQL
- SQL Server-聚焦使用索引和查询执行计划
- 初探Sql Server 执行计划及Sql查询优化
- 高性能可扩展mysql(执行计划,索引分析优化改写,删除重复数据,区间统计,满查询日志)