导入表结构:

select * into ProductCategory from AdventureWorksDW2014.dbo.DimProductCategory
select * into Product from AdventureWorksDW2014.dbo.DimProduct

开启磁盘io:

set statistics io on
select EnglishProductName,StandardCost,Color,Size,Weight from Product
where size>'M'--0.189 io:251
set statistics io off

非聚簇索引:

创建的语句:
create nonclustered index nc_product_size on product(size)

再次执行上面的查询代码(提高了三倍):

set statistics io on
select EnglishProductName,StandardCost,Color,Size,Weight from Product
where size>'M' --0.054 io:19
set statistics io off

建立覆盖索引:

create nonclustered index nc_product_size1 on product(size) include(EnglishProductName,
StandardCost,Color,Weight)

再次执行上述语句:

set statistics io on
select EnglishProductName,StandardCost,Color,Size,Weight from Product
where size>'M' --0.003 io:2
set statistics io off

数据库会自动选择索引:

没有创建索引的情况:

set statistics io on
select c.EnglishProductCategoryName,p.EnglishProductName,p.Color,p.Size
from product as p inner join ProductCategory as c on p.ProductSubcategoryKey=c.ProductCategoryKey
where c.ProductCategoryKey=1 --0.1928
set statistics io off

创建索引:

create nonclustered index nc_productcategory_key on ProductCategory(ProductcategoryKey) include
(EnglishProductCategoryName)

在次查询:

set statistics io on
select c.EnglishProductCategoryName,p.EnglishProductName,p.Color,p.Size
from product as p inner join ProductCategory as c on p.ProductSubcategoryKey=c.ProductCategoryKey
where c.ProductCategoryKey=1 --0.1928 io:c 2 p 251
set statistics io off

IO情况:

由此可见 Product表影响比较严重 251

建立一个非聚簇索引:(做一个物理排序)

create nonclustered index nc_product_categorykey on product(productsubcategorykey) include
(englishproductname,color,size)

执行语句:

set statistics io on
select c.EnglishProductCategoryName,p.EnglishProductName,p.Color,p.Size
from product as p inner join ProductCategory as c on p.ProductSubcategoryKey=c.ProductCategoryKey
where c.ProductCategoryKey=1 --4.29 od 1497 oh 783 c 155
set statistics io off

导入三张表:

select * into Customer from AdventureWorks2014.Sales.Customer
select * into OrderHeader from AdventureWorks2014.Sales.SalesOrderHeader
select * into OrderDetail from AdventureWorks2014.Sales.SalesOrderDetail

实现一些业务:

set statistics io on
select c.CustomerID,SUM(od.LineTotal) from OrderDetail as od inner join
orderheader as oh on od.SalesOrderID=oh.SalesOrderID inner join customer as c
on oh.CustomerID =c.CustomerID group by(c.CustomerID) --4.29
set statistics io off

优化的第一步:

1.查看sql语句写法是否有问题(进行改造)

set statistics io on
select oh.CustomerID,sum(od.LineTotal) from OrderDetail as od inner join
OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID) --3.77 od 1497 oh 783
set statistics io off

创建索引:

create nonclustered index nc_OrderDetail_SalesOrderID on OrderDetail(SalesOrderID) include
(linetotal)

创建另外一个索引:针对group by 的列

create nonclustered index nc_OrderHeader_CustomerID on OrderHeader(CustomerID)

在次执行上述语句:

set statistics io on
select oh.CustomerID,sum(od.LineTotal) from OrderDetail as od inner join
OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID) --3.10 od 533 oh 783
set statistics io off

采用索引视图的方式:

create view v_Order_Total
as
select oh.CustomerID,sum(od.LineTotal) as 总额 from OrderDetail as od inner join
OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID)

效果差不多:

set statistics io on
select * from v_Order_Total --3.10 od 533 oh 783
set statistics io off

修改:

alter view v_Order_Total
as
select oh.CustomerID as 客户ID, sum(od.LineTotal) as 总额 from OrderDetail as od inner join
OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID)

对唯一列做聚集索引:

 create clustered index c_vordertotal_customerid on v_order_total(客户ID)

直接运行报错:

解决方案:

在次执行:

alter view v_Order_Total
with schemabinding
as
select oh.CustomerID as 客户ID, sum(od.LineTotal) as 总额 from OrderDetail as od inner join
OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID)

报错:

解决方法:

alter view v_Order_Total
with schemabinding
as
select oh.CustomerID as 客户ID, sum(od.LineTotal) as 总额 from dbo.OrderDetail as od inner join
dbo.OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID)

在次创建:

 create clustered index c_vordertotal_customerid on v_order_total(客户ID)

报错:

办法:

 create unique clustered index c_vordertotal_customerid on v_order_total(客户ID)

报错:

方法:

alter view v_Order_Total
with schemabinding
as
select oh.CustomerID as 客户ID, sum(od.LineTotal) as 总额,COUNT_BIG(*) as 计数 from dbo.OrderDetail as od inner join
dbo.OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID)

