最近做的乙個小專案,出租管理,需要計算租金,有點複雜,如下圖
經過研究,寫出**如下:
set ansi_nulls on剛開始還行,資料一多就開始慢,29行資料達到了1.5秒,在網上問了幾天也沒有結果,自己在無意中看了下執行計畫,只覺得自己的螢幕太小,非常複雜,後來看到是最後那個資料zsum有點問題,去掉了速度還可以,在網上又找了找,看到了表變數,果斷在每個查詢後面都以表變數存起來再用,就是上例中注釋的部分,再執行,只有35ms,太好了~set quoted_identifier on
goalter
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 a
join (select standardid,sum(subtotal) as subtotal,orderid from steelbusinesslist group
bystandardid,orderid) b
on a.id =
b.orderid
) ajoin
steelstandard b
on a.standardid = b.id and b.productname =
@pne
) ajoin
steelproject b
on a.projectid = b.id and b.id =
@pid
) a
group
bya.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)assurplus
from
@temp_aasb
) a)
--insert into @temp_b select * from ts
select 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
0then'出庫
'when
1then'入庫
'end) as order_direction from
@temp_b b
sql優化一例
原sql,查詢總數300,每頁15資料也要8秒 select a from table where date format my time,y m d 2012 08 15 limit 0,15 優化後的sql,查詢總數18000,每頁15的資料只要1秒 select a from table wh...
SQL Server 查詢優化一例
前幾天去客戶那裡做效能檢查優化,發現有這樣一大段sql語句,每天會執行幾千次,一共36條相似語句,只是每條語句中的條件 病案大類 不同 select 一般醫療服務費 sum isnull 金額,0 from 流水帳 with nolock where 住院號 zyh and 專案 in select...
Oracle Sql優化一例 利用函式索引
在awr報告中,發現有個sql效率很低 select batch status from t batch info where batch status 0 and sys id stm06 檢視執行計畫發現查詢利用了索引,索引中包含了batch status欄位,但是通過以下sql查詢 selec...