简体   繁体   中英

SQL Server: Why SELECT * INTO is faster than SELECT * (network excluded)

Why would Query2 dramatically outperform Query1?

Query1:

SELECT * FROM [SomeComplexView];

Takes 4 minutes, does not use parallelism in query plan

Query2:

SELECT * INTO #t1 FROM [SomeComplexView];
SELECT * FROM #t1;

Takes 15 seconds, uses parallelism. I guess the answer should be quite simple and generic, so I omit the complexities of the view.

http://dataeducation.com/next-level-parallel-plan-forcing-an-alternative-to-8649/

When the query optimizer decides whether or not to select a parallel plan, it first comes up with a serial plan cost, then compares it to the parallel plan cost. If the parallel plan cost is lower, that's the one that gets used. This parallel plan cost is based on the serial cost, but with a few key modifications:...

--estimated plans
--serial cost:399
--parallel cost:204 <---!
select a.*
into #abc
from master.dbo.spt_values as a
cross join master.dbo.spt_values as b
--option(maxdop 1) 
--option (maxdop 2)


--serial cost: 28 <---!
--parallelx2 cost:41
--parallelx4 cost:20 
--parallelx8 cost:10
select a.*
from master.dbo.spt_values as a
cross join master.dbo.spt_values as b
--option (maxdop 1)
--option (querytraceon 8649, maxdop 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM