SlideShare a Scribd company logo
1 of 25
Matan Yungman
Microsoft Protection Services (via Log-On Software)
Blog: www.dbnewsfeed.com
Twitter: @MatanYungman
 What is the Plan Cache
 Ways to explore it
 How to successfully leverage it
 Querying it to identify problematic queries
 Querying Execution Plan XML
Parse


Bind

          Optimize
 Query Execution Time
 CPU
 Memory



           The Solution:
   Save plan for future reuse
 Built from 4 cache stores
  • CACHESTORE_OBJCP – Object Plans
  • CACHESTORE_SQLCP – Ad hoc and Prepared Plans
  • CACHESTORE_PHDR – Bound Trees
  • CACHESTORE_XPROC – Extended Procedures


 “Steals” its memory from the buffer pool
 Each store is basically a hash table
 Age-out algorithm
 sys.dm_exec_cached_plans
     One row per batch
     Use counts
     Object type, Cache object type
     Plan_handle
 sys.dm_exec_query_stats
     One row per statement in a batch
     Execution count, Plan generation count
     IO
     Duration
     Plan_handle, Sql_handle
     Query_hash and Query_plan_hash
 sys.dm_exec_sql_text
   Pass plan_handle/sql_handle to get batch text
   Use offsets to get query level text
 sys.dm_exec_query_plan
   Use to get the query plan
   Returns XML plan of all the statements in a batch
 sys.dm_exec_text_query_plan
   Use to get the plan at the statement level
   Returns nvarchar(max). Cast it to XML
 sys.dm_exec_plan_attributes
   Use to determine lack of reuse reasons
   All cache_key attributes must match for reuse to happen
 Objects (Stored Procedures, Functions, Triggers)
   (ObjectID * DB_ID) % (Hash Table Size)
 Ad-hoc and prepared statements
   Query Text Hash
   Exact text match is needed
 Also for reuse to be possible:
   Set Options
   User ID (Schema ID)
   DB_ID
   And More…
 Lots of plans with use count of 1
 Not necessarily user queries
   ADO.Net, EXEC(), System Queries
 How to identify:
   Search for ad-hoc plans with use count = 1
 Optimization options:
   Optimize for ad hoc workloads
   Switch to parameterized methods
   Turn on Forced Parameterization
      Try to test it’s appropriate for your system
      Can also be used selectively through plan guides
SELECT
SUM(CAST(size_in_bytes AS BIGINT))/1024.0/1024.0
AS SizeInMB, COUNT(*) AS PlanCount
FROM sys.dm_exec_cached_plans
WHERE objtype = 'Adhoc'
AND cacheobjtype = 'Compiled Plan'
AND usecounts = 1
 Nhibernate
   String parameter lengths are supplied according to each
    specific execution
   Solution: Nhibernate “prepare_sql” parameter
 Linq To SQL & Entity Framework
   Same as Nhibernate
   Solution: Upgrade to Dot Net 4.0
 Simple ADO.Net queries
   Act as Ad-hoc queries
   Optimization options:
      Optimize for ad-hoc workloads option
      Forced parameterization
      ADO.Net parameterized queries (supply length for strings)
• Can help identify potentially reusable queries
• Recommended query #2:

 SELECT
 query_hash,COUNT(*) AS PlanCount
 FROM sys.dm_exec_query_stats
 GROUP BY query_hash
 HAVING COUNT(*) > 1
