怎么优化更新表的内容半天出不来的语句
如下语句执行12分钟才出来:
UPDATE YearEndDates SET AvgDiluteClose =
(
SELECT AVG(ISNULL(P.[Close], 0.0) * ISNULL(P.CumulativeDilute, 1.0))
FROM PricesDaily P WITH (NOLOCK)
WHERE P.ASXCode = YearEndDates.ASXCode
AND P.[Date] > ISNULL(YearEndDates.PrevDate,'1900-01-01')
AND P.[Date] <= YearEndDates.[Date]
)
表YearEndDates 有4万条记录,主键和索引是ASXCode, Date ;
表PricesDaily 有1600万条记录,主键和索引是ASXCode, Date;
有什么方法能优化执行快点吗?因为还有其他的字段要update.
update YearEndDates
set CumulativeDilute = P.CumulativeDilute,
ClosePrice = P.[Close]
from YearEndDates Y
inner join PricesMonthly P on Y.ASXCode = P.ASXCode
and datepart(year, Y.[Date]) = datepart(year, P.[Date])
and datepart(month, Y.[Date]) = datepart(month, P.[Date])
表PricesMonthly 有90万条记录,主键和索引是ASXCode, Date;
语句执行都是很慢的.
------解决方案--------------------试试这个,我觉得单从执行计划来看貌似没啥问题
UPDATE YearEndDates
SET YearEndDates.avgdiluteclose = AVG(ISNULL(p.[CLOSE], 0.0)
* ISNULL(p.cumulativedilute, 1.0))
FROM YearEndDates
INNER JOIN PricesDaily p ON p.ASXCODE = YearEndDates.ASXCODE
WHERE p.[date] > ISNULL(YearEndDates.prevdate, '1900-01-01')
AND p.[date] <= YearEndDates.[date]