UPDATE IT
SET
IT.RetailStock=BS.RetailStock
,IT.RemainOrderQuantity=BS.RemainOrderQuantity
,IT.AllocateQuantity=BS.AllocateQuantity
,IT.SaleOneQuantity=BS.SaleOneQuantity
,IT.BranchAdjustQuantity=BS.BranchAdjustQuantity
,IT.ProdName=PT.ProdName
,IT.vardesc1=SPS.vardesc1
,IT.vardesc2=SPS.vardesc2
,IT.purchunits=PRS.purchunits
,IT.RetailPrice=PRS.RetailPrice
,IT.minorder=PRS.minorder
,IT.SusprRason=PRS.SusprRason
,IT.supplier=SU.supplier
,IT.[name]=SU.[name]
,IT.CatCode=TN.CatCode
FROM #IN_TABLE_S IT
INNER JOIN sql_live.dbo.productcodes SPS with(nolock)
ON IT.PROD_CD=SPS.variantcode
INNER JOIN sql_live.dbo.products PT with(nolock)
ON SPS.prodint = PT.prodint
INNER JOIN
(SELECT
BCH.branchcode,CS.coverint
FROM
sql_live.dbo.branches BCH with(nolock)
INNER JOIN sql_live.dbo.coverages CS with(nolock)
ON
BCH.levint =CS.levint
and
CS.typecode=1
WHERE EXISTS
(SELECT 1 FROM #Branch BR
WHERE BCH.branchcode =BR.BranchCD)
) CS
ON CS.branchcode=IT.BRANCHCode
INNER JOIN sql_live.dbo.PRSourcePrefs PRS with(nolock)
ON CS.coverint = PRS.plint
AND PRS.varint = SPS.varint
INNER JOIN EOB.dbo.CAOBuMonCatcode TN with(nolock)
ON PT.level2 = TN.BuMonCD
LEFT JOIN
#BranchStocks BS
ON IT.branchcode=BS.BRANCHCode
AND IT.PROD_CD=BS.ProductCode
LEFT JOIN sql_live.dbo.suppliers SU with(nolock)
ON PRS.supplier = SU.supplier
WHERE
EXISTS
(SELECT 1 FROM #Branch BR
WHERE IT.branchcode =BR.BranchCD)
'+@Conn_CatCode_SUP)
请大家帮我优化一下上面的update操作,谢谢!
其实你的本质想法已经在操作移动到cs上来做了。
嗯,作为sql优化,你可以把握几点
1.小表join大表
2.折分子句,以免重复连接。
提示:你可以先在纸上或者design IDE中画出/了解表与表之间的关系。这样的话,你的理解应该可以更清楚。
3.sp执行优于select方式
4.最后还是要明确一下临时表和表变量之间。你应该选择哪个?
好长,看不明白~~
还不如把需求说出来让大家帮你写一个呢
new 一个是新的啊。并且你又调用了存储过程。你可以用缓存,当然数据会对存储过程的执行计划缓存的。