75% 0-4GB
                          +
                    10% 4Gb-64GB
                          +
                      5% > 64GB
 Max number of items per cache store:
   10007 on 32-bit, 40009 on 64-bit
 Numbers are as of SQL Server 2005 SP2
 Age-out algorithm based on:
   Plan generation cost
   Usage
 On memory pressure, all plan costs are decreased by 1
 Plans with cost = 0 can be evicted
 Ad-hoc plans always enter with cost = 0
   Cost incremented by 1 on each reuse
 For other plans
   Cost is reset to the original value on reuse
 Ad-hoc max cost = 16
 Prepared max cost = 256
 No max cost for stored procedures
 DBCC FREEPROCCACHE
   Clears all plans
 Can also be more specific
   plan_handle / sql_handle / pool_name
 DBCC FREESYSTEMCACHE
   Specific cache store: ‘SQL Plans’ / ‘Object Plans’
 Undocumented
   DBCC FLUSHPROCINDB – Specific database
 We can answer questions like:
   Which queries are most IO/CPU/Time intensive?
     Absolute numbers or per run
   Which database is the most resource intensive?
   What is the value a plan was optimized for?
   Which queries/objects are frequently recompiled?
   And more..
 Plan is optimized for first execution’s parameters
 Local variable values not known to the optimizer
 Possible solution: Recompile
   Stored procedure level:
      Create Procedure..With Recompile
      Exec..With Recompile
      sp_recompile
   Statement level:
      Option (Recompile)


 As we saw, it comes with a performance penalty
 Create Procedure..With Recompile means:
   “Generate new plan on each run and don’t cache it”
 Exec..With Recompile means:
   “Generate new plan for this run and don’t cache it”
 Option (Rcompile) means:
   Generate new plan on each run
   Set execution_count to 1
   Increase plan_generation_num by 1
 Local variable problem?
   Pass the values as parameters from the outside
   Use Dynamic-SQL (preferably sp_executesql)
 Parameter Sniffing problem?
   Multiple procedures for different cardinalities
   Optimize for (@param = value)
   Optimize for unknown / parameter masking
   Plan freezing
      sp_create_plan_guide_from_handle
   Option (use plan)
      Can also be done with a plan guide
 Can answer questions like:
   Which plans suffer from missing indexes?
   Which plans suffer from Implicit_Conversion?
   Which plans use what index?
   Which plans’ optimization were timed out?
   Which plans have inaccurate row count estimation?
   And more..
 Further Learning:
   Brent Ozar’s sp_bliz procedure:
      http://www.brentozar.com/blitz/
   Jason Strate’s Plan Cache series
      http://www.jasonstrate.com/2011/02/can-you-dig-it-plan-
       cache-series/
   Jason Strate: Using XML to Query Execution Plans
      http://dba.sqlpass.org/MeetingArchive.aspx
 Reuse is almost always a good thing
 Try to prevent Plan Cache bloat
 Remember the effects of Recompile
 The Plan Cache is your workload – Get to know it
Optimize SQL Performance with Plan Cache Analysis

More Related Content

What's hot

Introduction to Structured Streaming
Introduction to Structured StreamingIntroduction to Structured Streaming
Introduction to Structured StreamingKnoldus Inc.
 
Salesforce Batch processing - Atlanta SFUG
Salesforce Batch processing - Atlanta SFUGSalesforce Batch processing - Atlanta SFUG
Salesforce Batch processing - Atlanta SFUGvraopolisetti
 
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should KnowSQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should KnowBob Ward
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3sqlserver.co.il
 
Online index rebuild automation
Online index rebuild automationOnline index rebuild automation
Online index rebuild automationCarlos Sierra
 
Oracle to PostgreSQL migration
Oracle to PostgreSQL migrationOracle to PostgreSQL migration
Oracle to PostgreSQL migrationstrikr .
 
SQL Server It Just Runs Faster
SQL Server It Just Runs FasterSQL Server It Just Runs Faster
SQL Server It Just Runs FasterBob Ward
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)Karthik .P.R
 
Experience sql server on l inux and docker
Experience sql server on l inux and dockerExperience sql server on l inux and docker
Experience sql server on l inux and dockerBob Ward
 
Hadoop spark online demo
Hadoop spark online demoHadoop spark online demo
Hadoop spark online demoTripti Jha
 
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)Matt Fuller
 
Sql server 2016 it just runs faster sql bits 2017 edition
Sql server 2016 it just runs faster   sql bits 2017 editionSql server 2016 it just runs faster   sql bits 2017 edition
Sql server 2016 it just runs faster sql bits 2017 editionBob Ward
 
