Hi Friends,
Microsoft introduced so many new features and improvements with SQL Server 2014. In addition to these features, one of the improvement provided in SQL Server 2014 is about SELECT INTO statement. The SELECT INTO generally used to copy the data from one table and inserts that data into a new table.
Before SQL Server 2014, SELECT INTO statement was single threaded operation and take long time. From SQL Server 2014 onward, SELECT INTO is multi-threaded operation and gives is much faster processing. Here, multi-threaded means this operation can execute using parallelism. Here, we will test the processing of SELECT INTO statement for both the cases.
Here, I am using AdventureWorksDW database with SQL Server 2014. To check the execution behaviour of SELECT INTO statement on SQL Server 2012, I’ll change the compatibility level of the database to SQL Server 2012 as shown below:
USE [master] GO ALTER DATABASE [AdventureWorksDW2014] SET COMPATIBILITY_LEVEL = 110 GO
Execute below SELECT INTO statement:
USE [AdventureWorksDW2014] GO SET STATISTICS TIME ON GO SELECT [ProductKey] ,[OrderDateKey] ,[DueDateKey] ,[ShipDateKey] ,[CustomerKey] ,[PromotionKey] ,[CurrencyKey] ,[SalesTerritoryKey] ,[SalesOrderNumber] ,[SalesOrderLineNumber] ,[RevisionNumber] ,[OrderQuantity] ,[UnitPrice] ,[ExtendedAmount] ,[UnitPriceDiscountPct] ,[DiscountAmount] ,[ProductStandardCost] ,[TotalProductCost] ,[SalesAmount] ,[TaxAmt] ,[CarrierTrackingNumber] ,[CustomerPONumber] INTO [AdventureWorksDW2014].[dbo].[FactInternetSalesTest1] FROM [AdventureWorksDW2014].[dbo].[FactInternetSales] GO SET STATISTICS TIME OFF GO
Execution Time Details are mention below:
SQL Server Execution Times:
CPU time = 235 ms, elapsed time = 246 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
Now try to change the compatibility level to 120, which is for SQL Server 2014:
USE [master] GO ALTER DATABASE [AdventureWorksDW2014] SET COMPATIBILITY_LEVEL = 120 GO
Execute below SELECT INTO statement:
USE [AdventureWorksDW2014] GO SET STATISTICS TIME ON GO SELECT [ProductKey] ,[OrderDateKey] ,[DueDateKey] ,[ShipDateKey] ,[CustomerKey] ,[PromotionKey] ,[CurrencyKey] ,[SalesTerritoryKey] ,[SalesOrderNumber] ,[SalesOrderLineNumber] ,[RevisionNumber] ,[OrderQuantity] ,[UnitPrice] ,[ExtendedAmount] ,[UnitPriceDiscountPct] ,[DiscountAmount] ,[ProductStandardCost] ,[TotalProductCost] ,[SalesAmount] ,[TaxAmt] ,[CarrierTrackingNumber] ,[CustomerPONumber] INTO [AdventureWorksDW2014].[dbo].[FactInternetSalesTest2] FROM [AdventureWorksDW2014].[dbo].[FactInternetSales] GO SET STATISTICS TIME OFF GO
Execution Time Details are mention below:
SQL Server Execution Times:
CPU time = 171 ms, elapsed time = 74 ms.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
The changes those you can notice here are:
- SQL Server 2014, started using parallelism for SELECT INTO execution.
- Execution time is much faster with parallelism compare to single threaded operation.
HAPPY LEARNING!
Thanks & Regards:
Prince Rastogi
Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook
Follow Prince Rastogi on Twitter | Follow Prince Rastogi on FaceBook