Автор: Alexey Knyazev
Теперь мы можем ускорять выполнение SELECT INTO благодаря распараллеливанию.
Демонстрация:
SQL Server 2012
1 2 3 4 5 |
Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) Dec 28 2012 20:23:12 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) |
SQL Server 2014 CTP1
1 2 3 4 5 |
Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64) Jun 10 2013 20:09:10 Copyright (c) Microsoft Corporation Enterprise Evaluation Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) |
На обоих версиях создадим таблицу dbo.test_table и наполним тестовыми данными (65536 записей)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
set nocount on; go if object_id( N'dbo.test_table', N'U' ) is not null drop table dbo.test_table; go create table dbo.test_table ( id int primary key , dt datetime default getdate() , uniqid uniqueidentifier default newid() , val nvarchar(1024) default replicate( 'A', 1024 ) ); go with cte1 as ( select t1.* from ( values(1),(1) ) t1(i) ) , cte2 as ( select t2.* from cte1 t1 cross join cte1 t2 ) , cte3 as ( select t3.* from cte2 t2 cross join cte2 t3 ) , cte4 as ( select t4.* from cte3 t3 cross join cte3 t4 ) , cte5 as ( select t5.* from cte4 t4 cross join cte4 t5 ) insert into dbo.test_table ( id ) select row_number() over ( order by (select null) ) as id from cte5; go |
После этого мы «перекинем» все эти данные во временную таблицу с помощью инструкции SELECT…INTO
1 2 3 4 5 6 7 8 9 10 11 |
set statistics time on; set statistics io on; go checkpoint; dbcc dropcleanbuffers; go select * into #t from dbo.test_table; go |
И сравним планы выполнения:
SQL Server 2012
SQL Server 2014