CosmosDB for DBAs & Developers
CosmosDB for DBAs & DevelopersCosmosDB for DBAs & Developers
CosmosDB for DBAs & DevelopersNiko Neugebauer
 
Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017Bob Ward
 
Module Owb External Execution
Module Owb External ExecutionModule Owb External Execution
Module Owb External ExecutionNicholas Goodman
 
MySQL HA Percona cluster @ MySQL meetup Mumbai
MySQL HA Percona cluster @ MySQL meetup MumbaiMySQL HA Percona cluster @ MySQL meetup Mumbai
MySQL HA Percona cluster @ MySQL meetup MumbaiRemote MySQL DBA
 
Drill architecture 20120913
Drill architecture 20120913Drill architecture 20120913
Drill architecture 20120913jasonfrantz
 
Clustered Columnstore - Deep Dive
Clustered Columnstore - Deep DiveClustered Columnstore - Deep Dive
Clustered Columnstore - Deep DiveNiko Neugebauer
 

What's hot (20)

Introduction to Structured Streaming
Introduction to Structured StreamingIntroduction to Structured Streaming
Introduction to Structured Streaming
 
Salesforce Batch processing - Atlanta SFUG
Salesforce Batch processing - Atlanta SFUGSalesforce Batch processing - Atlanta SFUG
Salesforce Batch processing - Atlanta SFUG
 
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should KnowSQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3
 
Online index rebuild automation
Online index rebuild automationOnline index rebuild automation
Online index rebuild automation
 
Oracle to PostgreSQL migration
Oracle to PostgreSQL migrationOracle to PostgreSQL migration
Oracle to PostgreSQL migration
 
SQL Server It Just Runs Faster
SQL Server It Just Runs FasterSQL Server It Just Runs Faster
SQL Server It Just Runs Faster
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)
 
Experience sql server on l inux and docker
Experience sql server on l inux and dockerExperience sql server on l inux and docker
Experience sql server on l inux and docker
 
Hadoop spark online demo
Hadoop spark online demoHadoop spark online demo
Hadoop spark online demo
 
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
 
Redshift deep dive
Redshift deep diveRedshift deep dive
Redshift deep dive
 
Sql server 2016 it just runs faster sql bits 2017 edition
Sql server 2016 it just runs faster   sql bits 2017 editionSql server 2016 it just runs faster   sql bits 2017 edition
Sql server 2016 it just runs faster sql bits 2017 edition
 
CosmosDB for DBAs & Developers
CosmosDB for DBAs & DevelopersCosmosDB for DBAs & Developers
CosmosDB for DBAs & Developers
 
Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017
 
Module Owb External Execution
Module Owb External ExecutionModule Owb External Execution
Module Owb External Execution
 
MySQL HA Percona cluster @ MySQL meetup Mumbai
MySQL HA Percona cluster @ MySQL meetup MumbaiMySQL HA Percona cluster @ MySQL meetup Mumbai
MySQL HA Percona cluster @ MySQL meetup Mumbai
 
Drill architecture 20120913
Drill architecture 20120913Drill architecture 20120913
Drill architecture 20120913
 
Introduction to AJAX
Introduction to AJAXIntroduction to AJAX
Introduction to AJAX
 
Clustered Columnstore - Deep Dive
Clustered Columnstore - Deep DiveClustered Columnstore - Deep Dive
Clustered Columnstore - Deep Dive
 

Viewers also liked

Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013sqlserver.co.il
 
Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013sqlserver.co.il
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012sqlserver.co.il
 
Extreme performance - IDF UG
Extreme performance - IDF UGExtreme performance - IDF UG
Extreme performance - IDF UGsqlserver.co.il
 
3 extreme performance - databases acceleration using ssd
3   extreme performance - databases acceleration using ssd 3   extreme performance - databases acceleration using ssd
3 extreme performance - databases acceleration using ssd sqlserver.co.il
 
3 where are my keys - sql explore
3   where are my keys - sql explore3   where are my keys - sql explore
3 where are my keys - sql exploresqlserver.co.il
 
