原文出处:https://www.cnblogs.com/cq-jiang/p/7711680.html (建议阅读原文)

一:sqlserver 执行计划介绍

     sqlserver 执行计是在sqlser manager studio 工具中打开,是检查一条sql执行效率的工具。建议配合SET STATISTICS IO ON等语句来一起使用,执行计划是从右向左看,耗时高的一般显示在右边,我们知道,sqlserver 查询数据库的方式为:

  1:表扫描(table scan) 查询速度最慢.

  2:聚集索引扫描(Clustered Index Scan),按聚集索引逐行进行查询,效率比表扫描高,但速度还是慢.

  3:索引扫描(index scan)效率比聚集索引快,根据索引滤出部分数据在进行逐行检查。

  4;索引查找(index seek) 效率比索引扫描还要快,根据索引定位记录所在位置再取出记录.

  5:聚集索引查找(Clustered Index Seek) 效率最快,直接根据聚集索引获取记录。

当发现某个查询比较慢时,可以首先检查哪些操作的成本比较高,再看看那些操作在查找记录时, 是不是【Table Scan】或者【Clustered Index Scan】,如果确实和这二种操作类型有关,则要考虑增加索引来解决了,sqlser 索引有两种,聚集索引和非聚集索引,聚集索引是一张表只能有一个,比如id,非聚集索引可以有多个,聚集索引是顺序排列的类似于字典查找拼音a、b、c……和字典文字内容顺序是相同的,非聚集索引与内容是非顺序排列的,类似字典偏旁查找时,同一个偏旁‘王’的汉字可能一个在第1页一个在第5页。

二:创建测试表

create table shopping_user(uId bigint primary key,uName varchar(10));
create table shopping_goods_category(cId bigint primary key,cName varchar(20));
create table shopping_goods(gId bigint primary key,gName varchar(50),gcId bigint,gPrice int);
create table shopping_order(oId bigint primary key,oUserId bigint,oAddTime datetime,oGoodsId bigint,oMoney int);

  创建测试sql

declare @index int;
set @index = 1;
while(@index<=10)
begininsert into shopping_user (uId,uName) values(@index,'user'+cast(@index as varchar(10)));set @index = @index+1;
end;insert into shopping_goods_category (cid,cName) values(1,'水果');
insert into shopping_goods_category (cid,cName) values( 2,'电脑');
insert into shopping_goods_category (cid,cName) values (3,'手机');
insert into shopping_goods_category (cid,cName) values (4,'服装');
insert into shopping_goods_category (cid,cName) values (5,'食品');------ 商品表sqldeclare @index int;
declare @num int;
set @index = 1;
set @num = 10000;
beginwhile(@index<=100*@num)beginif @index<=10*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,1,'水果'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end;else if @index >10*@num and @index <=20*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,1,'水果'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end;else if @index >20*@num and @index <=30*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,2,'电脑'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end;else if @index >30*@num and @index <=40*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,2,'电脑'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end;else if @index >40*@num and @index <=50*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,3,'手机'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end;else if @index >50*@num and @index <=60*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,3,'手机'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end; else if @index >60*@num and @index <=70*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,4,'服装'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end; else if @index >70*@num and @index <=80*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,4,'服装'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end; else if @index >80*@num and @index <=90*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,5,'食品'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end; else if @index >90*@num and @index <=100*@numbegininsert into shopping_goods (gId,gcId,gName,gPrice)values (@index,5,'食品'+cast (@index as varchar(10)),cast( floor(rand()*100) as int) );end; set @index = @index+1;end;
end;------- 订单表sqldeclare @index int;
declare @num int;
declare @timeNum int;
declare @userId int;
declare @goodsId int;
declare @money int;
declare @addTime varchar(30);
set @index = 1;
set @num = 10000;
set @timeNum = 0;
set @userId = 1;
set @goodsid = 1;
set @money = 100;
set @addTime = '';
beginwhile(@index<=100*@num)beginset @timeNum = cast( floor(rand()*30)+1 as int)set @userId = cast( floor(rand()*99)+1 as int)set @money = cast ( floor(rand()*5000)+@userId as int)set @addTime = dateadd(day,@timeNum,getdate())set @goodsId = cast( floor(rand()*999999)+1 as int)if @index<=10*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end;else if @index >10*@num and @index <=20*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end;else if @index >20*@num and @index <=30*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end;else if @index >30*@num and @index <=40*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end;else if @index >40*@num and @index <=50*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end;else if @index >50*@num and @index <=60*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end; else if @index >60*@num and @index <=70*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end; else if @index >70*@num and @index <=80*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end; else if @index >80*@num and @index <=90*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end; else if @index >90*@num and @index <=100*@numbegininsert into shopping_order (oid,oUserId,oAddTime,oGoodsId,oMoney)values (@index,@userId,@addTime,@goodsId,@money );end; set @index = @index+1;end;end;

  创建索引

