博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sql查询利用表变量优化一例
阅读量:5052 次
发布时间:2019-06-12

本文共 2637 字,大约阅读时间需要 8 分钟。

最近做的一个小项目,出租管理,需要计算租金,有点复杂,如下图

 

经过研究,写出代码如下:

set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER proc [dbo].[Expensedetails]        @PID int,        @PNE nvarchar(64)as--declare @Temp_A table(rank int null,projectID int null,ProjectName nvarchar(200) null,ProductName nvarchar(64) null,OrderDirection int null,orderdate smalldatetime null,subtotal decimal(18,2) null);--declare @Temp_B table(rank int null,projectID int null,ProjectName nvarchar(200) null,ProductName nvarchar(64) null,OrderDirection int null,orderdate smalldatetime null,subtotal decimal(18,2) null, uday decimal(18,2) null,surplus decimal(18,2) null,counts decimal(18,2) null);with t as (select Row_Number() OVER ( ORDER by orderdate ASC) rank, a.ProjectID,a.ProjectName,a.ProductName,a.OrderDirection,a.OrderDate,sum(a.SubTotal) as SubTotal from (select a.ID,a.ProjectID,b.ProjectName,a.ProductName,a.OrderDirection,a.OrderDate,a.SubTotal from (select a.ID,b.ProductName,a.ProjectID,a.OrderDirection,a.OrderDate,a.SubTotal from (select a.ID,a.ProjectID,b.StandardID,a.OrderDirection,a.OrderDate,b.SubTotal from SteelBusinessOrder ajoin (select StandardID,sum(SubTotal) as SubTotal,OrderID from SteelBusinessList group by StandardID,OrderID) bon a.ID = b.OrderID) ajoin SteelStandard bon a.StandardID = b.ID and b.ProductName = @PNE) ajoin SteelProject bon a.ProjectID = b.ID and b.ID = @PID) a group by a.ID,a.ProjectID,a.ProjectName,a.ProductName,a.OrderDirection,a.OrderDate)--insert into @Temp_A select * from t;with ts as (select *,uday * surplus as counts from (select  *,(select DATEDIFF(d,(select OrderDate from @Temp_A a where a.rank = b.rank),case when((select  min(OrderDate) from @Temp_A a where a.rank > b.rank ) is null) then dateadd(d,1,getdate())else (select  min(OrderDate) from @Temp_A a where a.rank>b.rank ) end)) as uday,isnull(isnull((select sum(isnull(SubTotal,0)) from @Temp_A a where a.rank <= b.rank and OrderDirection = 0 ),0)-isnull((select sum(isnull(SubTotal,0)) from @Temp_A a where a.rank <= b.rank and OrderDirection = 1 ),0),0)as surplusfrom @Temp_A as b) a)--insert into @Temp_B select * from tsselect rank,projectID,ProjectName,ProductName,OrderDirection,convert(char(10),orderdate,120) as orderdate,subtotal, uday,surplus,counts,(select SUM(counts) as zsum from @Temp_B  a where a.rank < = b.rank) as zsum,(case OrderDirection when 0 then '出库' when 1 then '入库' end) as Order_Direction   from @Temp_B  b

刚开始还行,数据一多就开始慢,29行数据达到了1.5秒,在网上问了几天也没有结果,自己在无意中看了下执行计划,只觉得自己的屏幕太小,非常复杂,后来看到是最后那个数据zsum有点问题,去掉了速度还可以,在网上又找了找,看到了表变量,果断在每个查询后面都以表变量存起来再用,就是上例中注释的部分,再执行,只有35ms,太好了~

转载于:https://www.cnblogs.com/happysmile/archive/2013/05/28/3104195.html

你可能感兴趣的文章
javascript正则(带g符号) 多次调用test 结果交替出现
查看>>
浅析jQuery删除节点的三个方法
查看>>
python 环境安装
查看>>
ajax无刷新上传图片
查看>>
观察者模式
查看>>
学习笔记:CLR的执行模型
查看>>
并发的简单介绍2
查看>>
ThreadLocal详解
查看>>
Python安装与环境变量的配置
查看>>
textarea跟随内容自动伸缩高度实现方案
查看>>
freeswitch与外部网关链接
查看>>
HDU 1284 钱币兑换问题(母函数)
查看>>
touch事件的分发机制
查看>>
Android 系统状态栏一体化
查看>>
Swift - 可选类型说明
查看>>
Android ViewTreeObserver简介
查看>>
剑指Offer - 九度1385 - 重建二叉树
查看>>
剑指Offer - 九度1506 - 求1+2+3+...+n
查看>>
LeetCode - Binary Tree Maximum Path Sum
查看>>
Careercup - Microsoft面试题 - 5700293077499904
查看>>