Home
%3CLINGO-SUB%20id%3D%22lingo-sub-390542%22%20slang%3D%22en-US%22%3ERe%3A%20Reduced%20recompilations%20for%20workloads%20using%20temporary%20tables%20across%20multiple%20scopes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-390542%22%20slang%3D%22en-US%22%3E%3CP%3E%3CFONT%20color%3D%22%230000ff%22%20size%3D%224%22%3E%3CSTRONG%3EExcellent%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-388391%22%20slang%3D%22en-US%22%3EReduced%20recompilations%20for%20workloads%20using%20temporary%20tables%20across%20multiple%20scopes%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-388391%22%20slang%3D%22en-US%22%3E%3CP%3ESQL%20Server%202019%20introduces%20several%20performance%20optimizations%20which%20will%20improve%20performance%20with%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fperformance%2Fintelligent-query-processing%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eminimal%20changes%20required%20to%20your%20application%20code%3C%2FA%3E.%26nbsp%3B%20%26nbsp%3BIn%20this%20blog%20post%20we%E2%80%99ll%20discuss%20one%20such%20improvement%20available%20in%20CTP%202.3%3A%20%3CSTRONG%3Ereduced%20recompilations%20for%20workloads%20using%20temporary%20tables%20in%20multiple%20scopes%3C%2FSTRONG%3E.%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20understand%20this%20improvement%2C%20we%E2%80%99ll%20first%20go%20over%20the%20current%20behavior%20prior%20to%20SQL%20Server%202019.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20referencing%20a%20temporary%20table%20with%20a%20DML%20statement%20(SELECT%2C%20INSERT%2C%20UPDATE%2C%20DELETE)%2C%20if%20the%20temporary%20table%20was%20created%20by%20an%20outer%20scope%20batch%2C%20we%20will%20recompile%20the%20DML%20statement%20%3CEM%3Eeach%20time%20it%20is%20executed%3C%2FEM%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20following%20example%20illustrates%20this%20behavior%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20457px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F99991i1937FCF6DA1A1AF4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221_RR.png%22%20title%3D%221_RR.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIn%20the%20outer%20procedure%20we%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ECreate%20a%20temporary%20table%3C%2FLI%3E%0A%3CLI%3ECall%20a%20stored%20procedure%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EThe%20inner%20stored%20procedure%20definition%20is%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20689px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F99992iD24AABE02705308D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222_RR.png%22%20title%3D%222_RR.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EFor%20the%20inner%20stored%20procedure%2C%20we%20have%20two%20DML%20statements%20that%20reference%20the%20temporary%20table%20created%20in%20the%20outer%20scope%20where%20we%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EInsert%20a%20row%20into%20the%20temporary%20table.%3C%2FLI%3E%0A%3CLI%3EReturn%20the%20row%20from%20the%20temporary%20table.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EWe%20created%20the%20temporary%20table%20in%20a%20different%20scope%20from%20the%20DML%20statements%2C%20and%20for%20the%20existing%20implementation%20(pre-SQL%20Server%202019)%2C%20we%20don%E2%80%99t%20%E2%80%9Ctrust%E2%80%9D%20that%20this%20temporary%20table%20schema%20hasn%E2%80%99t%20been%20materially%20changed%20and%20so%20we%20recompile%20the%20associated%20DML%20statements%20%3CEM%3Eeach%20time%20they%20are%20executed%3C%2FEM%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20additional%20recompilation%20activity%20increases%20CPU%20utilization%20and%20can%20decrease%20overall%20workload%20performance%20and%20throughput.%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3EStarting%20with%20SQL%20Server%202019%20CTP%202.3%2C%20we%20will%20perform%20additional%20lightweight%20checks%20to%20avoid%20unnecessary%20recompilations%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EWe%20will%20check%20if%20the%20outer-scope%20module%20used%20for%20creating%20the%20temporary%20table%20at%20compile%20time%20is%20the%20same%20one%20used%20for%20consecutive%20executions.%3C%2FLI%3E%0A%3CLI%3EWe%20will%20keep%20track%20of%20any%20data%20definition%20language%20(DDL)%20changes%20made%20at%20initial%20compilation%20and%20compare%20them%20with%20DDL%20operations%20for%20consecutive%20executions.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThe%20end%20result%20is%20a%20reduction%20in%20unwarranted%20recompilations%20and%20associated%20CPU-overhead.%3C%2FP%3E%0A%3CP%3EThe%20below%20figure%20shows%20test%20results%20from%2016%20concurrent%20threads%20each%20executing%20the%20%E2%80%9COutProc%E2%80%9D%20stored%20procedure%201%2C000%20times%20(in%20a%20loop).%20The%20Y-axis%20represents%20the%20number%20of%20occurrences%2C%20with%20the%20blue%20line%20representing%20%3CSTRONG%3EBatch%20Requests%2Fsec%3C%2FSTRONG%3E%20and%20the%20green%20line%20representing%20%3CSTRONG%3ESQL%20Re-Compilations%2Fsec%3C%2FSTRONG%3E%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F99993iDE75815664329F4E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%223_RR.png%22%20title%3D%223_RR.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EWhen%20the%20feature%20was%20enabled%20(%E2%80%9Cafter%E2%80%9D)%2C%20for%20this%20example%20we%20saw%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EImproved%20throughput%2C%20as%20represented%20by%20Batch%20Requests%2Fsec%20(blue%20line).%3C%2FLI%3E%0A%3CLI%3EShorter%20overall%20workload%20duration.%3C%2FLI%3E%0A%3CLI%3EMinimal%20recompilations%2C%20as%20represented%20by%20SQL%20Re-Compilations%2Fsec%20(green%20line)%20showing%20only%20a%20small%20increase%20at%20the%20beginning%20of%20the%20second%20test.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThis%20feature%20is%20enabled%20by%20default%20in%20SQL%20Server%202019%20CTP%202.3%20under%20all%20database%20compatibility%20levels.%20As%20of%20this%20writing%2C%20this%20feature%20is%20also%20enabled%20in%20Azure%20SQL%20Database%20under%20database%20compatibility%20level%20150%2C%20although%20it%20will%20soon%20be%20applied%20across%20all%20database%20compatibility%20levels%20(matching%20the%20behavior%20of%20SQL%20Server%202019).%3C%2FP%3E%0A%3CP%3EIf%20you%20have%20feedback%20on%20this%20feature%20or%20other%20query%20processing%20features%2C%20please%20email%20us%20at%26nbsp%3B%3CA%20href%3D%22mailto%3AIntelligentQP%40microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EIntelligentQP%40microsoft.com%3C%2FA%3E.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-388391%22%20slang%3D%22en-US%22%3E%3CP%3ESQL%20Server%202019%20introduces%20several%20performance%20optimizations%20which%20will%20improve%20performance%20with%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fperformance%2Fintelligent-query-processing%3Fview%3Dsql-server-2017%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Eminimal%20changes%20required%20to%20your%20application%20code%3C%2FA%3E.%26nbsp%3B%20%26nbsp%3BIn%20this%20blog%20post%20we%E2%80%99ll%20discuss%20one%20such%20improvement%20available%20in%20CTP%202.3%3A%20%3CSTRONG%3Ereduced%20recompilations%20for%20workloads%20using%20temporary%20tables%20in%20multiple%20scopes%3C%2FSTRONG%3E.%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

