SlideShare a Scribd company logo
SQL Tuning 101
The three pillars of SQL Tuning:
Diagnostics Collection, Root Cause Analysis and
Remediation
Carlos Sierra
Agenda
• Motivation
• Methodology
– Diagnostics Collection
– Root Cause Analysis
– Remediation
• Some Important Topics
– Execution Plan
– Cost-based Optimizer
– Plan Stability
• Free Tools
Motivation
• One bad SQL can degrade overall DB performance!
• SQL Tuning is:
– A Complex Task
– Part Science and part Art
– The Responsibility of WHO?
• DBA?
• Developer?
• Oracle?
• Application Vendor?
Methodology
The three pillars of SQL Tuning
1. Good Practices
2. Diagnostics Collection
3. Root Cause Analysis (RCA)
4. Remediation
I. Good Practices
• The forgotten pillar of SQL Tuning
• Quid pro Quo (e.g. This for That or Give and Take)
– Be nice to the CBO and the CBO will be nice to you
• A healthy environment requires less SQL Tuning
• A win-win!
• Except for Consultants like myself!
Some Good Practices
• Start with a modern and solid database release
• Reset all CBO related parameters (and some more)
• Gather CBO Statistics as per Application or Product
– Automatic task does a decent job on 11.2.0.4+
• Write reasonable SQL
– Use the product as designed and avoid weird SQL
– If SQL is too long or too complex the CBO will struggle
Partial list of Parameters to Reset
• optimizer_features_enable
• optimizer_index_cost_adj
• optimizer_index_caching
• db_file_multiblock_read_count (not CBO exclusive)
• cursor_sharing (not an optimizer parameter)
• optimizer_dynamic_sampling
• optimizer_mode
II. Diagnostics Collection
a) Identify SQL_ID
b) Use Oracle-product base Tools
c) Supplement with specialized Tools
II.a Identify SQL_ID
• Oracle Enterprise Manager (OEM)
– [ OEM] [ Database | Grid | Cloud ] Control
• Automatic Workload Repository (AWR)
• Statspack (SP)
• SQL Trace (and TKPROF)
• V$ dynamic views (e.g. V$SQL and V$SESSION)
II.b Oracle-product base Tools
• SQL Trace (and TKPROF)
• Active Session History (ASH)
• EXPLAIN PLAN FOR
• AUTOTRACE
• SQL Developer
• DBMS_XPLAN
• SQL Monitor
SQL Trace
• ALTER SESSION SET
– SQL_TRACE = [ TRUE | FALSE ]
– EVENTS ‘10046 trace name context forever, level N’
• N = 1: all executions (10g-) or 1st execution (11g+)
• N = 4: Binds
• N = 8: Waits
• N = 12 = 4 + 8
• N = 16: each execution (11g+)
• N = 64: 1st + each where DB time > 1min (11.2.0.2+)
Where is my Trace?
• V$DIAG_INFO (11.1+)
• USER_DUMP_DEST (deprecated 12c)
TKPROF: SQL Text and DB Calls
TKPROF: Execution Plan and Wait Events
Active Session History (ASH)
• 1 sec snapshots of V$SESSION
• Every 10 ASH samples into AWR (10 sec granularity)
• Sessions “ON CPU” or “WAITING” (non-idle)
• Multiple Dimensions
• Excellent repository for performance data mining
• Requires Oracle Diagnostics Pack!
SQL Trace vs. ASH
Pros
• Precise
• Free
Pros
• Always available
• Multi-dimensional repository
• Plan line granularity (11g+)
Cons
• Re-execute transaction
• Waits per cursor
– No plan line granularity
Cons
• Requires Diagnostics Pack
EXPLAIN PLAN FOR
• Actual SQL is not executed
• Blind to Bind Peeking, thus unreliable with Binds
sqltuning101-170419021007-2.pdf
SET AUTOTRACE ON
• Actual SQL is executed
• Includes small subset of Session Statistics
Query Output and Execution Plan
Predicate Information and Session Stats
SQL Developer
sqltuning101-170419021007-2.pdf
DBMS_XPLAN
• DISPLAY (from PLAN_TABLE)
• DISPLAY_CURSOR
• DISPLAY_AWR
• DISPLAY_SQL_PLAN_BASELINE
• DISPLAY_SQL_SET
• SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY%());
DBMS_XPLAN.DISPLAY_CURSOR
• SQL_ID
• CURSOR_CHILD_NO (defaults to 0!)
• FORMAT
– TYPICAL = DEFAULT
– ALL = TYPICAL + QB + PROJECTION + ALIAS + REMOTE
– ADVANCED = ALL + OUTLINE + BINDS
– ALLSTATS = IOSTATS + MEMSTATS (all executions)
– ALLSTATS LAST (last execution)
– ADAPTIVE (12c)
Sample query with hints and binds
sqltuning101-170419021007-2.pdf
SQL Monitor
• Part of the Oracle Tuning Pack
• Tabular and Graphical Execution Plan
• Timeline per Plan Line
• Binds and Predicates
• Nice and intuitive
• Executed from OEM or SQL*Plus
sqltuning101-170419021007-2.pdf
sqltuning101-170419021007-2.pdf
II.c Specialized Tools
• SQLTXPLAIN (a.k.a. SQLT)
– My Oracle Support (MOS) 215187.1
• SQLHC
– MOS 1366133.1
• SQLd360
– By Mauro Pagano
• Others?
III. Root Cause Analysis
• This is where you want to spend your time!
• Go over all the output of your specialized tool
• Look for correlations and possible causation
• Put yourself in the shoes of the Optimizer
• Question everything (is there a better way?)
• Ask for help after you have done your homework
• Enjoy the ride!
Typical Steps
1. Find where the time is spent (plan line)
– Too many executions or too much time per exec?
2. Validate estimated rows versus actual rows
– If off determine why (deficient statistics maybe?)
3. What is the history of this SQL?
– Can you find a better performing plan?
4. Can you make perfect sense of the SQL and plan?
IV. Remediation
• Explore multiple possibilities
– And try to prove yourself wrong!
• Implement the smallest-scope fix
– Avoid system-wide changes unless you are rock solid!
• Avoid guesses and assumptions (piñata method)
– Too many people out there are “tuning” blind-folded!
• Trust nobody! (be skeptic and test thoroughly)
Three Important Topics
1. Execution Plan
2. Cost-based Optimizer
3. Plan Stability
1. Execution Plan
• If you do not understand the SQL or the Execution
Plan stop right there!
• From SQL text you can draw a diagram if that helps
• To understand the Execution Plan start small
– Review a two-tables query and review plan
– Move to a three-tables query and review plan
– Introduce subqueries and more complex constructions
2. Cost-based Optimizer
• CBO was released on 7.3 (circa February 1996)
• A large and complex piece of software enhanced
for two decades
• By design the CBO produces an optimal execution
plan according to some representation of the data
– This means: execution plans are expected to change!
– Breathe deep and embrace the change!
3. Plan Stability
• Most DBAs hate Execution Plan changes
– They never hear of changes for the better!
• To remediate undesired plan changes we do have
several Plan Stability techniques
– SQL Plan Management (Baselines) from 11g
– SQL Profiles from 10g
– Deprecated Stored Outlines from 9i
– And then some others (SQL Patches and CBO Hints)
Popular Free Diagnostics Tools
• SQLTXPLAIN
• SQLHC
• SQLd360
• Stand-alone scripts
SQLTXPLAIN
SQLd360
sqltuning101-170419021007-2.pdf
SQLd360 Plan Tree
sqltuning101-170419021007-2.pdf
sqltuning101-170419021007-2.pdf
SQLT XPLAIN vs. SQLd360
Pros
• Oracle Support Standard
• Free (requires MOS account)
• Comprehensive and mature
Pros
• Free Software (no strings)
• Installs nothing on database
• Graphical visualization of data
Cons
• Requires installation
• HTML Tables (no charts)
Cons
• Still young (frequent releases)
SQL Tuning Road Map
1. Start with a clean and healthy environment
2. Identify SQL_ID
3. Collect diagnostics using comprehensive tools
4. Spend time analyzing diagnostics collected
5. Determine root cause
6. Explore remediation actions
7. Test and implement solution
Questions
The End
• http://carlos-sierra.net/
– Blog and downloads
• carlos.sierra.usa@gmail.com
– Questions and follow-up
• SQL Tuning is more about understanding
what your eyes see at first

