我是SQL的新手,正在编写一个查询,在该查询中,我从多个表中返回了许多采购订单信息。
我的查询运行缓慢,因此我四处搜寻,许多人建议研究执行计划。所以我做到了,但是我不完全了解结果。
我的执行计划说,
“缺少索引(影响99.8849):在[dbo]上创建非索引索引[]。[容器]([TrackingNumber]) 包含([ResolvedCarrierTrackingLink])
它正在谈论的表[dbo].[Container]
,加上使我的查询从采用2 seconds
到采用7 seconds
的方式。很明显,这是正确的,并且此表存在某种问题。
如何创建此索引?这是我可以在查询中执行的操作吗?还是必须进入实际的表并在其中乱七八糟?这是我正在运行的特定查询的临时更改,还是永久更改?是否有很好的资源指南来理解执行计划的结果及其含义?
这是我的完整查询代码:
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 ist.Stage_Code
ELSE pst.Stage_Code
END AMT_Stage,
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,
CASE
WHEN sh.packslip IS NOT NULL
THEN 'SHIPPED & UPLOADED'
ELSE z.status
END Accellos_Status,
CASE
WHEN sh.packslip IS NULL
THEN ph.Date_modfy
ELSE sh.Date_modfy
END Accellos_Timestamp,
o.Warehouse_Code,
b.UPS_Tracking_Number Tracking_Number,
CASE
WHEN sh.packslip IS NULL
THEN ph.ship_name
ELSE sh.SHIP_NAME
END Customer_Name,
CASE
WHEN sh.packslip IS NULL
THEN ph.SHIP_ADD1
ELSE sh.SHIP_ADD1
END Customer_Addr,
CASE
WHEN sh.packslip IS NULL
THEN ph.SHIP_ADD2
ELSE sh.SHIP_ADD2
END Customer_Addr2,
CASE
WHEN sh.packslip IS NULL
THEN ph.SHIP_CITY
ELSE sh.SHIP_CITY
END Customer_City,
case when sh.packslip is null then ph.SHIP_PROV else sh.SHIP_PROV end Customer_State,
case when sh.packslip is null then ph.SHIP_ZIP else sh.SHIP_ZIP end Customer_Zip,
o.Ship_Via_Code,
case when sh.packslip is null then ph.SHIP_VIA else sh.SHIP_VIA end Ship_Via,
sv.Description_1 as Ship_Descrp,
case when sh.packslip is null then ph.SHIP_SERVC else sh.SHIP_SERVC end Ship_Service,
case when sh.packslip is null then ph.SERVICE else sh.SERVICE end Service_Descrp,
case when sh.packslip is null then ph.SHIP_NUM else sh.SHIP_NUM end Ship_Num,
case when sh.packslip is null then ph.COST_SHIP else sh.COST_SHIP end Ship_Cost,
SUM(od.Freight_Charges) OVER(PARTITION BY o.control_number) AS Shipping_Cost_To_Customer,
case when sh.packslip is null then ph.SHIP_WGHT else sh.SHIP_WGHT end Ship_Weight,
cont.ResolvedCarrierTrackingLink
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].Invoices_Stage ist (nolock) ON o.Company_Code = ist.Company_Code
AND o.Division_Code = ist.Division_Code
AND i.Invoice_Number = ist.Invoice_Number
LEFT JOIN
[JMNYC-AMTDB].[AMTPLUS].[dbo].PickTickets_Stage pst (nolock) on o.Company_Code=pst.Company_Code and o.Division_Code=pst.Division_Code and p.PickTicket_Number=pst.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 o.Customer_Purchase_Order_Number = sh.cust_po
LEFT JOIN
[A1Ship].[dbo].Container cont ON cont.TrackingNumber = b.UPS_Tracking_Number
WHERE
CONCAT((o.Company_Code), (o.Division_Code)) = '03001'
AND o.Customer_Number = 'ecom2x'
AND o.Customer_Purchase_Order_Number = '3124188'
答案 0 :(得分:0)
那是什么?
您确实应该对索引进行一些研究,但是从根本上说,它们往往会加快某些查询的速度,同时减慢更新,插入和删除的速度。
如何制作一个?
至少在最新版本的SSMS中,您可以右键单击该消息,然后选择“缺少索引详细信息”,这将打开脚本编辑器窗口。您可以编辑索引的名称。例如,您发布的索引如下所示:
CREATE NONCLUSTERED INDEX [IX_Container_TrackingNumber_INC_ResolvedCarrierTrackingLink]
ON [dbo].[Container] ([TrackingNumber])
INCLUDE ([ResolvedCarrierTrackingLink])
您可以执行此tsql代码来创建索引。根据您的组织,您可能需要向登录名/用户添加权限才能执行此操作,还可能需要OPS或DBA团队的批准。