SQL SERVER 优化实例:从运行30分钟到运行只要30秒

2016-02-19 18:16 6 1 收藏

今天图老师小编要向大家分享个SQL SERVER 优化实例:从运行30分钟到运行只要30秒教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - 编程语言 】

以下的SQL语句在服务器需要运行长达30分钟才能完成:SELECT   dbo.ComFlow.ComFlowCode, dbo.ComFlow.ComCode,
dbo.ComFlow.CustCode, dbo.ComFlow.DepartCode, dbo.ComFlow.SaleCode,
           dbo.ComFlow.EmpCode, dbo.ComFlow.Quantity *
dbo.Commodity.ConvertRate AS Quantity, CONVERT(datetime, DATENAME(yyyy,
           dbo.ComFlow.FlowDate) + '-' + DATENAME(mm, dbo.ComFlow.
FlowDate) + '-' + DATENAME(dd, dbo.ComFlow.FlowDate)) AS FlowDate,
           dbo.ComFlow.SalType, dbo.Employee.DepartCode AS
DepartIn, dbo.Sale.DepartCode AS DepartOut,
           dbo.ComFlow.Quantity * dbo.Commodity.TradePrice *
dbo.Commodity.Discount / 100 AS Total, dbo.Department.GrpCode AS GrpCodeIn,
           Department1.GrpCode AS GrpCodeOut
FROM     dbo.ComFlow INNER JOIN
           dbo.Customer ON
        dbo.ComFlow.SalType IN (N'促销', N'流向退货', N'多级流向')
AND dbo.ComFlow.CustCode = dbo.Customer.CustCode
        Or ComFlow_1.SalType IN (N'自然流向', N'自然流向退货')
AND ComFlow_1.OutCustCode = Customer_1.CustCode
      INNER JOIN
           dbo.CustomerRelation ON dbo.ComFlow.ComCode =
dbo.CustomerRelation.ComCode AND
           dbo.CustomerRelation.CustCode = dbo.Customer.CustCode
INNER JOIN
           dbo.Employee ON dbo.CustomerRelation.EmpCode =
dbo.Employee.
EmpCode INNER JOIN
           dbo.Sale ON dbo.ComFlow.SaleCode = dbo.Sale.SaleCode
INNER JOIN
           dbo.Department ON dbo.Department.DepartCode = dbo.Employee.DepartCode INNER JOIN
           dbo.Department AS Department1 ON Department1.DepartCode
= dbo.Sale.DepartCode AND
           dbo.Department.GrpCode Department1.GrpCode INNER JOIN
           dbo.Commodity ON dbo.ComFlow.ComCode = dbo.Commodity.ComCode
WHERE  
(NOT (dbo.ComFlow.SalType = N'流向退货')) OR
           (NOT (dbo.Customer.Type = N'医药公司'))

虽然说,我们使用这个语句的应用是一个BI应用,实时性要求不高,但是,我觉得没有道理会运行这么久,应该有办法优化。

  第一步,我看了看索引,好像没有问题,都有

  第二步,检查关系,有没有错,没有错,和应用要求是一致的,尤其计算出来的结果和同事使用另外一种方法的计算结果是一致的(同事使用多个视图分步累加)。

  第三步,看看这个语句有没有什么特别之处?

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  我注意到特别之处就是使用底色标出的部分:

dbo.ComFlow.SalType IN (N'促销', N'流向退货', N'多级流向')
AND dbo.ComFlow.CustCode = dbo.Customer.CustCode        
Or ComFlow_1.SalType IN (N'自然流向', N'自然流向退货') AND ComFlow_1.OutCustCode
= Customer_1.CustCode

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)


  这是一个Or关系的关联?就是这个问题?

  分析这个语句可以看出,这个Or语句其实是可以分解成Union语句的,所以把它变成下面的:

SELECT   dbo.ComFlow.ComFlowCode, dbo.ComFlow.ComCode,
dbo.ComFlow.CustCode, dbo.ComFlow.DepartCode, dbo.ComFlow.SaleCode,
           dbo.ComFlow.EmpCode, dbo.ComFlow.Quantity *
dbo.Commodity.ConvertRate AS Quantity, CONVERT(datetime, DATENAME(yyyy,
           dbo.ComFlow.FlowDate) + '-' + DATENAME(mm,
dbo.ComFlow.FlowDate) + '-' + DATENAME(dd, dbo.ComFlow.FlowDate)) AS FlowDate,
           dbo.ComFlow.SalType, dbo.Employee.DepartCode AS DepartIn,
dbo.Sale.DepartCode AS DepartOut,
           dbo.ComFlow.Quantity * dbo.Commodity.TradePrice *
dbo.Commodity.Discount / 100 AS Total, dbo.Department.GrpCode AS GrpCodeIn,
           Department1.GrpCode AS GrpCodeOut
FROM     dbo.ComFlow INNER JOIN
           dbo.Customer ON dbo.ComFlow.SalType IN (N'促销', N'流向退货',
N'多级流向') AND dbo.ComFlow.CustCode = dbo.Customer.CustCode INNER JOIN
           dbo.CustomerRelation ON dbo.ComFlow.ComCode = dbo.CustomerRelation.ComCode AND
           dbo.CustomerRelation.CustCode = dbo.Customer.CustCode INNER JOIN
           dbo.Employee ON dbo.CustomerRelation.EmpCode = dbo.Employee.EmpCode INNER JOIN
           dbo.Sale ON dbo.ComFlow.SaleCode = dbo.Sale.SaleCode INNER JOIN
           dbo.Department ON dbo.Department.DepartCode =