More Related Content

PDF
An Approach to Sql tuning - Part 1
PPTX
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
PPTX
Database Fundamental Concepts- Series 1 - Performance Analysis
PPTX
Adapting and adopting spm v04
PPTX
Embarcadero In Search of Plan Stability Part 1 Webinar Slides
PDF
Free oracle performance tools
PDF
Oracle Database : Addressing a performance issue the drilldown approach
PPT
261197832 8-performance-tuning-part i
An Approach to Sql tuning - Part 1
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Database Fundamental Concepts- Series 1 - Performance Analysis
Adapting and adopting spm v04
Embarcadero In Search of Plan Stability Part 1 Webinar Slides
Free oracle performance tools
Oracle Database : Addressing a performance issue the drilldown approach
261197832 8-performance-tuning-part i

Similar to sqltuning101-170419021007-2.pdf (20)

PDF
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
PDF
Breaking data
PDF
Winning performance challenges in oracle standard editions
PDF
Collaborate 2019 - How to Understand an AWR Report
PPTX
Oracle Database Performance Tuning Basics
PDF
Winning performance challenges in oracle standard editions
PDF
Performance Stability, Tips and Tricks and Underscores
PDF
Oracle Performance Tuning Fundamentals
PPTX
Presentación Oracle Database Migración consideraciones 10g/11g/12c
PDF
In Search of Plan Stability - Part 1
PPTX
Understanding SQL Trace, TKPROF and Execution Plan for beginners
PDF
collab2011-tuning-ebusiness-421966.pdf
PDF
Getting optimal performance from oracle e business suite
PDF
Getting optimal performance from oracle e business suite(aioug aug2015)
PDF
A data driven etl test framework sqlsat madison
PPT
Collaborate 2011-tuning-ebusiness-416502
PPTX
PPTX
Oracle OpenWorld 2016 Review - Focus on Data, BigData, Streaming Data, Machin...
PPTX
AWS Redshift Introduction - Big Data Analytics
PPTX
T sql performance guidelines for better db stress powers
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Breaking data
Winning performance challenges in oracle standard editions
Collaborate 2019 - How to Understand an AWR Report
Oracle Database Performance Tuning Basics
Winning performance challenges in oracle standard editions
Performance Stability, Tips and Tricks and Underscores
Oracle Performance Tuning Fundamentals
Presentación Oracle Database Migración consideraciones 10g/11g/12c
In Search of Plan Stability - Part 1
Understanding SQL Trace, TKPROF and Execution Plan for beginners
collab2011-tuning-ebusiness-421966.pdf
Getting optimal performance from oracle e business suite
Getting optimal performance from oracle e business suite(aioug aug2015)
A data driven etl test framework sqlsat madison
Collaborate 2011-tuning-ebusiness-416502
Oracle OpenWorld 2016 Review - Focus on Data, BigData, Streaming Data, Machin...
AWS Redshift Introduction - Big Data Analytics
T sql performance guidelines for better db stress powers
Ad