7 sql azure for sql explore
7   sql azure for sql explore7   sql azure for sql explore
7 sql azure for sql exploresqlserver.co.il
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2sqlserver.co.il
 
What Is New In 2008 R2 Public
What Is New In 2008 R2 PublicWhat Is New In 2008 R2 Public
What Is New In 2008 R2 Publicsqlserver.co.il
 
Lessons learned from running massive WordPress sites at scale
Lessons learned from running massive WordPress sites at scaleLessons learned from running massive WordPress sites at scale
Lessons learned from running massive WordPress sites at scaleCory Fowler
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkelsqlserver.co.il
 
6 sql explorer - powershell dba
6   sql explorer - powershell dba6   sql explorer - powershell dba
6 sql explorer - powershell dbasqlserver.co.il
 

Viewers also liked (17)

Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013
 
Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012
 
DAC 2012
DAC 2012DAC 2012
DAC 2012
 
Dbcc.doc
Dbcc.docDbcc.doc
Dbcc.doc
 
Extreme performance - IDF UG
Extreme performance - IDF UGExtreme performance - IDF UG
Extreme performance - IDF UG
 
Dat308
Dat308Dat308
Dat308
 
IO Dubi Lebel
IO Dubi LebelIO Dubi Lebel
IO Dubi Lebel
 
3 extreme performance - databases acceleration using ssd
3   extreme performance - databases acceleration using ssd 3   extreme performance - databases acceleration using ssd
3 extreme performance - databases acceleration using ssd
 
3 where are my keys - sql explore
3   where are my keys - sql explore3   where are my keys - sql explore
3 where are my keys - sql explore
 
7 sql azure for sql explore
7   sql azure for sql explore7   sql azure for sql explore
7 sql azure for sql explore
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2
 
What Is New In 2008 R2 Public
What Is New In 2008 R2 PublicWhat Is New In 2008 R2 Public
What Is New In 2008 R2 Public
 
Lessons learned from running massive WordPress sites at scale
Lessons learned from running massive WordPress sites at scaleLessons learned from running massive WordPress sites at scale
Lessons learned from running massive WordPress sites at scale
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
 
Board retreat pwpt template
Board retreat pwpt templateBoard retreat pwpt template
Board retreat pwpt template
 
6 sql explorer - powershell dba
6   sql explorer - powershell dba6   sql explorer - powershell dba
6 sql explorer - powershell dba
 

Similar to Optimize SQL Performance with Plan Cache Analysis

Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning serviceRuth Yakubu
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingGrant Fritchey
 
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning PerformanceAnirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning PerformanceLviv Startup Club
 
Prediction as a service with ensemble model in SparkML and Python ScikitLearn
Prediction as a service with ensemble model in SparkML and Python ScikitLearnPrediction as a service with ensemble model in SparkML and Python ScikitLearn
Prediction as a service with ensemble model in SparkML and Python ScikitLearnJosef A. Habdank
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native CompilationPGConf APAC
 
Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Rajeev Rastogi (KRR)
 
Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...
Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...
Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...inside-BigData.com
 
Flex 4.5 jeyasekar
Flex 4.5  jeyasekarFlex 4.5  jeyasekar
Flex 4.5 jeyasekarjeya soft
 
hbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at Alibaba
hbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at Alibabahbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at Alibaba
hbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at AlibabaMichael Stack
 
Toronto meetup 20190917
Toronto meetup 20190917Toronto meetup 20190917
Toronto meetup 20190917Bill Liu
 
Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...
Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...
Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...Turi, Inc.
 
Start machine learning in 5 simple steps
Start machine learning in 5 simple stepsStart machine learning in 5 simple steps
Start machine learning in 5 simple stepsRenjith M P
 
A so common questions and answers
A so common questions and answersA so common questions and answers
A so common questions and answersAmit Sharma
 
Webinar: Performance Tuning + Optimization
Webinar: Performance Tuning + OptimizationWebinar: Performance Tuning + Optimization
Webinar: Performance Tuning + OptimizationMongoDB
 
AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...
AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...
AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...GeeksLab Odessa
 
MLOps pipelines using MLFlow - From training to production
MLOps pipelines using MLFlow - From training to productionMLOps pipelines using MLFlow - From training to production
MLOps pipelines using MLFlow - From training to productionFabian Hadiji
 
Intro to SnappyData Webinar
Intro to SnappyData WebinarIntro to SnappyData Webinar
Intro to SnappyData WebinarSnappyData
 

Similar to Optimize SQL Performance with Plan Cache Analysis (20)

Azure machine learning service
Azure machine learning serviceAzure machine learning service
Azure machine learning service
 
Performance Tuning
Performance TuningPerformance Tuning
Performance Tuning
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and Alerting
 
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning PerformanceAnirudh Koul. 30 Golden Rules of Deep Learning Performance
Anirudh Koul. 30 Golden Rules of Deep Learning Performance
 
Prediction as a service with ensemble model in SparkML and Python ScikitLearn
Prediction as a service with ensemble model in SparkML and Python ScikitLearnPrediction as a service with ensemble model in SparkML and Python ScikitLearn
Prediction as a service with ensemble model in SparkML and Python ScikitLearn
 
Run time storage
Run time storageRun time storage
Run time storage
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native Compilation
 
Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2
 
Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...
Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...
Efficient Model Selection for Deep Neural Networks on Massively Parallel Proc...
 
System mldl meetup
System mldl meetupSystem mldl meetup
System mldl meetup
 
Flex 4.5 jeyasekar
Flex 4.5  jeyasekarFlex 4.5  jeyasekar
Flex 4.5 jeyasekar
 
hbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at Alibaba
hbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at Alibabahbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at Alibaba
hbaseconasia2019 Phoenix Improvements and Practices on Cloud HBase at Alibaba
 
Toronto meetup 20190917
Toronto meetup 20190917Toronto meetup 20190917
Toronto meetup 20190917
 
Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...
Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...
Declarative Machine Learning: Bring your own Syntax, Algorithm, Data and Infr...
 
Start machine learning in 5 simple steps
Start machine learning in 5 simple stepsStart machine learning in 5 simple steps
Start machine learning in 5 simple steps
 
A so common questions and answers
A so common questions and answersA so common questions and answers
A so common questions and answers
 
Webinar: Performance Tuning + Optimization
Webinar: Performance Tuning + OptimizationWebinar: Performance Tuning + Optimization
Webinar: Performance Tuning + Optimization
 
AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...
AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...
AI&BigData Lab 2016. Руденко Петр: Особенности обучения, настройки и использо...
 
MLOps pipelines using MLFlow - From training to production
MLOps pipelines using MLFlow - From training to productionMLOps pipelines using MLFlow - From training to production
MLOps pipelines using MLFlow - From training to production
 
Intro to SnappyData Webinar
Intro to SnappyData WebinarIntro to SnappyData Webinar
Intro to SnappyData Webinar
 

More from sqlserver.co.il

SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1sqlserver.co.il
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Eventssqlserver.co.il
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoresqlserver.co.il
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACsqlserver.co.il
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatialsqlserver.co.il
 
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...sqlserver.co.il
 
3 extreme performance - databases acceleration using ssd
3   extreme performance - databases acceleration using ssd 3   extreme performance - databases acceleration using ssd
3 extreme performance - databases acceleration using ssd sqlserver.co.il
 
4 extreme performance - part ii
4   extreme performance - part ii4   extreme performance - part ii
4 extreme performance - part iisqlserver.co.il
 
2 extreme performance - smaller is better
2   extreme performance - smaller is better2   extreme performance - smaller is better
2 extreme performance - smaller is bettersqlserver.co.il
 
1 extreme performance - part i
1   extreme performance - part i1   extreme performance - part i
1 extreme performance - part isqlserver.co.il
 
