SlideShare a Scribd company logo
1 of 49
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
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
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
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
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
SQLd360 Plan Tree
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

What's hot

Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Aaron Shilo
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsJohn Beresniewicz
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the TradeCarlos Sierra
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsZohar Elkayam
 
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsYour tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsJohn Kanagaraj
 
Troubleshooting Complex Oracle Performance Problems with Tanel Poder
Troubleshooting Complex Oracle Performance Problems with Tanel PoderTroubleshooting Complex Oracle Performance Problems with Tanel Poder
Troubleshooting Complex Oracle Performance Problems with Tanel PoderTanel Poder
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsCarlos Sierra
 
Introducing the eDB360 Tool
Introducing the eDB360 ToolIntroducing the eDB360 Tool
Introducing the eDB360 ToolCarlos Sierra
 
Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder Oracle Scripts and Tools (2010)Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder Oracle Scripts and Tools (2010)Tanel Poder
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperJeff Smith
 
SQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cSQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cTanel Poder
 
Oracle statistics by example
Oracle statistics by exampleOracle statistics by example
Oracle statistics by exampleMauro Pagano
 
MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and EngineAbdul Manaf
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder
 
Oracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention TroubleshootingOracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention TroubleshootingTanel Poder
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresJitendra Singh
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performanceMauro Pagano
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Kyle Hailey
 

What's hot (20)

Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
 
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsYour tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
 
AWR and ASH Deep Dive
AWR and ASH Deep DiveAWR and ASH Deep Dive
AWR and ASH Deep Dive
 
Troubleshooting Complex Oracle Performance Problems with Tanel Poder
Troubleshooting Complex Oracle Performance Problems with Tanel PoderTroubleshooting Complex Oracle Performance Problems with Tanel Poder
Troubleshooting Complex Oracle Performance Problems with Tanel Poder
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
Introducing the eDB360 Tool
Introducing the eDB360 ToolIntroducing the eDB360 Tool
Introducing the eDB360 Tool
 
Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder Oracle Scripts and Tools (2010)Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder Oracle Scripts and Tools (2010)
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
 
SQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cSQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12c
 
Oracle statistics by example
Oracle statistics by exampleOracle statistics by example
Oracle statistics by example
 
MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and Engine
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata Migrations
 
Oracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention TroubleshootingOracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention Troubleshooting
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and Underscores
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle
 

Similar to SQL Tuning 101

An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1Navneet Upneja
 
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse SupportOracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse Supportnkarag
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDatabase Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDAGEOP LTD
 
Embarcadero In Search of Plan Stability Part 1 Webinar Slides
Embarcadero In Search of Plan Stability Part 1 Webinar SlidesEmbarcadero In Search of Plan Stability Part 1 Webinar Slides
Embarcadero In Search of Plan Stability Part 1 Webinar SlidesEmbarcadero Technologies
 
Oracle Database : Addressing a performance issue the drilldown approach
Oracle Database : Addressing a performance issue the drilldown approachOracle Database : Addressing a performance issue the drilldown approach
Oracle Database : Addressing a performance issue the drilldown approachLaurent Leturgez
 
261197832 8-performance-tuning-part i
261197832 8-performance-tuning-part i261197832 8-performance-tuning-part i
261197832 8-performance-tuning-part iNaviSoft
 
Winning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editionsWinning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editionsPini Dibask
 
Collaborate 2019 - How to Understand an AWR Report
Collaborate 2019 - How to Understand an AWR ReportCollaborate 2019 - How to Understand an AWR Report
Collaborate 2019 - How to Understand an AWR ReportAlfredo Krieg
 
Oracle Database Performance Tuning Basics
Oracle Database Performance Tuning BasicsOracle Database Performance Tuning Basics
Oracle Database Performance Tuning Basicsnitin anjankar
 
Winning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editionsWinning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editionsPini Dibask
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cRonald Francisco Vargas Quesada
 
In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1Enkitec
 
collab2011-tuning-ebusiness-421966.pdf
collab2011-tuning-ebusiness-421966.pdfcollab2011-tuning-ebusiness-421966.pdf
collab2011-tuning-ebusiness-421966.pdfElboulmaniMohamed
 
