From adaptive to intelligent: query processing in SQL Server 2019

Slides:



Advertisements
Similar presentations
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Advertisements

Using the Optimizer to Generate an Effective Regression Suite: A First Step Murali M. Krishna Presented by Harumi Kuno HP.
SQL Performance 2011/12 Joe Chang, SolidQ
Dos and don’ts of Columnstore indexes The basis of xVelocity in-memory technology What’s it all about The compression methods (RLE / Dictionary encoding)
10 Things Not To Do With SQL SQLBits 7. Some things you shouldn’t do.
DB performance tuning using indexes Section 8.5 and Chapters 20 (Raghu)
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Tempdb Parasites Jason Hall-Dir. of Client SQL Sentry Blog-jasonhall.blogs.sqlsentry.net.
SQL Server 2005 SP2 Israeli SQL Server User Group March 2005 Ami Levin
Query Processing Presented by Aung S. Win.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Identifying Reversible Functions From an ROBDD Adam MacDonald.
1 Robert Wijnbelt Health Check your Database A Performance Tuning Methodology.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Database Management 9. course. Execution of queries.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Applications hitting a wall today with SQL Server Locking/Latching Scale-up Throughput or latency SLA Applications which do not use SQL Server.
Around the world (of query plan operators) in 50 minutes David Morrison BI Consultant.
M.Kersten MonetDB, Cracking and recycling Martin Kersten CWI Amsterdam.
MISSION CRITICAL COMPUTING Siebel Database Considerations.
The Instruction Set Architecture. Hardware – Software boundary Java Program C Program Ada Program Compiler Instruction Set Architecture Microcode Hardware.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
TOP 10 Thinks you shouldn’t do with/in your database
LECTURE 12 Virtual Memory. VIRTUAL MEMORY Just as a cache can provide fast, easy access to recently-used code and data, main memory acts as a “cache”
Eugene Meidinger Execution Plans
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
High Performance Functions SQLBits VI. Going backwards is faster than going forwards.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
A deep dive into SQL Server Plan Cache Management.
Closing the Query Processing Loop in Oracle 11g Allison Lee, Mohamed Zait.
Session Name Pelin ATICI SQL Premier Field Engineer.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Deep dive on four SQL Server 2017 Features
Joe Sack, Principal Program Manager, Microsoft
In-Memory Capabilities
Tuning Transact-SQL Queries
Query Tuning without Production Data
Query Tuning without Production Data
Query Tuning without Production Data
Four Rules For Columnstore Query Performance
Let Me Finish... Isolating Write Operations
Blazing-Fast Performance:
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
Decoding the Cardinality Estimator to Speed Up Queries
SQL Server 2017 has more cool features than just running on Linux
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
SQL 2014 In-Memory OLTP What, Why, and How
Top Tips for Better TSQL Stored Procedures
Execution Plans Demystified
Deep Dive into Adaptive Query Processing
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
Transact SQL Performance Tips
Introduction to reading execution plans
Four Rules For Columnstore Query Performance
Introduction to Execution Plans
Let Me Finish... Isolating Write Operations
Evaluation of Relational Operations: Other Techniques
Diving into Query Execution Plans
Introduction to Execution Plans
Introduction to Execution Plans
Improving the Performance of Functions
Automagic Tuning - SQL Server 2019 and Beyond
From adaptive to intelligent:
Presentation transcript:

From adaptive to intelligent: query processing in SQL Server 2019 Hugo Kornelis From adaptive to intelligent: query processing in SQL Server 2019

Hugo Kornelis I make SQL Server Fast I make SQLServerFast.com Execution Plan Reference: http://sqlserverfast.com/epr Detailed description of all operators, and other relevant information Other content Blog: http://sqlserverfast.com/blog Articles Longer, even more detailed in-depth information Resources Deck and demo for this session (and others)

Hugo Kornelis I make SQLServerFast.com I do other community things I work (consulting, training) Contact details Email: hugo@perFact.info Twitter: @Hugo_Kornelis https://sqlserverfast.com/presentations/from-adaptive-to-intelligent-query-processing-in-sql-2019/

Query Processing Query optimization Query

Query Processing Query optimization Query execution Query Cardinality estimates Reality

Adaptive Query Processing SQL Server 2017

Adaptive Query Processing Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/intelligent-query-processing?view=sql-server-2017

Adaptive Intelligent Query Processing SQL Server 2019

Intelligent Query Processing Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/intelligent-query-processing?view=sql-server-2017

Memory Grant Feedback Memory Grant: memory required to execute plan Determined during compile time Based on cardinality estimates So … can be wrong

Memory Grant Feedback Memory Grant: memory required to execute plan Determined during compile time Under-estimated? Spills to tempdb! Can be very slow!

Memory Grant Feedback Memory Grant: memory required to execute plan Determined during compile time Under-estimated? Spills to tempdb! Over-estimated? Resources wasted! Impacts concurrency

Memory Grant Feedback Memory Grant: memory required to execute plan Determined during compile time Under-estimated? Spills to tempdb! Over-estimated? Resources wasted! Memory Grant Feedback Compilation and execution as normal Cached plan updated after execution

Memory Grant Feedback Memory Grant Feedback: Details Adjusts down if >50% unused Adjusts up after any spill Based on last execution only Can “chase own tail” in alternating patterns Increases thresholds after X consecutive adaptations Stops adapting after Y consecutive adaptations

Memory Grant Feedback Memory Grant Feedback: Details Adjusts down if >50% unused Adjusts up after any spill Stores last execution only Information stored in plan cache Forgotten when plan recompiles