4 sql explore סודות האופטימייזר
4   sql explore סודות האופטימייזר4   sql explore סודות האופטימייזר
4 sql explore סודות האופטימייזרsqlserver.co.il
 

More from sqlserver.co.il (17)

SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStore
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DAC
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatial
 
מיכאל
מיכאלמיכאל
מיכאל
 
נועם
נועםנועם
נועם
 
עדי
עדיעדי
עדי
 
מיכאל
מיכאלמיכאל
מיכאל
 
DBCC - Dubi Lebel
DBCC - Dubi LebelDBCC - Dubi Lebel
DBCC - Dubi Lebel
 
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...Fast transition to sql server 2012 from mssql 2005 2008 for  developers - Dav...
Fast transition to sql server 2012 from mssql 2005 2008 for developers - Dav...
 
ISUG 113: File stream
ISUG 113: File streamISUG 113: File stream
ISUG 113: File stream
 
3 extreme performance - databases acceleration using ssd
3   extreme performance - databases acceleration using ssd 3   extreme performance - databases acceleration using ssd
3 extreme performance - databases acceleration using ssd
 
4 extreme performance - part ii
4   extreme performance - part ii4   extreme performance - part ii
4 extreme performance - part ii
 
2 extreme performance - smaller is better
2   extreme performance - smaller is better2   extreme performance - smaller is better
2 extreme performance - smaller is better
 
1 extreme performance - part i
1   extreme performance - part i1   extreme performance - part i
1 extreme performance - part i
 
4 sql explore סודות האופטימייזר
4   sql explore סודות האופטימייזר4   sql explore סודות האופטימייזר
4 sql explore סודות האופטימייזר
 