dbo.Employee.DepartCode INNER JOIN
           dbo.Department AS Department1 ON Department1.DepartCode =
dbo.Sale.DepartCode AND
           dbo.Department.GrpCode Department1.GrpCode INNER JOIN
           dbo.Commodity ON dbo.ComFlow.ComCode = dbo.Commodity.ComCode
WHERE   (NOT (dbo.ComFlow.SalType = N'流向退货')) OR
           (NOT (dbo.Customer.Type = N'医药公司'))
UNION ALL
SELECT   ComFlow_1.ComFlowCode, ComFlow_1.ComCode, ComFlow_1.CustCode,
ComFlow_1.DepartCode, ComFlow_1.SaleCode, ComFlow_1.EmpCode,
           ComFlow_1.Quantity * Commodity_1.ConvertRate AS Quantity,
CONVERT(datetime, DATENAME(yyyy, ComFlow_1.FlowDate) + '-' + DATENAME(mm,
           ComFlow_1.FlowDate) + '-' + DATENAME(dd, ComFlow_1.FlowDate))
AS FlowDate, ComFlow_1.SalType, Employee_1.DepartCode AS DepartIn,
           Sale_1.DepartCode AS DepartOut, ComFlow_1.Quantity *
Commodity_1.TradePrice * Commodity_1.Discount / 100 AS Total,
           Department_1.GrpCode AS GrpCodeIn, Department1.GrpCode AS GrpCodeOut
FROM     dbo.ComFlow AS ComFlow_1 INNER JOIN
           dbo.Customer AS Customer_1 ON ComFlow_1.SalType IN (N'自然流向',
N'自然流向退货') AND
           ComFlow_1.OutCustCode = Customer_1.CustCode INNER JOIN
           dbo.CustomerRelation AS CustomerRelation_1 ON
ComFlow_1.ComCode = CustomerRelation_1.ComCode AND
           CustomerRelation_1.CustCode = Customer_1.CustCode INNER JOIN
           dbo.Employee AS Employee_1 ON CustomerRelation_1.EmpCode =
Employee_1.EmpCode INNER JOIN
           dbo.Sale AS Sale_1 ON ComFlow_1.SaleCode = Sale_1.SaleCode
INNER JOIN
           dbo.Department AS Department_1 ON Department_1.DepartCode
= Employee_1.DepartCode INNER JOIN
           dbo.Department AS Department1 ON Department1.DepartCode =
Sale_1.DepartCode AND Department_1.GrpCode Department1.GrpCode INNER JOIN
           dbo.Commodity AS Commodity_1 ON ComFlow_1.ComCode = Commodity_1.ComCode
WHERE   (NOT (ComFlow_1.SalType = N'流向退货')) OR
           (NOT (Customer_1.Type = N'医药公司'))

  需要30分钟才能运行完毕的语句只要30几秒就完成了。

  这里可以看出,Or的语句可能破坏了索引的作用。使用Or进行关联虽然逻辑非常清楚,但是效率低。

  使用Union虽然冗长,但是用在这里效率要高。

来源:http://www.tulaoshi.com/n/20160219/1617141.html

延伸阅读
组网并不是网络专家和高手的专利,如果你是一位初学者,只需掌握一点点计算机知识,再找个老师辅导就有足够实力组建一个小网,费话不多说,下面笔者就介绍初学者如何快速组建小型寝室网络。 动手之前先准备一下组网的所需要的设备:电脑(装有Win98)、8口的集线器、双绞线、RJ-45头、夹线钳、网卡和网线测试仪(可选)。 A 放置设备 将计算机...
1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT'; 2. /*+FIRST_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳响应时间,使资源消耗最小化. 例如: SELECT /*+FIRST_ROWS*/ EMP_NO,EMP_NAM,DAT_IN...
游泳30分钟有效保护前列腺 .hzh {display: none; } 有研究表明,适度的体育锻炼可保持内分泌稳定,调节免疫功能,从而降低前列腺癌发病的危险性。 每天游泳 可防前列腺癌 男性如果每天游泳30分钟,患晚期前列腺癌的可能性会大大降低。而从事骑自行车和体操等运动强度相对较大 的男性,患前列腺癌的概率要比前者高出30...
标签: Web开发
by Jim Hollenhorst  译 寒带鱼 你是否曾经想过正则表达式是什么,怎样能够快速得到对它的一个基本的认识?我的目的就是在30分钟内带你入门并且对正则表达式有一个基本的理解。事实是正则表达式并没有它看起来那么复杂。学习它最好的办法就是开始写正则表达式并且不断实践。在最初的30分钟之后,你就应该知道一些基本的结构并且...
一般来讲睡前瑜伽20~30分钟为宜。运动后30~40分钟后睡觉,人将很容易进入深度睡眠状态,从而提高睡眠质量。现在就教你几套针对身体各个部位简单有效的睡前瑜伽,既减肥养生又助眠,赶紧看看吧。 一、锻炼全身的睡前操 ●双手合十置于胸前。 ●双臂尽量向上伸展,保持十秒。 ●双臂打开呈一百八十度。 ●...

经验教程

160

收藏

7
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部