Query Profiling Options in SQL Server Karol Papaj Query Profiling Options in SQL Server
Karol Papaj SolidQ SQL Server BI SQL Server High Availability SQL Server Relational Engine kpapaj@solidq.com @KarolPapaj http://kapa-consulting.sk/kapa-consulting/blog-en/
Execution plan with estimates What’s Execution Plan Instructions how to perform a given query Execution plan with estimates how many rows each node will have to process Execution plan with runtime statistics
Why we need Estimated Plan? Performance troubleshooting (why is your query slow?) Estimated Serial? Parallel? Seek? Scan? Loop?Merge?Hash? Serial? Parallel? Seek? Scan? Loop?Merge?Hash?
How Developer Can Get Estimated Plan SET SHOWPLAN_TEXT SET SHOWPLAN_XML SET SHOWPLAN_ALL Ctrl+L or “Display Estimated Execution Plan” in SSMS
How Developer Can Get Estimated Plan Demo How Developer Can Get Estimated Plan
Why we need Actual Plan? Performance troubleshooting (why is your query slow?) Actual Duration? SQL Server 2014 SP2 and higher Number of rows Cardinality estimation error? Missing filter condition Outdated statistics Memory used Threads used Degree of parallelism
How Developer Can Get Actual Plan SET STATISTICS PROFILE SET STATISTICS XML Ctrl+K or “Include Actual Execution Plan” in SSMS
How Developer Can Get Actual Plan Demo How Developer Can Get Actual Plan
How DBA Can Get Estimated Plan? Showplan XML event in Profiler query_pre_execution_showplan in Extended Events sys.dm_exec_cached_plans
How DBA Can Get Estimated Plan Demo How DBA Can Get Estimated Plan
How DBA Can Get Actual Plan? Showplan XML Statistics Profile in Profiler query_post_execution_showplan in Extended Events
How DBA Can Get Actual Plan Demo How DBA Can Get Actual Plan
2 Problems
1
Legacy profiling infrastructure CPU Overhead https://blogs.msdn.microsoft.com/sql_server_team/query-progress-anytime-anywhere/
Lightweight infrastructure SQL Server 2016 SP1 query_thread_profile extended event DBCC TRACEON(7412,-1) GO Enabled by TF 7412
Lightweight infrastructure Demo Lightweight infrastructure
Profiling Infrastructure CPU Overhead Source: https://blogs.msdn.microsoft.com/sql_server_team/query-progress-anytime-anywhere/
2
query_thread_profile QUERY_PLAN_PROFILE SQL 2016 SP2 CU3 SQL 2017 CU11 SQL 2019 query_thread_profile extended event SELECT COUNT (*) FROM Sales.SalesOrderHeaderEnlarged; OPTION (USE HINT('QUERY_PLAN_PROFILE')); QUERY_PLAN_PROFILE hint
Demo QUERY_PLAN_PROFILE
How to use QUERY_PLAN_PROFILE without touching a code Query_ID Profiling 1 2 Configuration table Plan Guide
QUERY_PLAN_PROFILE Practical Usage Demo QUERY_PLAN_PROFILE Practical Usage
Be careful on getting Actual Execution Plan from production on SQL Server before 2014 SP2/2016 SQL 2014 SP2/2016 better SQL 2016 SP1 much better (enable lightweight infrastructure if CPU usage is < 95% on your server) SQL Server 2019 has lightweight infrastructure enabled by default SQL 2016 SP2 CU3/2017 CU11/2019 even better! (query_plan_profile_hint)
QUESTIONS ? #SQLCLINIC
Resources MS SQL Tiger Team blog: https://blogs.msdn.microsoft.com/sql_server_team/query-progress-anytime-anywhere/ Execution Plan Reference by Hugo Kornelis: https://sqlserverfast.com/epr/
THANK YOU !