Optimize SQL Performance with Plan Cache Analysis

  • 1. Matan Yungman Microsoft Protection Services (via Log-On Software) Blog: www.dbnewsfeed.com Twitter: @MatanYungman
  • 2.  What is the Plan Cache  Ways to explore it  How to successfully leverage it  Querying it to identify problematic queries  Querying Execution Plan XML
  • 3. Parse Bind Optimize
  • 4.  Query Execution Time  CPU  Memory The Solution: Save plan for future reuse
  • 5.  Built from 4 cache stores • CACHESTORE_OBJCP – Object Plans • CACHESTORE_SQLCP – Ad hoc and Prepared Plans • CACHESTORE_PHDR – Bound Trees • CACHESTORE_XPROC – Extended Procedures  “Steals” its memory from the buffer pool  Each store is basically a hash table  Age-out algorithm
  • 6.  sys.dm_exec_cached_plans  One row per batch  Use counts  Object type, Cache object type  Plan_handle  sys.dm_exec_query_stats  One row per statement in a batch  Execution count, Plan generation count  IO  Duration  Plan_handle, Sql_handle  Query_hash and Query_plan_hash
  • 7.  sys.dm_exec_sql_text  Pass plan_handle/sql_handle to get batch text  Use offsets to get query level text  sys.dm_exec_query_plan  Use to get the query plan  Returns XML plan of all the statements in a batch  sys.dm_exec_text_query_plan  Use to get the plan at the statement level  Returns nvarchar(max). Cast it to XML  sys.dm_exec_plan_attributes  Use to determine lack of reuse reasons  All cache_key attributes must match for reuse to happen
  • 8.  Objects (Stored Procedures, Functions, Triggers)  (ObjectID * DB_ID) % (Hash Table Size)  Ad-hoc and prepared statements  Query Text Hash  Exact text match is needed  Also for reuse to be possible:  Set Options  User ID (Schema ID)  DB_ID  And More…
  • 9.  Lots of plans with use count of 1  Not necessarily user queries  ADO.Net, EXEC(), System Queries  How to identify:  Search for ad-hoc plans with use count = 1  Optimization options:  Optimize for ad hoc workloads  Switch to parameterized methods  Turn on Forced Parameterization  Try to test it’s appropriate for your system  Can also be used selectively through plan guides
  • 10. SELECT SUM(CAST(size_in_bytes AS BIGINT))/1024.0/1024.0 AS SizeInMB, COUNT(*) AS PlanCount FROM sys.dm_exec_cached_plans WHERE objtype = 'Adhoc' AND cacheobjtype = 'Compiled Plan' AND usecounts = 1
  • 11.  Nhibernate  String parameter lengths are supplied according to each specific execution  Solution: Nhibernate “prepare_sql” parameter  Linq To SQL & Entity Framework  Same as Nhibernate  Solution: Upgrade to Dot Net 4.0  Simple ADO.Net queries  Act as Ad-hoc queries  Optimization options:  Optimize for ad-hoc workloads option  Forced parameterization  ADO.Net parameterized queries (supply length for strings)
  • 12. • Can help identify potentially reusable queries • Recommended query #2: SELECT query_hash,COUNT(*) AS PlanCount FROM sys.dm_exec_query_stats GROUP BY query_hash HAVING COUNT(*) > 1
  • 13. 75% 0-4GB + 10% 4Gb-64GB + 5% > 64GB  Max number of items per cache store:  10007 on 32-bit, 40009 on 64-bit  Numbers are as of SQL Server 2005 SP2
  • 14.  Age-out algorithm based on:  Plan generation cost  Usage  On memory pressure, all plan costs are decreased by 1  Plans with cost = 0 can be evicted  Ad-hoc plans always enter with cost = 0  Cost incremented by 1 on each reuse  For other plans  Cost is reset to the original value on reuse  Ad-hoc max cost = 16  Prepared max cost = 256  No max cost for stored procedures
  • 15.  DBCC FREEPROCCACHE  Clears all plans  Can also be more specific  plan_handle / sql_handle / pool_name  DBCC FREESYSTEMCACHE  Specific cache store: ‘SQL Plans’ / ‘Object Plans’  Undocumented  DBCC FLUSHPROCINDB – Specific database
  • 16.  We can answer questions like:  Which queries are most IO/CPU/Time intensive?  Absolute numbers or per run  Which database is the most resource intensive?  What is the value a plan was optimized for?  Which queries/objects are frequently recompiled?  And more..
  • 17.  Plan is optimized for first execution’s parameters  Local variable values not known to the optimizer  Possible solution: Recompile  Stored procedure level:  Create Procedure..With Recompile  Exec..With Recompile  sp_recompile  Statement level:  Option (Recompile)  As we saw, it comes with a performance penalty
  • 18.
  • 19.
  • 20.  Create Procedure..With Recompile means:  “Generate new plan on each run and don’t cache it”  Exec..With Recompile means:  “Generate new plan for this run and don’t cache it”  Option (Rcompile) means:  Generate new plan on each run  Set execution_count to 1  Increase plan_generation_num by 1
  • 21.  Local variable problem?  Pass the values as parameters from the outside  Use Dynamic-SQL (preferably sp_executesql)  Parameter Sniffing problem?  Multiple procedures for different cardinalities  Optimize for (@param = value)  Optimize for unknown / parameter masking  Plan freezing  sp_create_plan_guide_from_handle  Option (use plan)  Can also be done with a plan guide
  • 22.  Can answer questions like:  Which plans suffer from missing indexes?  Which plans suffer from Implicit_Conversion?  Which plans use what index?  Which plans’ optimization were timed out?  Which plans have inaccurate row count estimation?  And more..
  • 23.  Further Learning:  Brent Ozar’s sp_bliz procedure:  http://www.brentozar.com/blitz/  Jason Strate’s Plan Cache series  http://www.jasonstrate.com/2011/02/can-you-dig-it-plan- cache-series/  Jason Strate: Using XML to Query Execution Plans  http://dba.sqlpass.org/MeetingArchive.aspx
  • 24.  Reuse is almost always a good thing  Try to prevent Plan Cache bloat  Remember the effects of Recompile  The Plan Cache is your workload – Get to know it

Editor's Notes

  1. Ad-hoc lookup demo
  2. Demoes all the way down until Querying execution plan xml.
  3. Demoes all the way down