开发者

3 different Insert into #table select in parallel SQL Server 2008

开发者 https://www.devze.com 2023-03-04 00:45 出处:网络
I have 1 temporary table and I am doing something like: Insert into #table1 select ...from #temporal Insert into #table2 select ...from #temporal

I have 1 temporary table and I am doing something like:

Insert into #table1 select ...  from #temporal
Insert into #table2 select ...  from #temporal
Insert into #table3 select ...  from #temporal

As every select take a conside开发者_如何学Pythonrable time I would like to paralelize these 3 queries is there any way to do this in SQL Server 2008?


It sounds like the root of the problem is that the SELECT from #temporal is a performance problem. Do you have an index on that temp table #temporal? Likely an index (or statistics update) would help you out to avoid writing more code to work around this problem.

Are you able, or have you tried measuring performance against storing the results from that one #temporal SELECT into a table variable?

DECLARE @myTemporal TABLE (id int, foo varchar(100))
INSERT INTO @myTemporal (id, foo)
    SELECT id, foo FROM #temporal;

Then your n INSERTs can pull from the table variable, rather than the expensive/nonperformant query.

Insert into #table1 select id, foo from @myTemporal;
Insert into #table2 select id, foo from @myTemporal;
Insert into #table3 select id, foo from @myTemporal;

The benefit is that you won't have to execute the SELECT 3x against your temp table. You'd be inserting into your 3 temp tables from the table variable. All rows, no WHERE clause.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号