我将在这里寻求帮助,我有这个查询加入8个不同的表,每次我运行它,它将需要永远,我希望有人可能能够提供帮助帮助我简化以下查询,任何帮助/建议将不胜感激,谢谢。
with CTE as(
select DATEPART(year, a.DATE_RCVD) AS YearRcvd, a.DATE_RCVD, DATEPART(month, a.DATE_RCVD) AS MonthRcvd, a.ITEM_CODE, a.VENDOR_CODE,
d.VENDOR_NAME, a.ITEM_COST_RCVD AS PO_Price, a.ITEM_COST_INV AS AP_Inv_Price, a.QTY_RCVD, b.INVENT_TYPE_IND, a.PO_NUMB, a.PO_LINE_NUMB,
a.DATE_WANTED,
CASE WHEN VENDOR_TERMS_PERCENT = 0 THEN 'NET' + ' ' + CONVERT(varchar(4), e.VENDOR_TERMS_DAYS) ELSE CONVERT(varchar(7),
e.VENDOR_TERMS_PERCENT) + '%' + ' ' + CONVERT(varchar(4), e.DISCOUNT_DAYS) + ' ' + 'NET' + ' ' + CONVERT(varchar(4), e.VENDOR_TERMS_DAYS)
END AS TERMS, a.ITEM_COST_RCVD * a.QTY_RCVD AS PO_Value, a.ITEM_COST_INV * a.QTY_RCVD AS AP_InvValue, f.DATE_VENDOR_INV, f.VENDOR_INV_NUMB,
CASE WHEN DATEPART(month, a.DATE_RCVD) < 4 THEN 1 WHEN DATEPART(month, a.DATE_RCVD) >= 4 AND DATEPART(month, a.DATE_RCVD)
< 7 THEN 2 WHEN DATEPART(month, a.DATE_RCVD) >= 7 AND DATEPART(month, a.DATE_RCVD) < 10 THEN 3 ELSE 4 END AS Quarter,
h.FOB_CODE
from podinrh a inner join incinv1 b on a.item_code=b.item_code
inner join pocpodf c on a.po_numb=c.po_numb and a.po_line_numb=c.po_line_numb
inner join pocvnms d on a.vendor_code=d.vendor_code
inner join podpovi e on a.vendor_code=e.vendor_code
inner join apvhhdr f on a.vendor_code=f.vendor_code
inner join PODPOIH h on a.PO_NUMB=h.PO_NUMB
inner join APVHDTL g on f.VOUCHER_NUMB=g.VOUCHER_NUMB and f.company_code=g.company_code
and a.PO_NUMB=g.PO_NUMB and a.PO_LINE_NUMB=g.PO_LINE_NUMB and a.GRN_NUMB=g.RECEIPT_NUMB and
a.GRN_LINE_NUMB=g.RECEIPT_LINE_NUMB
)
select top 100 * from cte
答案 0 :(得分:0)
可以做很多事情来提高查询的性能 - 通常使用索引。
如果没有看到有关数据库的更多信息,很难提供帮助。
但是,我确实看到了一件可以改变的事情......
CASE
WHEN DATEPART(month, a.DATE_RCVD) < 4 THEN 1 WHEN DATEPART(month, a.DATE_RCVD) >= 4 AND DATEPART(month, a.DATE_RCVD)
< 7 THEN 2
WHEN DATEPART(month, a.DATE_RCVD) >= 7 AND DATEPART(month, a.DATE_RCVD) < 10 THEN 3
ELSE 4
END AS Quarter,
应该是
DATEPART(quarter,a.DATE_RCVD) AS Quarter
此外,这看起来更好,但可能不会产生太大的影响:
CASE
WHEN VENDOR_TERMS_PERCENT != 0
THEN CONVERT(varchar(7), e.VENDOR_TERMS_PERCENT) + '% ' + CONVERT(varchar(4), e.DISCOUNT_DAYS) + ' '
ELSE ''
END + 'NET ' + CONVERT(varchar(4), e.VENDOR_TERMS_DAYS) AS TERMS,