SQL Server 2019 introduces several performance optimizations which will improve performance with minimal changes required to your application code.   In this blog post we’ll discuss one such improvement available in CTP 2.3: reduced recompilations for workloads using temporary tables in multiple scopes.   

In order to understand this improvement, we’ll first go over the current behavior prior to SQL Server 2019. 

When referencing a temporary table with a DML statement (SELECT, INSERT, UPDATE, DELETE), if the temporary table was created by an outer scope batch, we will recompile the DML statement each time it is executed

The following example illustrates this behavior:

1_RR.png

In the outer procedure we:

  1. Create a temporary table
  2. Call a stored procedure

The inner stored procedure definition is as follows:

2_RR.png

For the inner stored procedure, we have two DML statements that reference the temporary table created in the outer scope where we:

  1. Insert a row into the temporary table.
  2. Return the row from the temporary table.

We created the temporary table in a different scope from the DML statements, and for the existing implementation (pre-SQL Server 2019), we don’t “trust” that this temporary table schema hasn’t been materially changed and so we recompile the associated DML statements each time they are executed

This additional recompilation activity increases CPU utilization and can decrease overall workload performance and throughput.  

Starting with SQL Server 2019 CTP 2.3, we will perform additional lightweight checks to avoid unnecessary recompilations:

  • We will check if the outer-scope module used for creating the temporary table at compile time is the same one used for consecutive executions.
  • We will keep track of any data definition language (DDL) changes made at initial compilation and compare them with DDL operations for consecutive executions.

The end result is a reduction in unwarranted recompilations and associated CPU-overhead.

The below figure shows test results from 16 concurrent threads each executing the “OutProc” stored procedure 1,000 times (in a loop). The Y-axis represents the number of occurrences, with the blue line representing Batch Requests/sec and the green line representing SQL Re-Compilations/sec:

3_RR.png

When the feature was enabled (“after”), for this example we saw:

  • Improved throughput, as represented by Batch Requests/sec (blue line).
  • Shorter overall workload duration.
  • Minimal recompilations, as represented by SQL Re-Compilations/sec (green line) showing only a small increase at the beginning of the second test.

This feature is enabled by default in SQL Server 2019 CTP 2.3 under all database compatibility levels. As of this writing, this feature is also enabled in Azure SQL Database under database compatibility level 150, although it will soon be applied across all database compatibility levels (matching the behavior of SQL Server 2019).

If you have feedback on this feature or other query processing features, please email us at IntelligentQP@microsoft.com

1 Comment
Contributor

Excellent