%3CLINGO-SUB%20id%3D%22lingo-sub-305305%22%20slang%3D%22en-US%22%3ESQLSweet16!%2C%20Episode%203%3A%20Parallel%20INSERT%20%E2%80%A6%20SELECT%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-305305%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jul%2006%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CI%3E%20Authored%20by%20Sanjay%20Mishra%20%3C%2FI%3E%3C%2FP%3E%3CBR%20%2F%3E%20%3CEM%3ESanjay%20Mishra%2C%20with%20Arvind%20Shyamsundar%20%3C%2FEM%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CEM%3EReviewed%20By%3A%20Sunil%20Agarwal%2C%20Denzil%20Ribeiro%2C%20Mike%20Ruthruff%2C%20Mike%20Weiner%20%3C%2FEM%3E%3CBR%20%2F%3E%3CBR%20%2F%3ELoading%20large%20amounts%20of%20data%20from%20one%20table%20to%20another%20is%20a%20common%20task%20in%20many%20applications.%20Over%20the%20years%2C%20there%20have%20been%20%3CA%20href%3D%22https%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fdd425070%2528v%3Dsql.100%2529.aspx%3Ff%3D255%26amp%3BMSPPError%3D-2147217396%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eseveral%20techniques%20%3C%2FA%3Eto%20improve%20the%20performance%20of%20the%20data%20loading%20operations.%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fbb510411(v%3Dsql.120).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQL%20Server%202014%20allowed%20parallelism%20%3C%2FA%3Efor%20SELECT%20%E2%80%A6%20INTO%20operations.%20However%2C%20the%20users%20needed%20more%20flexibility%2C%20in%20terms%20of%20placement%20of%20the%20target%20table%2C%20existing%20data%20in%20the%20target%20table%2C%20etc.%2C%20which%20are%20not%20possible%20with%20the%20SELECT%20%E2%80%A6%20INTO%20statement.%20Loading%20data%20into%20an%20existing%20table%20(with%20or%20without%20existing%20data)%20through%20an%20INSERT%20%E2%80%A6%20SELECT%20statement%20has%20been%20a%20serial%20operation.%20Until%20SQL%20Server%202016.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20SQL%20Server%202016%2C%20under%20certain%20conditions%2C%20allows%20an%20INSERT%20%E2%80%A6%20SELECT%20statement%20to%20operate%20in%20parallel%2C%20thereby%20significantly%20reducing%20the%20data%20loading%20time%20for%20these%20applications.%20A%20hidden%20gem!%20%3CBR%20%2F%3E%3CBR%20%2F%3EFigure%201%20illustrates%20loading%20time%20with%20and%20without%20parallelism.%20The%20test%20was%20performed%20on%20an%208-core%20machine%20(Figure%203%20shows%20the%20degree%20of%20parallelism%20achieved)%2C%20on%20a%20table%20with%2050%20million%20rows.%20Your%20mileage%20will%20vary.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcaption%20id%3D%22attachment_2985%22%20align%3D%22alignnone%22%20width%3D%22827%22%5D%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F63531iF13F8DF845DFB62A%22%20%2F%3EFigure%201%3A%20INSERT%20...%20SELECT%20with%20and%20without%20parallelism%20(SQL%20Server%202016)%5B%2Fcaption%5D%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-1673363169%22%20id%3D%22toc-hId-1846833461%22%20id%3D%22toc-hId-1846833461%22%3EImportant%20to%20Know%3C%2FH2%3E%3CBR%20%2F%3E%20Two%20important%20criteria%20must%20be%20met%20to%20allow%20parallel%20execution%20of%20an%20INSERT%20%E2%80%A6%20SELECT%20statement.%20%3CBR%20%2F%3E%3COL%3E%3CBR%20%2F%3E%3CLI%3EThe%20database%20compatibility%20level%20must%20be%20130.%20Execute%20%E2%80%9CSELECT%20name%2C%20compatibility_level%20FROM%20sys.databases%E2%80%9D%20to%20determine%20the%20compability%20level%20of%20your%20database%2C%20and%20if%20it%20is%20not%20130%2C%20execute%20%E2%80%9CALTER%20DATABASE%20%3CMYDB%3E%20SET%20COMPATIBILITY_LEVEL%20%3D%20130%E2%80%9D%20to%20set%20it%20to%20130.%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fbb510680.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Changing%20the%20compatibility%20level%20of%20a%20database%20influences%20some%20behavior%20changes.%20%3C%2FA%3E%20You%20should%20test%20and%20ensure%20that%20your%20overall%20application%20works%20well%20with%20the%20new%20compatibility%20level.%3C%2FMYDB%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EMust%20use%20the%20TABLOCK%20hint%20with%20the%20INSERT%20%E2%80%A6%20SELECT%20statement.%20For%20example%3A%20INSERT%20INTO%20table_1%20%3CSTRONG%3EWITH%20(TABLOCK)%20%3C%2FSTRONG%3ESELECT%20*%20FROM%20table_2.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FOL%3E%3CBR%20%2F%3E%20There%20are%20a%20few%20restrictions%20under%20which%20parallel%20insert%20is%20disabled%2C%20even%20when%20the%20above%20requirements%20are%20met.%20We%20will%20cover%20the%20restrictions%2C%20and%20work%20arounds%2C%20if%20applicable%2C%20in%20a%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsqlcat%2F2016%2F07%2F21%2Freal-world-parallel-insert-what-else-you-need-to-know%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eseparate%20blog%20post%20%3C%2FA%3E.%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--878793792%22%20id%3D%22toc-hId--705323500%22%20id%3D%22toc-hId--705323500%22%3EHow%20to%20know%20you%20are%20getting%20parallelism%3C%2FH2%3E%3CBR%20%2F%3E%20The%20simplest%20way%20to%20check%20for%20parallelism%20is%20the%20execution%20plan.%20Figure%202%20shows%20an%20execution%20plan%20for%20an%20INSERT%20%E2%80%A6%20SELECT%20statement%20without%20parallelism.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcaption%20id%3D%22attachment_2986%22%20align%3D%22alignnone%22%20width%3D%22975%22%5D%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F63532iDB2A39E6100BF3C1%22%20%2F%3EFigure%202%3A%20INSERT%20...%20SELECT%20execution%20plan%20without%20parallelism%5B%2Fcaption%5D%20%3CBR%20%2F%3E%3CBR%20%2F%3EUnder%20appropriate%20conditions%2C%20the%20same%20statement%20can%20use%20parallelism%2C%20as%20shown%20in%20Figure%203.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcaption%20id%3D%22attachment_2976%22%20align%3D%22alignnone%22%20width%3D%221000%22%5D%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F63533i284D393F62C13FE8%22%20%2F%3EFigure%203%3A%20INSERT%20...%20SELECT%20execution%20plan%20with%20parallelism%5B%2Fcaption%5D%20%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-864016543%22%20id%3D%22toc-hId-1037486835%22%20id%3D%22toc-hId-1037486835%22%3ECall%20to%20Action%3C%2FH2%3E%3CBR%20%2F%3E%20Tell%20us%20about%20your%20application%20scenarios%20where%20you%20plan%20to%20use%20this%20functionality.%20Also%20if%20you%20are%20already%20taking%20advantage%20of%20this%20hidden%20gem%2C%20share%20some%20data%20points%2C%20such%20as%20data%20volume%2C%20load%20time%2C%20the%20benefits%20you%20are%20seeing%20from%20parallelism%2C%20etc.%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-305305%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jul%2006%2C%202016%20Authored%20by%20Sanjay%20MishraSanjay%20Mishra%2C%20with%20Arvind%20ShyamsundarReviewed%20By%3A%20Sunil%20Agarwal%2C%20Denzil%20Ribeiro%2C%20Mike%20Ruthruff%2C%20Mike%20WeinerLoading%20large%20amounts%20of%20data%20from%20one%20table%20to%20another%20is%20a%20common%20task%20in%20many%20applications.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-305305%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edata%20loading%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eparallel%20insert%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eparallelism%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESQL%20Server%202016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Esqlsweet16%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Esweet%2016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Etablock%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1459756%22%20slang%3D%22en-US%22%3ERe%3A%20SQLSweet16!%2C%20Episode%203%3A%20Parallel%20INSERT%20%26amp%3B%238230%3B%20SELECT%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1459756%22%20slang%3D%22en-US%22%3E%3CP%3ECan%20we%20do%20this%20on%20the%20secondary%20replica%20where%20we%20are%20inserting%20data%20into%20a%20temporary%20table%20as%20part%20of%20a%20transaction%3F%20Our%20processes%20takes%20a%20lot%20of%20time%20to%20dump%20the%20data%20into%20temporary%20table%20as%20it%20is%20selected%20from%20complex%20joins%20done%20on%20the%20tables%20in%20the%20AG%20database%20on%20the%20secondary%20replica.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft
First published on MSDN on Jul 06, 2016

