[英]“Create Nonclustered Index”, What is that and how do I make one?
I am new to SQL and am writing a query where I return a lot of information from multiple tables for a purchase order. 我是SQL的新手,正在编写一个查询,在该查询中我从多个表中返回了很多采购订单信息。
My query was running slow so I googled around and many advised to look into the execution plan. 我的查询运行缓慢,因此我四处搜寻,许多人建议研究执行计划。 So I did that but I don't fully understand the results. 所以我做到了,但是我不完全了解结果。
My execution plan says, 我的执行计划说
"Missing Index (Impact 99.8849): CREATE NONCLUSTERED INDEX [] ON [dbo].[Container] ([TrackingNumber]) INCLUDE ([ResolvedCarrierTrackingLink]) “缺少索引(Impact 99.8849):在[dbo]上创建非索引索引[]。[容器]([TrackingNumber])包含([ResolvedCarrierTrackingLink]
The table it is talking about, [dbo].[Container]
, adding that made my query go from taking 2 seconds
to taking 7 seconds
. 它正在谈论的表[dbo].[Container]
加上了使我的查询从2 seconds
变为7 seconds
。 So obviously, it is right and there is some sort of problem with this table. 很明显,这是正确的,并且此表存在某种问题。
How do I create this index? 如何创建该索引? Is this something I can do in my query or do I have to go into the actual table and mess around there? 这是我可以在查询中执行的操作吗?还是必须进入实际的表并在其中乱七八糟? Is this a temporary change for the specific query I'm running or is this a permanent change? 这是我正在运行的特定查询的临时更改,还是永久更改? Is there a good resource guide for understanding the results of the the execution plan and what they mean? 是否有很好的资源指南来理解执行计划的结果及其含义?
This is my full query code: 这是我的完整查询代码:
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'
What is that? 那是什么?
You really should do some research on indexes, but basically they tend to make certain queries faster while slowing down updates, inserts and deletes. 您确实应该对索引进行一些研究,但是从根本上说,它们倾向于使某些查询更快,同时减慢更新,插入和删除的速度。
How to make one? 如何做一个?
At least in the latest version of SSMS, you can right click the message and choose Missing Index Details which will open a script editor window. 至少在最新版本的SSMS中,您可以右键单击该消息,然后选择“缺少索引详细信息”,这将打开脚本编辑器窗口。 You can edit the name of the index. 您可以编辑索引的名称。 For example, the index you posted would look something like this: 例如,您发布的索引如下所示:
CREATE NONCLUSTERED INDEX [IX_Container_TrackingNumber_INC_ResolvedCarrierTrackingLink]
ON [dbo].[Container] ([TrackingNumber])
INCLUDE ([ResolvedCarrierTrackingLink])
You can execute this tsql code to create the index. 您可以执行此tsql代码来创建索引。 Depending on your organization you may need permissions added to your login/user to execute this and also may need approval from an OPS or DBA team. 根据您的组织,您可能需要向您的登录名/用户添加权限才能执行此操作,还可能需要OPS或DBA团队的批准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.