我已經運行了一個執行計劃並注意到查詢在插入臨時表時需要時間。我們有多個查詢插入到臨時表中。我在下面分享了其中兩個。如何通過storedprocedure查詢將聚集索引添加到臨時表中。它需要動態創建索引,並摧毀它在臨時表上添加聚簇索引以提高性能
if object_id('tempdb..#MarketTbl') is not null drop table #MarketTbl else
select
mc.companyId,
mc.pricingDate,
mc.tev,
mc.sharesOutstanding,
mc.marketCap
into #MarketTbl
from ciqMarketCap mc
where mc.pricingDate > @date
and mc.companyId in (select val from @companyId)
---- pricing table: holds pricing data for the stock pprice
if object_id('tempdb..#PricingTbl') is not null drop table #PricingTbl else
select
s.companyId,
peq.pricingDate,
ti.currencyId,
peq.priceMid
into #PricingTbl
from ciqsecurity s
join ciqtradingitem ti on s.securityid = ti.securityid
join ciqpriceequity peq on peq.tradingitemid = ti.tradingitemid
where s.primaryFlag = 1
and s.companyId in (select val from @companyId)
and peq.pricingDate> @date
and ti.primaryflag = 1
你嘗試過CREATE INDEX嗎? –
[臨時表上的聚集索引]的可能重複(https://stackoverflow.com/questions/7663967/clustered-index-on-temp-table) –
您是否嘗試將臨時表或SELECT中的INSERT改進爲臨時表? –