Authored by Sanjay Mishra


Sanjay Mishra, with Arvind Shyamsundar

Reviewed By: Sunil Agarwal, Denzil Ribeiro, Mike Ruthruff, Mike Weiner

Loading large amounts of data from one table to another is a common task in many applications. Over the years, there have been several techniques to improve the performance of the data loading operations. SQL Server 2014 allowed parallelism for SELECT … INTO operations. However, the users needed more flexibility, in terms of placement of the target table, existing data in the target table, etc., which are not possible with the SELECT … INTO statement. Loading data into an existing table (with or without existing data) through an INSERT … SELECT statement has been a serial operation. Until SQL Server 2016.

SQL Server 2016, under certain conditions, allows an INSERT … SELECT statement to operate in parallel, thereby significantly reducing the data loading time for these applications. A hidden gem!

Figure 1 illustrates loading time with and without parallelism. The test was performed on an 8-core machine (Figure 3 shows the degree of parallelism achieved), on a table with 50 million rows. Your mileage will vary.

[caption id="attachment_2985" align="alignnone" width="827"] Figure 1: INSERT ... SELECT with and without parallelism (SQL Server 2016)[/caption]

Important to Know


Two important criteria must be met to allow parallel execution of an INSERT … SELECT statement.

  1. The database compatibility level must be 130. Execute “SELECT name, compatibility_level FROM sys.databases” to determine the compability level of your database, and if it is not 130, execute “ALTER DATABASE <MyDB> SET COMPATIBILITY_LEVEL = 130” to set it to 130. Changing the compatibility level of a database influences some behavior changes. You should test and ensure that your overall application works well with the new compatibility level.

  2. Must use the TABLOCK hint with the INSERT … SELECT statement. For example: INSERT INTO table_1 WITH (TABLOCK) SELECT * FROM table_2.


There are a few restrictions under which parallel insert is disabled, even when the above requirements are met. We will cover the restrictions, and work arounds, if applicable, in a separate blog post .

How to know you are getting parallelism


The simplest way to check for parallelism is the execution plan. Figure 2 shows an execution plan for an INSERT … SELECT statement without parallelism.

[caption id="attachment_2986" align="alignnone" width="975"] Figure 2: INSERT ... SELECT execution plan without parallelism[/caption]

Under appropriate conditions, the same statement can use parallelism, as shown in Figure 3.

[caption id="attachment_2976" align="alignnone" width="1000"] Figure 3: INSERT ... SELECT execution plan with parallelism[/caption]

Call to Action


Tell us about your application scenarios where you plan to use this functionality. Also if you are already taking advantage of this hidden gem, share some data points, such as data volume, load time, the benefits you are seeing from parallelism, etc.
1 Comment
Regular Visitor

Can we do this on the secondary replica where we are inserting data into a temporary table as part of a transaction? Our processes takes a lot of time to dump the data into temporary table as it is selected from complex joins done on the tables in the AG database on the secondary replica.