SQL查询花费的时间太长。如何提高查询效率?从数据库表创建较小的表?

时间:2018-12-31 17:20:41

标签: sql sql-server

我是SQL的新手,我试图做一个简单的查询,从许多不同的表中收集有关一个订单的许多信息。

这是我的代码(主要是select语句):

Select 

o.Customer_Purchase_Order_Number as Shopify_Num,
o.Control_Number, 

o.Date_Entered as AMT_Timestamp,

case when p.PickTicket_Number is null then i.PickTicket_Number else p.PickTicket_Number end PickTicket_Number,

i.Invoice_Number,

CASE WHEN  o.Order_Status=30 THEN 'CANCELED' 
WHEN  o.Order_Status=45 THEN 'SHIPPED' 
WHEN  o.Order_Status=10 THEN 'NORMAL' 
WHEN  o.Order_Status=20 THEN 'NORMAL' 
END Order_Status, 

od.Odet_Line_Number,
od.Item_Number,
st.Description,
od.Color_Code,
c.Color_Description,

o.Order_Value as Total_Price_Before_Discount, 
od.Price*od.Quantity_Ordered as Line_Price_Before_Discount, 
od.Discount_Value, 
od.Discount_Percentage, 
od.Price*od.Quantity_Ordered-od.Discount_Value as Line_Price_After_Discount,

SUM(od.Price*od.Quantity_Ordered-od.Discount_Value) OVER(PARTITION BY o.control_number) AS Total_Price_After_Discount,

od.Tax_Value, od.Tax_Percentage,

od.Quantity_Ordered,
od.Quantity_Allocated,
od.Quantity_Invoiced,

CASE WHEN  od.Line_Status=90 THEN 'ALLOCATED'
WHEN  od.Line_Status=80 THEN 'CANCELED' 
WHEN  od.Line_Status=70 THEN 'SHIPPED' 
WHEN  od.Line_Status=50 THEN 'NORMAL' 
END Line_Status,

SUM(od.Freight_Charges) OVER(PARTITION BY o.control_number) AS Shipping_Cost_To_Customer,

case When sh.packslip is not null then 'SHIPPED & UPLOADED' ELSE z.status END Accellos_Status,
sh.Date_modfy Accellos_Timestamp, 

o.Warehouse_Code,
b.UPS_Tracking_Number AMT_Tracking_Number, 
sm.TRACKTRACE Accellos_Tracking_Number, 
sh.SHIP_NAME ,
sh.SHIP_ADD1 ,
sh.SHIP_ADD2 ,
sh.SHIP_CITY ,
sh.SHIP_PROV ,
sh.SHIP_ZIP,
o.Ship_Via_Code, 
sv.Description_1 as Ship_Descrp,
sh.SHIP_VIA,
sh.SHIP_SERVC,
sh.SERVICE,
sh.SHIP_NUM,
sh.COST_SHIP,
sh.SHIP_WGHT

from [JMNYC-AMTDB].[AMTPLUS].[dbo].Orders o (nolock)
join [JMNYC-AMTDB].[AMTPLUS].[dbo].Order_Detail od (nolock) on o.Company_Code=od.Company_Code and o.Division_Code=od.Division_Code and o.Control_Number=od.Control_Number
left join [JMNYC-AMTDB].[AMTPLUS].[dbo].PickTickets P (nolock) on o.Company_Code=p.Company_Code and o.Division_Code=p.Division_Code and o.Control_Number=p.Control_Number
left join [JMNYC-AMTDB].[AMTPLUS].[dbo].Invoices i (nolock) on o.Company_Code=i.Company_Code and o.Division_Code=i.Division_Code and o.Control_Number=i.Control_Number
left join [JMNYC-AMTDB].[AMTPLUS].[dbo].box b (nolock) on o.Company_Code=b.Company_Code and o.Division_Code=b.Division_Code and i.PickTicket_Number=b.PickTicket_Number

left join [JMNYC-AMTDB].[AMTPLUS].[dbo].Color c (nolock) on od.Color_Code = c.Color_Code
left join [JMNYC-AMTDB].[AMTPLUS].[dbo].Style st (nolock) on o.Company_Code=st.Company_Code and o.Division_Code=st.Division_Code and od.item_number=st.item_number and c.color_code = st.color_code 
left join [JMNYC-AMTDB].[AMTPLUS].[dbo].Ship_Via_File sv (nolock) on o.ship_via_code = sv.ship_via_code

left join pickhead ph (nolock) on p.PickTicket_Number=ph.packslip
left join Z_Status z on ph.PROCSTEP=z.procstep
left join SHIPHIST sh (nolock) on case when p.PickTicket_Number is null then i.PickTicket_Number else p.PickTicket_Number end=sh.packslip

left join SHIPMSTR2 sm (nolock) on case when p.PickTicket_Number is null then i.PickTicket_Number else p.PickTicket_Number end=sm.packslip

where LTRIM(RTRIM(o.Company_Code))+LTRIM(RTRIM(o.Division_Code))='03001'  and o.Customer_Number='ecom2x'
and o.Customer_Purchase_Order_Number='3112480'

此查询大约需要10秒钟才能运行。有什么办法可以提高效率?它已经是准系统了。我假设我应该首先尝试最小化任何冗余表,因为我要加入的这些表非常大。如果我需要所有选择的桌子,我能做些什么吗?当我要加入的表中有很多列时,它们只是针对整个表中的一个特定列而加入。这会改变什么吗?

因此,我可以从数据库表中创建一堆新表,仅选择所需的列。然后我可以改用那些表。我试图做这样的事情,但它只会使查询运行无休止(或至少一分钟)。

这是我尝试的:

我认为它失败了,因为我从本质上告诉它重新创建这个庞大的表,这需要很多时间。

SELECT Company_Code, Division_Code, Control_Number, Customer_Purchase_Order_Number, 
       Date_Entered, Order_Status, Order_Value, 
       Warehouse_Code, Ship_Via_Code 
       into #o from [JMNYC-AMTDB].[AMTPLUS].[dbo].Orders OT (nolock)

SELECT Company_Code, Division_Code, Control_Number, Odet_Line_Number, 
       Item_Number, Color_Code, Price, 
       Discount_Value, Discount_Percentage, Tax_Value, Tax_Percentage,
       Quantity_Ordered, Quantity_Allocated, Quantity_Invoiced, Line_Status, Freight_Charges 
       into #od from [JMNYC-AMTDB].[AMTPLUS].[dbo].Order_Detail ODT (nolock) 
.
.
.
from #o (nolock)
join #od (nolock) on o.Company_Code=od.Company_Code and o.Division_Code=od.Division_Code and o.Control_Number=od.Control_Number
left join #p (nolock) on o.Company_Code=p.Company_Code and o.Division_Code=p.Division_Code and o.Control_Number=p.Control_Number
.
.
.

有人知道如何加快此代码的速度/总体上使它更有效吗?

0 个答案:

没有答案