2012-11-28 44 views
-1

我正在使用以下查詢在我的一個Java swing應用程序中顯示記錄。此查詢執行速度非常慢。有沒有什麼辦法可以提高性能如何在tsql中提高子查詢的性能或子查詢的替代性

有沒有什麼辦法在較小的時間間隔內完成任務。

select d.orderid,d.ordername,d.design,isnull(e.OrderQty,0) OrderQty 
from 
(
select a.orderid,ordername,design 
from filerorn a,ordersNew.dbo.orders b where a.orderid=b.orderid and sub_stage = 'G' 
And a.id [email protected] 
)d 
LEFT JOIN 
(
select a.OrderId,a.Design,(case when b.ItemId is null 
then a.Qty else 1 end) -isnull(c.Qty,0) OrderQty from 
OrdersNew..ordersDesign a 
left join 
OrdersNew..OrdersBGTPDesign b 
on a.OrderId=b.OrderId and a.Design 
COLLATE DATABASE_DEFAULT 
=b.Design COLLATE DATABASE_DEFAULT and a.Sno=b.SNo 
left join 
(select a.OrderId,Design,Qty from ExcludeDesign a 
left join 
dbo.ExcludeDesign_d b 
on a.ID=b.ID 

)c 
on a.OrderId=c.OrderId and a.Design 
COLLATE DATABASE_DEFAULT 
=c.Design COLLATE DATABASE_DEFAULT and a.Sno=c.Sno 

)e 
ON d.OrderId=e.OrderId and d.Design COLLATE DATABASE_DEFAULT 
=e.Design COLLATE DATABASE_DEFAULT 
and d.sno=e.Sno 
and d.ItemId=e.ItemId 
order by d.ordername,d.design,d.sno,d.Itemid 
select d.orderid,d.ordername,d.design,isnull(e.OrderQty,0) OrderQty 
from 
(
select a.orderid,ordername,design 
from filerorn a,ordersNew.dbo.orders b where a.orderid=b.orderid and sub_stage = 'G' 
And a.id [email protected] 
)d 
LEFT JOIN 
(
select a.OrderId,a.Design,(case when b.ItemId is null 
then a.Qty else 1 end) -isnull(c.Qty,0) OrderQty from 
OrdersNew..ordersDesign a 
left join 
OrdersNew..OrdersBGTPDesign b 
on a.OrderId=b.OrderId and a.Design 
COLLATE DATABASE_DEFAULT 
=b.Design COLLATE DATABASE_DEFAULT and a.Sno=b.SNo 
left join 
(select a.OrderId,Design,Qty from ExcludeDesign a 
left join 
dbo.ExcludeDesign_d b 
on a.ID=b.ID 

)c 
on a.OrderId=c.OrderId and a.Design 
COLLATE DATABASE_DEFAULT 
=c.Design COLLATE DATABASE_DEFAULT and a.Sno=c.Sno 

)e 
ON d.OrderId=e.OrderId and d.Design COLLATE DATABASE_DEFAULT 
=e.Design COLLATE DATABASE_DEFAULT 
and d.sno=e.Sno 
and d.ItemId=e.ItemId 
order by d.ordername,d.design,d.sno,d.Itemid 

其中orderdesign和訂單都有記錄萬元

Edit 

15%

select a.orderid,ordername,design 
from filerorn a,ordersNew.dbo.orders b where a.orderid=b.orderid and sub_stage = 'G' 
And a.id [email protected] 

46%

select a.OrderId,a.Design,(case when b.ItemId is null 
then a.Qty else 1 end) -isnull(c.Qty,0) OrderQty from 
OrdersNew..ordersDesign a 
left join 
OrdersNew..OrdersBGTPDesign b 
on a.OrderId=b.OrderId and a.Design 
+1

您是否使用了查詢分析器來確定查詢的最慢部分在哪裏? –

+0

爲什麼在這裏指定排序順序? 'a.Design COLLATE DATABASE_DEFAULT = b.Design COLLATE DATABASE_DEFAULT'請你包含表格的模式和數據庫的缺省排序順序?知道這些字段是否具有不同的歸類序列和/或數據庫缺省值是否與這些字段不同是很重要的。如果您在查詢中強制排序順序發生變化,則會非常昂貴。 – MatBailie

+0

是的,我這樣做是由於兩個表的排序規則的差異,因爲它產生的錯誤 –

回答

1

如何查看和彈出一些索引以使其更快搜索。或者在你的桌子上製作索引,如果你不喜歡做一個視圖,可以加快搜索速度。

+0

已經有索引在OrdersDesign OrderID,Design,Sno –

+0

刪除你的答案我想刪除這個問題@ user1811846 –