Getting optimal performance from oracle e business suite
Getting optimal performance from oracle e business suiteGetting optimal performance from oracle e business suite
Getting optimal performance from oracle e business suiteaioughydchapter
 
Getting optimal performance from oracle e business suite(aioug aug2015)
Getting optimal performance from oracle e business suite(aioug aug2015)Getting optimal performance from oracle e business suite(aioug aug2015)
Getting optimal performance from oracle e business suite(aioug aug2015)pasalapudi123
 
A data driven etl test framework sqlsat madison
A data driven etl test framework sqlsat madisonA data driven etl test framework sqlsat madison
A data driven etl test framework sqlsat madisonTerry Bunio
 
Collaborate 2011-tuning-ebusiness-416502
Collaborate 2011-tuning-ebusiness-416502Collaborate 2011-tuning-ebusiness-416502
Collaborate 2011-tuning-ebusiness-416502kaziul Islam Bulbul
 

Similar to SQL Tuning 101 (20)

An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1
 
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse SupportOracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDatabase Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance Analysis
 
Embarcadero In Search of Plan Stability Part 1 Webinar Slides
Embarcadero In Search of Plan Stability Part 1 Webinar SlidesEmbarcadero In Search of Plan Stability Part 1 Webinar Slides
Embarcadero In Search of Plan Stability Part 1 Webinar Slides
 
Free oracle performance tools
Free oracle performance toolsFree oracle performance tools
Free oracle performance tools
 
Oracle Database : Addressing a performance issue the drilldown approach
Oracle Database : Addressing a performance issue the drilldown approachOracle Database : Addressing a performance issue the drilldown approach
Oracle Database : Addressing a performance issue the drilldown approach
 
261197832 8-performance-tuning-part i
261197832 8-performance-tuning-part i261197832 8-performance-tuning-part i
261197832 8-performance-tuning-part i
 
Breaking data
Breaking dataBreaking data
Breaking data
 
Winning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editionsWinning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editions
 
Collaborate 2019 - How to Understand an AWR Report
Collaborate 2019 - How to Understand an AWR ReportCollaborate 2019 - How to Understand an AWR Report
Collaborate 2019 - How to Understand an AWR Report
 
Oracle Database Performance Tuning Basics
Oracle Database Performance Tuning BasicsOracle Database Performance Tuning Basics
Oracle Database Performance Tuning Basics
 
Winning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editionsWinning performance challenges in oracle standard editions
Winning performance challenges in oracle standard editions
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
 
In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1
 
collab2011-tuning-ebusiness-421966.pdf
collab2011-tuning-ebusiness-421966.pdfcollab2011-tuning-ebusiness-421966.pdf
collab2011-tuning-ebusiness-421966.pdf
 
Getting optimal performance from oracle e business suite
Getting optimal performance from oracle e business suiteGetting optimal performance from oracle e business suite
Getting optimal performance from oracle e business suite
 
Getting optimal performance from oracle e business suite(aioug aug2015)
Getting optimal performance from oracle e business suite(aioug aug2015)Getting optimal performance from oracle e business suite(aioug aug2015)
Getting optimal performance from oracle e business suite(aioug aug2015)
 
A data driven etl test framework sqlsat madison
A data driven etl test framework sqlsat madisonA data driven etl test framework sqlsat madison
A data driven etl test framework sqlsat madison
 
Collaborate 2011-tuning-ebusiness-416502
Collaborate 2011-tuning-ebusiness-416502Collaborate 2011-tuning-ebusiness-416502
Collaborate 2011-tuning-ebusiness-416502
 
Oow2016 review-db-dev-bigdata-BI
Oow2016 review-db-dev-bigdata-BIOow2016 review-db-dev-bigdata-BI
Oow2016 review-db-dev-bigdata-BI
 

Recently uploaded

The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 

Recently uploaded (20)

The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 

SQL Tuning 101

  • 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
  • 18.
  • 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
  • 23.
  • 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
  • 27.
  • 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
  • 29.
  • 30.
  • 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
  • 42.
  • 44.
  • 45.
  • 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