More from TricantinoLopezPerez (14)

PDF
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
PDF
2020-TipsAndTrickssssasdfasdfasdfasdfasdf.pdf
PDF
33-ORAWORLDasdfasdfas asdfasdfasdfa asd.pdf
PDF
con9578-2088758.pdf
PDF
linux-memory-explained.pdf
PPT
Oracle Wait Events That Everyone Should Know.ppt
PDF
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
PDF
exadata-database-machine-kpis-3224944.pdf
DOC
TA110_System_Capacity_Plan.doc
PDF
kscope2013vst-130627142814-phpapp02.pdf
PDF
TGorman Collab16 UnixTools 20160411.pdf
PPT
BrownbagIntrotosqltuning.ppt
PPT
sqltuningcardinality1(1).ppt
PDF
ash-1-130820122404-phpapp01.pdf
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
2020-TipsAndTrickssssasdfasdfasdfasdfasdf.pdf
33-ORAWORLDasdfasdfas asdfasdfasdfa asd.pdf
con9578-2088758.pdf
linux-memory-explained.pdf
Oracle Wait Events That Everyone Should Know.ppt
pdfslide.net_em12c-capacity-planning-with-oem-metrics_2.pdf
exadata-database-machine-kpis-3224944.pdf
TA110_System_Capacity_Plan.doc
kscope2013vst-130627142814-phpapp02.pdf
TGorman Collab16 UnixTools 20160411.pdf
BrownbagIntrotosqltuning.ppt
sqltuningcardinality1(1).ppt
ash-1-130820122404-phpapp01.pdf
Ad

Recently uploaded (20)

PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
1_Introduction to advance data techniques.pptx
PDF
Fluorescence-microscope_Botany_detailed content
PPT
Quality review (1)_presentation of this 21
PPTX
Major-Components-ofNKJNNKNKNKNKronment.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
Foundation of Data Science unit number two notes
PDF
Launch Your Data Science Career in Kochi – 2025
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
PPTX
Introduction to Knowledge Engineering Part 1
PDF
.pdf is not working space design for the following data for the following dat...
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
Clinical guidelines as a resource for EBP(1).pdf
1_Introduction to advance data techniques.pptx
Fluorescence-microscope_Botany_detailed content
Quality review (1)_presentation of this 21
Major-Components-ofNKJNNKNKNKNKronment.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Foundation of Data Science unit number two notes
Launch Your Data Science Career in Kochi – 2025
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
climate analysis of Dhaka ,Banglades.pptx
Supervised vs unsupervised machine learning algorithms
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
Introduction to Knowledge Engineering Part 1
.pdf is not working space design for the following data for the following dat...
Reliability_Chapter_ presentation 1221.5784
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx

sqltuning101-170419021007-2.pdf

  • 1. SQL Tuning 101 The three pillars of SQL Tuning: Diagnostics Collection, Root Cause Analysis and Remediation Carlos Sierra
  • 2. Agenda • Motivation • Methodology – Diagnostics Collection – Root Cause Analysis – Remediation • Some Important Topics – Execution Plan – Cost-based Optimizer – Plan Stability • Free Tools
  • 3. Motivation • One bad SQL can degrade overall DB performance! • SQL Tuning is: – A Complex Task – Part Science and part Art – The Responsibility of WHO? • DBA? • Developer? • Oracle? • Application Vendor?
  • 4. Methodology The three pillars of SQL Tuning 1. Good Practices 2. Diagnostics Collection 3. Root Cause Analysis (RCA) 4. Remediation
  • 5. I. Good Practices • The forgotten pillar of SQL Tuning • Quid pro Quo (e.g. This for That or Give and Take) – Be nice to the CBO and the CBO will be nice to you • A healthy environment requires less SQL Tuning • A win-win! • Except for Consultants like myself!
  • 6. Some Good Practices • Start with a modern and solid database release • Reset all CBO related parameters (and some more) • Gather CBO Statistics as per Application or Product – Automatic task does a decent job on 11.2.0.4+ • Write reasonable SQL – Use the product as designed and avoid weird SQL – If SQL is too long or too complex the CBO will struggle
  • 7. Partial list of Parameters to Reset • optimizer_features_enable • optimizer_index_cost_adj • optimizer_index_caching • db_file_multiblock_read_count (not CBO exclusive) • cursor_sharing (not an optimizer parameter) • optimizer_dynamic_sampling • optimizer_mode
  • 8. II. Diagnostics Collection a) Identify SQL_ID b) Use Oracle-product base Tools c) Supplement with specialized Tools
  • 9. II.a Identify SQL_ID • Oracle Enterprise Manager (OEM) – [ OEM] [ Database | Grid | Cloud ] Control • Automatic Workload Repository (AWR) • Statspack (SP) • SQL Trace (and TKPROF) • V$ dynamic views (e.g. V$SQL and V$SESSION)
  • 10. II.b Oracle-product base Tools • SQL Trace (and TKPROF) • Active Session History (ASH) • EXPLAIN PLAN FOR • AUTOTRACE • SQL Developer • DBMS_XPLAN • SQL Monitor
  • 11. SQL Trace • ALTER SESSION SET – SQL_TRACE = [ TRUE | FALSE ] – EVENTS ‘10046 trace name context forever, level N’ • N = 1: all executions (10g-) or 1st execution (11g+) • N = 4: Binds • N = 8: Waits • N = 12 = 4 + 8 • N = 16: each execution (11g+) • N = 64: 1st + each where DB time > 1min (11.2.0.2+)
  • 12. Where is my Trace? • V$DIAG_INFO (11.1+) • USER_DUMP_DEST (deprecated 12c)
  • 13. TKPROF: SQL Text and DB Calls
  • 14. TKPROF: Execution Plan and Wait Events
  • 15. Active Session History (ASH) • 1 sec snapshots of V$SESSION • Every 10 ASH samples into AWR (10 sec granularity) • Sessions “ON CPU” or “WAITING” (non-idle) • Multiple Dimensions • Excellent repository for performance data mining • Requires Oracle Diagnostics Pack!
  • 16. SQL Trace vs. ASH Pros • Precise • Free Pros • Always available • Multi-dimensional repository • Plan line granularity (11g+) Cons • Re-execute transaction • Waits per cursor – No plan line granularity Cons • Requires Diagnostics Pack
  • 17. EXPLAIN PLAN FOR • Actual SQL is not executed • Blind to Bind Peeking, thus unreliable with Binds
  • 19. SET AUTOTRACE ON • Actual SQL is executed • Includes small subset of Session Statistics
  • 20. Query Output and Execution Plan
  • 21. Predicate Information and Session Stats
  • 24. DBMS_XPLAN • DISPLAY (from PLAN_TABLE) • DISPLAY_CURSOR • DISPLAY_AWR • DISPLAY_SQL_PLAN_BASELINE • DISPLAY_SQL_SET • SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY%());
  • 25. DBMS_XPLAN.DISPLAY_CURSOR • SQL_ID • CURSOR_CHILD_NO (defaults to 0!) • FORMAT – TYPICAL = DEFAULT – ALL = TYPICAL + QB + PROJECTION + ALIAS + REMOTE – ADVANCED = ALL + OUTLINE + BINDS – ALLSTATS = IOSTATS + MEMSTATS (all executions) – ALLSTATS LAST (last execution) – ADAPTIVE (12c)
  • 26. Sample query with hints and binds
  • 28. SQL Monitor • Part of the Oracle Tuning Pack • Tabular and Graphical Execution Plan • Timeline per Plan Line • Binds and Predicates • Nice and intuitive • Executed from OEM or SQL*Plus
  • 31. II.c Specialized Tools • SQLTXPLAIN (a.k.a. SQLT) – My Oracle Support (MOS) 215187.1 • SQLHC – MOS 1366133.1 • SQLd360 – By Mauro Pagano • Others?
  • 32. III. Root Cause Analysis • This is where you want to spend your time! • Go over all the output of your specialized tool • Look for correlations and possible causation • Put yourself in the shoes of the Optimizer • Question everything (is there a better way?) • Ask for help after you have done your homework • Enjoy the ride!
  • 33. Typical Steps 1. Find where the time is spent (plan line) – Too many executions or too much time per exec? 2. Validate estimated rows versus actual rows – If off determine why (deficient statistics maybe?) 3. What is the history of this SQL? – Can you find a better performing plan? 4. Can you make perfect sense of the SQL and plan?
  • 34. IV. Remediation • Explore multiple possibilities – And try to prove yourself wrong! • Implement the smallest-scope fix – Avoid system-wide changes unless you are rock solid! • Avoid guesses and assumptions (piñata method) – Too many people out there are “tuning” blind-folded! • Trust nobody! (be skeptic and test thoroughly)
  • 35. Three Important Topics 1. Execution Plan 2. Cost-based Optimizer 3. Plan Stability
  • 36. 1. Execution Plan • If you do not understand the SQL or the Execution Plan stop right there! • From SQL text you can draw a diagram if that helps • To understand the Execution Plan start small – Review a two-tables query and review plan – Move to a three-tables query and review plan – Introduce subqueries and more complex constructions
  • 37. 2. Cost-based Optimizer • CBO was released on 7.3 (circa February 1996) • A large and complex piece of software enhanced for two decades • By design the CBO produces an optimal execution plan according to some representation of the data – This means: execution plans are expected to change! – Breathe deep and embrace the change!
  • 38. 3. Plan Stability • Most DBAs hate Execution Plan changes – They never hear of changes for the better! • To remediate undesired plan changes we do have several Plan Stability techniques – SQL Plan Management (Baselines) from 11g – SQL Profiles from 10g – Deprecated Stored Outlines from 9i – And then some others (SQL Patches and CBO Hints)
  • 39. Popular Free Diagnostics Tools • SQLTXPLAIN • SQLHC • SQLd360 • Stand-alone scripts
  • 46. SQLT XPLAIN vs. SQLd360 Pros • Oracle Support Standard • Free (requires MOS account) • Comprehensive and mature Pros • Free Software (no strings) • Installs nothing on database • Graphical visualization of data Cons • Requires installation • HTML Tables (no charts) Cons • Still young (frequent releases)
  • 47. SQL Tuning Road Map 1. Start with a clean and healthy environment 2. Identify SQL_ID 3. Collect diagnostics using comprehensive tools 4. Spend time analyzing diagnostics collected 5. Determine root cause 6. Explore remediation actions 7. Test and implement solution
  • 49. The End • http://carlos-sierra.net/ – Blog and downloads • carlos.sierra.usa@gmail.com – Questions and follow-up • SQL Tuning is more about understanding what your eyes see at first