SQL Server 2014 – SELECT INTO Statement

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.

SELECT INTO Statement

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.

select-into-statement2

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

   

About Prince Rastogi

Prince Rastogi is working as Database Administrator at Elephant Insurance, Richmond. He is having more than 8 years of experience and worked in ERP Domain, Wealth Management Domain. Currently he is working in Insurance domain. In the starting of his career he was working on SQL Server, Internet Information Server and Visual Source Safe. He is post graduate in Computer Science. Prince is ITIL certified professional. Prince likes to explore technical things for Database World and Writing Blogs. He is Technical Editor and Blogger at SQLServerGeeks.com. He is a regular speaker at DataPlatformDay events in Delhi NCR. He has also presented some in depth sessions about SQL Server in SQL Server Conferences in Bangalore.

View all posts by Prince Rastogi →

Leave a Reply

Your email address will not be published.