执行创建索引:

 create unique clustered index c_vordertotal_customerid on v_order_total(客户ID)

成功

set statistics io on
select * from v_Order_Total --0.09 io:92
set statistics io off

执行计划:

会自动进行值的更新,不用关心

对语句的访问会用到刚才的架构:

set statistics io on
select oh.CustomerID,sum(od.LineTotal) from OrderDetail as od inner join
OrderHeader as oh on od.SalesOrderID=oh.SalesOrderID group by(oh.CustomerID) --0.09 io:92
set statistics io off

SqlServer性能优化索引(五)的更多相关文章

  1. 03.SQLServer性能优化之---存储优化系列

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 概  述:http://www.cnblogs.com/dunitian/p/60413 ...

  2. PLSQL_性能优化索引Index介绍(概念)

    2014-06-01 BaoXinjian

  3. 01.SQLServer性能优化之----强大的文件组----分盘存储

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 文章内容皆自己的理解,如有不足之处欢迎指正~谢谢 前天有学弟问逆天:“逆天,有没有一种方 ...

  4. 02.SQLServer性能优化之---牛逼的OSQL----大数据导入

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 上一篇:01.SQLServer性能优化之----强大的文件组----分盘存储 http ...

  5. SQLServer性能优化专题

    SQLServer性能优化专题 01.SQLServer性能优化之----强大的文件组----分盘存储(水平分库) http://www.cnblogs.com/dunitian/p/5276431. ...

  6. SQLServer性能优化之---数据库级日记监控

    上节回顾:https://www.cnblogs.com/dotnetcrazy/p/11029323.html 4.6.6.SQLServer监控 脚本示意:https://github.com/l ...

  7. JAVA性能优化的五种方式

    一,JAVA性能优化之设计优化 设计优化处于性能优化手段的上层.它往往须要在软件开发之前进行.在软件开发之前,系统架构师应该就评估系统可能存在的各种潜在问题和技术难点,并给出合理的设计方案,因为软件设 ...

  8. SqlServer性能优化(一)

    一:数据存储的方式: 1.数据文件:.mdf或.ndf 2.日志文件:.ldf 二:事务日志的工作步骤: 1.数据修改由应用程序发出(在缓冲区进行缓存) 2.数据页位于缓存区缓冲中,或者读入缓冲区缓存 ...

  9. SqlServer性能优化 查询和索引优化(十二)

    查询优化的过程: 查询优化: 功能:分析语句后最终生成执行计划 分析:获取操作语句参数 索引选择 Join算法选择 创建测试的表: select * into EmployeeOp from Adve ...

随机推荐

  1. 生产排产表DL-ZPPR002

    *&---------------------------------------------------------------------* *& Report ZPPR002 * ...

  2. MacOS长按无效问题

    defaults write -g ApplePressAndHoldEnabled -bool FALSE 注销并重新登录系统使其更改生效. 如果需要恢复长按键盘可以重音字符或非英文字符的功能,请打 ...

  3. Sublime Text 3 使用问题答疑

    命令面板/命令模式:ctrl+shift+pctrl+cctrl+v → ctrl+shift+v粘贴时会保持原格式(缩进)ctrl+sctrl+z撤销ctrl+y恢复撤销在当前行下面添加一行:ctr ...

  4. 如何用fir.im 命令行工具 打包上传

    1.注册fir.拿到token 2.安装 fir-cli 使用 Ruby 构建, 无需编译, 只要安装相应 gem 即可. $ ruby -v # > 1.9.3 $ gem install f ...

  5. python 获取脚本所在目录

    平时写python经常会想获得脚本所在的目录,例如有个文件跟脚本文件放在一个相对的目录位置,那就可以通过脚本文件的目录找到对应的文件,即使以后脚本文件移到其他地方,脚本也基本不需要改动(相对于写死目录 ...

  6. 7个步骤:让JavaScript变得更好

    Dan Odell介绍了他编写的七步写出无瑕代码的计划,是在简化过程中最有用的工具.   随着浏览器性能提高,伴随着新的HTML5的编程接口的稳步采用,网页上的JavaScript的音量在逐渐增加.然 ...

  7. MicroERP开发技术分享:技术选型

    为什么要想起开发一个近似一套完整的ERP软件呢,原因有二:一是想在空闲时间把以前的进销存软件丰富一下,结果越搞越大了:二是这些年光搞C#了,不想把VB6忘光了 非微软的东西还真没时间去学,也有主要原因 ...

  8. C# 大小写转换,方便index of

    ToUpper:小写转大写ToLower:大写转小写 例: string str=120cm*150g/m2;从中取出120和150,但是又要规避大小写问题,这时候就需要将str转换为大写,然后ind ...

  9. C语言-预估校正法求常微分方程

    #include<stdio.h> #include<math.h> #define n 14 int main(){ double a = 0.0, b = 1.4,h,m= ...

  10. c语言调用函数打印一维数组-2-指针

    方法一(规范): #include <stdio.h> #include <math.h> #include <stdlib.h> //函数预声明 ], int m ...