create index gcid_index on shopping_goods (gcid);
create index userid_index on shopping_order(ouserid);
create index goodsid_index on shopping_order(ogoodsid);

三:执行计划分析

  这里使用上一篇文章sql语句百万数据量优化方案中提到的,in和exists来分析,sql语句如下:

SET STATISTICS IO ONselect top 20 * from shopping_order where exists (
select top 10 gid from shopping_goods where gcid =2 and ogoodsid = gid order by gprice desc)select top 20 * from shopping_order where goodsid in (
select top 10 gid from shopping_goods where gcid =2 order by gprice desc)-- DBCC DROPCLEANBUFFERS 

  

从上图中发现,使用exists,开销最大的是,使用聚集索引查找,而使用in,第一次操作(从右各左看),就使用了聚集索引扫描,in的效果明显差。我们再来看聚集索引查找结果,聚集索引返回的行数是20,见下图.

然后我们来看使用in查询,聚集索引扫描,查询结果却是20w

接着我们来看使用in查询,第二个开销大的排序,从刚才查询出来的20w数据中,order by desc 返回前20条数据。

此处我们还可以使用SET STATISTICS IO ON来查询这两者的io开销:

    扫描计数:执行的扫描次数;

    逻辑读取:从数据缓存读取的页数;

    物理读取:从磁盘读取的页数;

    预读:为进行查询而放入缓存的页数

重要:如果对于一个SQL查询有多种写法,那么这四个值中的逻辑读(logical reads)决定了哪个是最优化的。

从上图中发现,exists查询:shopping_order表扫描次数是2,逻辑读取是80,shopping_goods表,扫描次数是1,逻辑读取是6次,

          而in  shopping_order表扫描次数是2,逻辑读取是55,shopping_goods表,扫描次数是5,逻辑读取是5247次,当然工作中的sql肯定要复杂得多,但我们可以借助这个工具来找到需要优化的sql,当然这也只是执行计划,可能实际执行的效率和这个计划有出入,但我们还是可以借鉴执行计划来找到其中的不足。

