提高SQL Server中的插入速度

时间:2017-10-10 15:49:42

标签: sql-server sql-server-2008

我正在尝试在SQL Server 2008中优化以下查询。执行计划显示此查询相对于批处理是90%。插入物显示75%。我正在寻找一个选项来进行批量插入,并发插入等。有人可以指望加快这个过程。我可以SqlBulkCopy或使用OpenRowset功能吗?我确保where条件

中的所有字段都有索引
INSERT INTO [xxxxxxx].[dbo].[xxxxxxx] WITH (Tablock)
    SELECT 
        s.companyId,
        ti.tickerSymbol,
        s.securityName, 
        ex.exchangeName,
        cur.currencyName, 
        primaryFlag = CASE 
                         WHEN ti.primaryFlag = 1 AND s.primaryFlag = 1  
                            THEN 1 
                         ELSE 0 
                      END,
        ti.tradingItemId,
        peq.pricingDate, peq.priceOpen,
        peq.priceHigh, peq.priceLow, peq.priceMid,
        peq.priceClose, peq.priceBid, peq.priceAsk,
        peq.volume, peq.adjustmentFactor, peq.VWAP,
        mc.marketCap, mc.TEV, mc.sharesOutstanding
    FROM
        ciqsecurity s 
    INNER JOIN
        CoreReferenceStaging.dbo.MarketDataTemp1 a ON a.companyId = s.companyId 
    INNER JOIN
        ciqtradingitem ti ON s.securityid = ti.securityid
    LEFT JOIN
        ciqpriceequity peq ON peq.tradingitemid = ti.tradingitemid
    LEFT JOIN 
        CoreReferenceStaging.dbo.MarketDataTemp2 mc ON (mc.companyId= s.companyId AND mc.pricingDate = peq.pricingDate)
    INNER JOIN
        ciqExchange ex ON ex.exchangeId = ti. exchangeId
    INNER JOIN
        ciqCurrency cur ON cur.currencyid = ti.currencyId
    ORDER BY
        peq.pricingDate DESC

我曾尝试将插件分批,但似乎需要更长的时间。例如以下

DECLARE @BatchSize int = 1000

WHILE 1 = 1
BEGIN

INSERT INTO [xxxxxx].[dbo].[xxxxxxx]  with (Tablock)
select 
    s.companyId,
    ti.tickerSymbol,
    s.securityName, 
    ex.exchangeName,
    cur.currencyName, 
    primaryFlag = case when ti.primaryFlag = 1 and s.primaryFlag = 1 then 1 else 0 end,
    ti.tradingItemId,
    peq.pricingDate,
    peq.priceOpen,
    peq.priceHigh,
    peq.priceLow,
    peq.priceMid,
    peq.priceClose, 
    peq.priceBid,
    peq.priceAsk,
    peq.volume,
    peq.adjustmentFactor,
    peq.VWAP,
    mc.marketCap,
    mc.TEV, 
    mc.sharesOutstanding
from ciqsecurity s 
    inner join CoreReferenceStaging.dbo.MarketDataTemp1 a on a.companyId = s.companyId 
    inner join ciqtradingitem ti on s.securityid = ti.securityid
    left join ciqpriceequity peq on peq.tradingitemid = ti.tradingitemid
    left join CoreReferenceStaging.dbo.MarketDataTemp2 mc on (mc.companyId= s.companyId and mc.pricingDate = peq.pricingDate)
    inner join ciqExchange ex on ex.exchangeId = ti. exchangeId
    inner join ciqCurrency cur on cur.currencyid = ti.currencyId
order by peq.pricingDate desc

IF @@ROWCOUNT < @BatchSize BREAK END

上设置统计信息的结果
Table 'ciqExchange'. Scan count 17, logical reads 67, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ciqCurrency'. Scan count 1, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'MarketDataTemp1'. Scan count 10, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ciqSecurity'. Scan count 500, logical reads 53703, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ciqTradingItem'. Scan count 17, logical reads 33082, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 11382, logical reads 1193682, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ciqPriceEquity'. Scan count 15681, logical reads 96425, physical reads 0, read-ahead reads 3, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'MarketDataTemp2'. Scan count 17, logical reads 1648, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

执行计划1

enter image description here

执行计划2

enter image description here

1 个答案:

答案 0 :(得分:0)

查询似乎调整得很好,似乎没有返工的关键点。 它只提取超过4千万到5千万条记录,只需11秒即可执行。在我看来,你不能提高查询... 您可以对索引碎片进行一些检查,以防重建它们但是你不会在5秒内完成...我认为查询没问题且没有瓶颈......