Memory Grant Feedback Memory Grant Feedback: Details Adjusts down if >50% unused Adjusts up after any spill Stores last execution only Information stored in plan cache Extended events available spilling_report_to_memory_grant_feedback memory_grant_updated_by_feedback memory_grant_feedback_loop_disabled (debug channel)

Intelligent Query Processing Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/intelligent-query-processing?view=sql-server-2017

Table Variable Deferred Compilation

Table Variable Deferred Compilation

Table Variable Deferred Compilation

Table Variable Deferred Compilation 5944 ∗0.3 -- (inequality)

Table Variable Deferred Compilation 5944 -- (equality)

Intelligent Query Processing Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/intelligent-query-processing?view=sql-server-2017

 ?  ? Batch Mode on Rowstore Traditional processing (row mode) GetNext() GetNext()  ? GetNext()  ? GetNext()

? Batch Mode on Rowstore Batch mode processing GetNext() GetNext()              

Batch Mode on Rowstore Batch mode processing Introduced in SQL Server 2012 Benefits analytic operations (join, aggregation, window functions) Benefits processing of large data collections Overhead / startup cost Requires columnstore index on at least one table in query Trickery to get batch mode without columnstore index

Batch Mode on Rowstore Batch mode on rowstore New in SQL Server 2019 Batch mode without columnstore index – no trickery! “Interesting” tables used? CTP 2.2: >= 131,702 rows “Interesting” operations used? CTP 2.2: join, aggregation, windows aggregate with >= 131,702 rows Compare estimated cost No non-supported features

Batch Mode on Rowstore Batch mode on rowstore New in SQL Server 2019 Batch mode without columnstore index – no trickery! Limitations In-memory tables and indexes Can only be read in row mode, rest of plan can still use batch mode LOB data, XML, spatial, full-text search, cursors No batch mode at all

Batch Mode on Rowstore Batch mode on rowstore New in SQL Server 2019 Batch mode without columnstore index – no trickery! Limitations Sometimes batch mode can actually be slower than row mode Many of these cases will improve before RTM But … do test and monitor your workloads!

Intelligent Query Processing Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/intelligent-query-processing?view=sql-server-2017

Scalar UDF Inlining Scalar user-defined functions Good for development Code encapsulation Code reuse Bad for performance Executed once per row Optimizer crippled No parallelism

Scalar UDF Inlining The solution: FROID Runs at parse time Tries to convert UDF into equivalent query expression (In internal represenatation) Does some optimization (constant folding, dead code elimination, and others) Injects this with APPLY in query Result goes to Query Optimizer

Scalar UDF Inlining Example

Scalar UDF Inlining FROID equivalent (simplified!)

Scalar UDF Inlining FROID execution plan

Scalar UDF Inlining FROID performance Old behavior (compatibility level 140 or lower) 10 executions took 10.8 seconds FROID enabled (compatibility level 150) 10 executions took 2.1 seconds

Scalar UDF Inlining Limitations of FROID Limitations on UDF itself No functions that can change between calls Time-related, e.g. GETDATE(), CURRENT_TIMESTAMP, … Affects state for future call, e.g. NEWSEQUENTIALID(), RAND() , … No loops (WHILE) in UDF code No table variables used Check sys.sql_modules.is_inlineable to verify if UDF qualifies

Scalar UDF Inlining Limitations of FROID Limitations on UDF itself Limitations on how/where UDF is used UDF itself not used in GROUP BY Nesting and recursion are partly supported Replacement stops at certain level

http://tinyurl.com/FROID-dox Scalar UDF Inlining Limitations of FROID Limitations on UDF itself Limitations on how/where UDF is used Actual list is too long to include here Check full documentation on MSDN http://tinyurl.com/FROID-dox

Intelligent Query Processing Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/intelligent-query-processing?view=sql-server-2017

Approximate Count Distinct Sometimes, a close estimate is “good enough” Can be very beneficial for some specific problems For example “how many unique visitors on our web site this month?” (On a site that logs 10 million pageviews per day) We care about trend, not about the exact correct number

Approximate Count Distinct Standard approach (using COUNT DISTINCT)

Approximate Count Distinct New alternative (using APPROX_COUNT_DISTINCT)

Approximate Count Distinct How does it work? HyperLogLog algorithm (based on Flajolet-Martin algorithm) Flajolet-Martin: Hash to get “pseudo-random” value for each input value Find number of zeroes after last one in the binary representation of the hash (Can also use left-most zeroes, left-most ones, etc. – just a choice) Track highest number of zeroes in the input (50% of values end in 0 / 25% in 00 / 12.5% in 000 / etc) The higher this number, the less likely – so probably more distinct values

Approximate Count Distinct How does it work? HyperLogLog algorithm (based on Flajolet-Martin algorithm) Flajolet-Martin Weaknesses of Flajolet-Martin High variance (even single row can have value that hashes to 0x1000000000) Hash collisions can also cause incorrect results

Approximate Count Distinct How does it work? HyperLogLog algorithm (based on Flajolet-Martin algorithm) Flajolet-Martin Weaknesses of Flajolet-Martin Flajolet-Martin  HyperLog  HyperLogLog Divides input in separate subsets (based on first bits in binary hash) Computes Flajolet-Martin for each subset Final result based on harmonic mean of the results per subset Error margin: within 2% for at least 97% of all use cases (In other words: 3% chance to be more than 2% wrong)

Intelligent Query Processing Source: https://docs.microsoft.com/en-us/sql/relational-databases/performance/intelligent-query-processing?view=sql-server-2017

Twitter: @Hugo_Kornelis T H E E N D Questions? Email: hugo@perFact.info Twitter: @Hugo_Kornelis https://sqlserverfast.com/presentations/from-adaptive-to-intelligent-query-processing-in-sql-2019/