sqlser 2005 使用执行计划来优化你的sql (实用、赞)相关推荐

  1. mysql有没有mssql执行计划_MSSQL优化执行计划

    介绍了关于利用MSSQL执行计划来优化mssql数据库哦,有需要的朋友参考一下. 今天来探索下MSSQL的执行计划,来让大家知道如何查看MSSQL的优化机制,以此来优化SQL查询. 代码如下 复制代码 ...

  2. MySQL 的索引、执行计划、优化器算法

    SQL处理流程 INDEX 索引 索引介绍 索引:是排序的快速查找的特殊数据结构,定义作为查找条件的字段上,又称为键key,索引通过存储引擎实现: 索引相当于一本书的目录,可以优化查询. 优点: 索引 ...

  3. 面试官:不会看 Explain执行计划,简历敢写 SQL 优化?

    来自:程序员内点事 昨天中午在食堂,和部门的技术大牛们坐在一桌吃饭,作为一个卑微技术渣仔默默的吃着饭,听大佬们高谈阔论,研究各种高端技术,我TM也想说话可实在插不上嘴. 聊着聊着突然说到他上午面试了一 ...

  4. mysql 执行计划extra_SQL优化 MySQL版 -分析explain SQL执行计划与Extra

    Extra 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 注:此文章必须有一定的Mysql基础,或观看执行计划入门篇传送门: https://www.cnblogs.com/Sta ...

  5. mysql生成树状执行计划_SQL优化 MySQL版 -分析explain SQL执行计划与笛卡尔积

    SQL优化 MySQL版 -分析explain SQL执行计划 作者 Stanley 罗昊 [转载请注明出处和署名,谢谢!] 首先我们先创建一个数据库,数据库中分别写三张表来存储数据; course: ...

  6. oracle的sql执行计划语句,最权威Oracle获取SQL语句执行计划大全

    该文档为根据相关资料整理.总结而成,主要讲解Oracle数据库中,获取SQL语句执行计划的最权威.最正确的方法.步骤,此外,还详细说明了每种方法中可选项的意义及使用方法,以方便大家和自己日常工作中查阅 ...

  7. 【转自杨建荣博客】通过执行计划中的CONCATENATION分析sql问题

    转自: http://blog.itpub.net/23718752/viewspace-1403180/ 昨天开发的一个同事找到我,说写了一条sql语句,但是执行了半个小时还没有执行完,想让我帮忙看 ...

  8. mysql 执行计划 优化_执行计划

    #### 概念 一个sql语句在没有执行(运行)之前,先计算一下该sql语句需要调用的相关资源,再决定该sql语句是否要最终执行,该行为被称为"执行计划". #### 作用 主要用 ...

  9. map语法获取index_MySQL SQL语法优化——使用Explain查看执行计划

    夜深,最近在写SQL优化的一些文章,看到私聊中,有很多人在问如何判断是否需要优化或者是如何查看MySQL执行计划,本文简要介绍一下MySQL EXPLAIN命令. EXPLAIN命令是查看优化器如何决 ...

最新文章

  1. Gold Code,Gold Sequence
  2. 新手小白 python之路 Day1 (三级菜单功能实现)
  3. camera 输入数据格式 lcd 显示原理
  4. d06调试详细说明_D06电脑板调试软件使用说明_20100706
  5. PostgreSQL Oracle 兼容性之 - INDEX SKIP SCAN (递归查询变态优化) 非驱动列索引扫描优化...
  6. mysql插入图片数据
  7. 机器学习基础--一些基本的概念
  8. 插入排序之表插入排序
  9. USACO 4.3.1 Buy Low, Buy Lower
  10. jQuery AJAX 方法
  11. 网络模型一般是指 OSI 七层参考模型和 TCP/IP 五层参考模型。
  12. 转:孩子,上学去!乖
  13. idea下载Scala插件(详细)
  14. 怎么制作gif动图,gif动态图怎么制作
  15. 解决spacy3.2报错:Can‘t find model ‘en‘.
  16. Tyvj 1039 忠诚2
  17. java javaw javaws MC_java和 javaw 及 javaws的区别解析
  18. 关于unity粒子系统renderer设为mesh(网格)模式后无法旋转的问题
  19. C#-WinForm-简单的音频播放器(基于WindowsMediaPlayer控件)(一)
  20. [转载]Java完美经典读书笔记

热门文章

  1. 欧姆龙CJ2M 与海利普变频器通讯
  2. springboot中使用人大金仓
  3. Self-training Improves Pre-training for Natural Language Understanding 笔记
  4. 如何设置文本不换行省略号显示等CSS常用文本属性
  5. 小程序字体规范、颜色参考(单位/px)
  6. Mysql高级完整版
  7. 各大巨头纷纷出手 数据中心市场硝烟弥漫
  8. POJ3258River Hopscotch题解
  9. c语言全民飞机游戏代码,课内资源 - 基于VC++和OpenCV实现的全民飞机大战游戏
  10. CSS:使用css3制作渐变色纺锤线