SlideShare a Scribd company logo
1 of 31
1
References

Optimization

Types of
Optimization

Understanding
The Execution
Plan

Problem and
Solutions with
practical
examples

Query
Optimization
2
3
• RBO uses predefined set of precedence
rules(golden rules) to figure out the optimal
path
• These rules used to choose one index over
another index and when full table scan
• Oracle 9i has 20 “golden rules” for optimal
execution path
ALTER SESSION SET OPTIMIZER_MODE = RULE/CHOOSE;
4
5
6
Emp_Id

Ename

EAddr

Emp_Id

Emp_dept Emp_sal

7
Index1(A1,B,C); Table 1
Index2(A2,F,G); Table 2
Select *
From Table1,Table2
Where A1=1 and B=2 and
G=2 and F like ’%something’
and A1=A2

Two 1-Col Index: Table 1
Index1(A)
Index2(B);
One 2-Col Index: Table 2
Index3(E,F);
8
Rule-Based Optimizer Problems and Solutions

9
Problem 1: Incorrect Driving Table
SELECT COUNT(*) FROM acct a, trans b
WHERE b.cost_center = 'MASS'
AND a.acct_name = 'MGA'
AND a.acct_name = b.acct_name;
Response = 19.722 seconds

SELECT COUNT(*) FROM trans b, acct a
WHERE b.cost_center= 'MASS'
AND a.acct_name = 'MGA'
AND a.acct_name = b.acct_name;
Response = 1.904 seconds
10
Problem 2: Incorrect Driving Index

SELECT COUNT(*)
FROM trans
WHERE cost_center = 'MASS'
AND bmark_id = 9;
Response Time = 4.255 seconds

SELECT COUNT(*)
FROM trans
WHERE bmark_id = 9
AND cost_center = 'MASS';
Response Time = 1.044 seconds
11
Problem 3: Using the ORDER BY Index and not the WHERE Index

SELECT fod_flag, account_no
FROM account_master
WHERE (account_code like 'I%')
ORDER BY account_no;
Index_1 UNIQUE (ACCOUNT_NO)
Index_2 (ACCOUNT_CODE)

Index (ACCOUNT_CODE, ACCOUNT_NO)

12
ANALYZE & DBMS_STATS

ANALYZE

Collect statistics about tables, clusters
and indexes, and store those statistics
in the data dictionary

DBMS_STATS
RULE BASED

RULE BASED

If table not analyzed CBO apply
Rule Based Logic to select best path

13
• ANALYZE COMPUTE & ANALYZE ESTIMATE

• Table and it’s index
• Index dropped..?

• DBMS_STATS.GATHER_SCHEMA_STATS &
GATHER_TABLE_STATS : CASCADE=>TRUE

14
1. ANALYZE TABLE EMP ESTIMATE STATISTICS
SAMPLE 5 PERCENT FOR ALL INDEXED COLUMNS;
2. ANALYZE INDEX EMP_NDX1 ESTIMATE
STATISTICS SAMPLE 5 PERCENT FOR ALL INDEXED
COLUMNS;
3. ANALYZE TABLE EMP COMPUTE STATISTICS FOR
ALL INDEXED COLUMNS;
4. ANALYZE TABLE EMP DELETE STATISTICS;
15
• Check Syntax
• Object Privilege

Parse

Plans
• All possible
execution plans
listed

• Calculate cost
of each
execution plans

Cost
16
Cost-Based Optimizer Problems and Solutions

17
Problem 1: The Skewness Problem

SELECT acct_no, customer, product, trans_date, amt
FROM trans
WHERE status='O';
Response time = 16.308 seconds
> ANALYZE TABLE TRANS COMPUTE STATISTICS FOR ALL
INDEXED COLUMNS
Response time reduces to 0.259 seconds
18
Problem 2: Analyzing with Wrong Data

The cost-based optimizer requires accurate
information, including accurate data volumes, to have
any chance of creating efficient execution plans
otherwise performance will degrade a large scale

Example:
Before analyze the cardinality is 1000(10000/10) on
10000 rows
And now we insert the 10000 new rows with distinct
values so what will be the new cardinality?

19
Problem 4: Choosing an Inferior Index

where business_unit = :5
and ledger = :6
and fiscal_year = :7
and accounting_period = :8
and affiliate = :9
and statistics_code = :10
and project_id = :11
and account = :12
and currency_cd = :13
and deptid = :14
and product = :15
20
Problem 4: Choosing an Inferior Index

where business_unit = :5
and ledger = :6
and fiscal_year = :7
and accounting_period between 1 and 12
and affiliate = :9
and statistics_code = :10
and project_id = :11
and account = :12
and currency_cd = :13
and deptid = :14
and product = :15
21
Problem 4: Choosing an Inferior Index

business_unit
ledger
fiscal_year
affiliate
statistics_code
project_id
account
currency_cd
deptid
Product
accounting_period
22
SELECT prod_category, AVG(amount_sold)
FROM o_sales s, o_products p
WHERE p.prod_id = s.prod_id
GROUP BY prod_category;

GET SALES

GET
PRODUCTS

Apply Join

Apply
Group
BY(Sort)

Return
Result
Interpreting Execution plan

Level 3

GROUP BY

Level 2

JOIN

Level 1

O_SALES

O_PRODUCT
Understanding the Execution Plans
Join Order
Join
Method

Join Type
Access
Method
Partition
Pruning

Cardinality

Parallel
Execution
Cardinality
1. Estimate no of rows coming out of the operation
2. Complex formula used for this calculations
3. Incorrect Cardinality

Total Rows: 18
Different val: 3
Access Method
Table

Index

Join

Full Table
Scan

Index
Unique Scan

Hash Joins

Row ID

Index Range
Scan

Nested
Loops joins

Index Skip
Scan

Sort Merge
Joins

Full Index
Scan
Fast Full
Index Scan
Bitmap
Index
Hints can be placed in SQL to force optimizer to follow our desired
retrieval path rather then calculated by the optimizer
Select /* +RULE */
From emp , dept
Where…..
Select statement instructs the optimizer to use the rule based
optimizer rather than the cost based optimizer.
Delete /*+RULE*/ . . . . . . . .
Update /*+RULE*/ . . . . . . . .

29
1. Data Warehousing using Oracle
Dr. P.S.Deshpande

2. The oracle optimizer explain the explain plan
Oracle.com
3. Oracle SQL Tuning Pocket Reference
Mark Gurry

30
31

More Related Content

What's hot

Arm developement
Arm developementArm developement
Arm developementhirokiht
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationKarthik Vivek
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languagesAnkit Pandey
 
Data Structure Assignment help , Data Structure Online tutors
Data Structure Assignment help , Data Structure Online tutorsData Structure Assignment help , Data Structure Online tutors
Data Structure Assignment help , Data Structure Online tutorsjohn mayer
 
Mapreduce total order sorting technique
Mapreduce total order sorting techniqueMapreduce total order sorting technique
Mapreduce total order sorting techniqueUday Vakalapudi
 
Parallel Algorithms- Sorting and Graph
Parallel Algorithms- Sorting and GraphParallel Algorithms- Sorting and Graph
Parallel Algorithms- Sorting and GraphDr Shashikant Athawale
 

What's hot (20)

sCode optimization
sCode optimizationsCode optimization
sCode optimization
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
Lesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
 
Analysis algorithm
Analysis algorithmAnalysis algorithm
Analysis algorithm
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
 
Arm developement
Arm developementArm developement
Arm developement
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
Unit 3 part2
Unit 3 part2Unit 3 part2
Unit 3 part2
 
Lesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving processLesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving process
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Project
ProjectProject
Project
 
Memoization & HOF
Memoization & HOFMemoization & HOF
Memoization & HOF
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Data Structure Assignment help , Data Structure Online tutors
Data Structure Assignment help , Data Structure Online tutorsData Structure Assignment help , Data Structure Online tutors
Data Structure Assignment help , Data Structure Online tutors
 
Mapreduce total order sorting technique
Mapreduce total order sorting techniqueMapreduce total order sorting technique
Mapreduce total order sorting technique
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Parallel Algorithms- Sorting and Graph
Parallel Algorithms- Sorting and GraphParallel Algorithms- Sorting and Graph
Parallel Algorithms- Sorting and Graph
 

Viewers also liked

U-SQL Query Execution and Performance Basics (SQLBits 2016)
U-SQL Query Execution and Performance Basics (SQLBits 2016)U-SQL Query Execution and Performance Basics (SQLBits 2016)
U-SQL Query Execution and Performance Basics (SQLBits 2016)Michael Rys
 
DB2 Performance Tuning Z/OS - email me please for more details
DB2 Performance Tuning Z/OS - email me please for more detailsDB2 Performance Tuning Z/OS - email me please for more details
DB2 Performance Tuning Z/OS - email me please for more detailsManikandan Suresh
 
SQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghSQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghRishu Mehra
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statementsxKinAnx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
14. Query Optimization in DBMS
14. Query Optimization in DBMS14. Query Optimization in DBMS
14. Query Optimization in DBMSkoolkampus
 

Viewers also liked (8)

U-SQL Query Execution and Performance Basics (SQLBits 2016)
U-SQL Query Execution and Performance Basics (SQLBits 2016)U-SQL Query Execution and Performance Basics (SQLBits 2016)
U-SQL Query Execution and Performance Basics (SQLBits 2016)
 
DB2 Performance Tuning Z/OS - email me please for more details
DB2 Performance Tuning Z/OS - email me please for more detailsDB2 Performance Tuning Z/OS - email me please for more details
DB2 Performance Tuning Z/OS - email me please for more details
 
SQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghSQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet Singh
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statements
 
Query execution
Query executionQuery execution
Query execution
 
Sql injection
Sql injectionSql injection
Sql injection
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
14. Query Optimization in DBMS
14. Query Optimization in DBMS14. Query Optimization in DBMS
14. Query Optimization in DBMS
 

Similar to Query Optimization Techniques and Understanding Execution Plans

MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewOlav Sandstå
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain PlanMaria Colgan
 
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems gamemaker762
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql executionxKinAnx
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0PMILebanonChapter
 
Chapter 6-Scheduling in Industrial of Engineering
Chapter 6-Scheduling in Industrial of EngineeringChapter 6-Scheduling in Industrial of Engineering
Chapter 6-Scheduling in Industrial of EngineeringFaizAzmy2
 
Explaining the explain_plan
Explaining the explain_planExplaining the explain_plan
Explaining the explain_planarief12H
 
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxMSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxgilpinleeanna
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planMaria Colgan
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewOlav Sandstå
 
Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)Geoffrey De Smet
 
Twp optimizer-with-oracledb-12c-1963236
Twp optimizer-with-oracledb-12c-1963236Twp optimizer-with-oracledb-12c-1963236
Twp optimizer-with-oracledb-12c-1963236Santosh Kumar
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptxTadiwaMawere
 
Adaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAdaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAnju Garg
 
CSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsCSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsYhal Htet Aung
 

Similar to Query Optimization Techniques and Understanding Execution Plans (20)

Part5 sql tune
Part5 sql tunePart5 sql tune
Part5 sql tune
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql execution
 
PLSQL Practices
PLSQL PracticesPLSQL Practices
PLSQL Practices
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0
 
Chapter 6-Scheduling in Industrial of Engineering
Chapter 6-Scheduling in Industrial of EngineeringChapter 6-Scheduling in Industrial of Engineering
Chapter 6-Scheduling in Industrial of Engineering
 
Explaining the explain_plan
Explaining the explain_planExplaining the explain_plan
Explaining the explain_plan
 
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docxMSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
MSCD650 Final Exam feedback FormMSCD650 Final Exam Grading For.docx
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)
 
Twp optimizer-with-oracledb-12c-1963236
Twp optimizer-with-oracledb-12c-1963236Twp optimizer-with-oracledb-12c-1963236
Twp optimizer-with-oracledb-12c-1963236
 
Data envelopment analysis
Data envelopment analysisData envelopment analysis
Data envelopment analysis
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptx
 
Adaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAdaptive Query Optimization in 12c
Adaptive Query Optimization in 12c
 
Cpk problem solving_pcba smt machine
Cpk problem solving_pcba smt machineCpk problem solving_pcba smt machine
Cpk problem solving_pcba smt machine
 
CSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsCSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow Charts
 

Recently uploaded

Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Recently uploaded (20)

Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 

Query Optimization Techniques and Understanding Execution Plans

  • 1. 1
  • 2. References Optimization Types of Optimization Understanding The Execution Plan Problem and Solutions with practical examples Query Optimization 2
  • 3. 3
  • 4. • RBO uses predefined set of precedence rules(golden rules) to figure out the optimal path • These rules used to choose one index over another index and when full table scan • Oracle 9i has 20 “golden rules” for optimal execution path ALTER SESSION SET OPTIMIZER_MODE = RULE/CHOOSE; 4
  • 5. 5
  • 6. 6
  • 8. Index1(A1,B,C); Table 1 Index2(A2,F,G); Table 2 Select * From Table1,Table2 Where A1=1 and B=2 and G=2 and F like ’%something’ and A1=A2 Two 1-Col Index: Table 1 Index1(A) Index2(B); One 2-Col Index: Table 2 Index3(E,F); 8
  • 10. Problem 1: Incorrect Driving Table SELECT COUNT(*) FROM acct a, trans b WHERE b.cost_center = 'MASS' AND a.acct_name = 'MGA' AND a.acct_name = b.acct_name; Response = 19.722 seconds SELECT COUNT(*) FROM trans b, acct a WHERE b.cost_center= 'MASS' AND a.acct_name = 'MGA' AND a.acct_name = b.acct_name; Response = 1.904 seconds 10
  • 11. Problem 2: Incorrect Driving Index SELECT COUNT(*) FROM trans WHERE cost_center = 'MASS' AND bmark_id = 9; Response Time = 4.255 seconds SELECT COUNT(*) FROM trans WHERE bmark_id = 9 AND cost_center = 'MASS'; Response Time = 1.044 seconds 11
  • 12. Problem 3: Using the ORDER BY Index and not the WHERE Index SELECT fod_flag, account_no FROM account_master WHERE (account_code like 'I%') ORDER BY account_no; Index_1 UNIQUE (ACCOUNT_NO) Index_2 (ACCOUNT_CODE) Index (ACCOUNT_CODE, ACCOUNT_NO) 12
  • 13. ANALYZE & DBMS_STATS ANALYZE Collect statistics about tables, clusters and indexes, and store those statistics in the data dictionary DBMS_STATS RULE BASED RULE BASED If table not analyzed CBO apply Rule Based Logic to select best path 13
  • 14. • ANALYZE COMPUTE & ANALYZE ESTIMATE • Table and it’s index • Index dropped..? • DBMS_STATS.GATHER_SCHEMA_STATS & GATHER_TABLE_STATS : CASCADE=>TRUE 14
  • 15. 1. ANALYZE TABLE EMP ESTIMATE STATISTICS SAMPLE 5 PERCENT FOR ALL INDEXED COLUMNS; 2. ANALYZE INDEX EMP_NDX1 ESTIMATE STATISTICS SAMPLE 5 PERCENT FOR ALL INDEXED COLUMNS; 3. ANALYZE TABLE EMP COMPUTE STATISTICS FOR ALL INDEXED COLUMNS; 4. ANALYZE TABLE EMP DELETE STATISTICS; 15
  • 16. • Check Syntax • Object Privilege Parse Plans • All possible execution plans listed • Calculate cost of each execution plans Cost 16
  • 17. Cost-Based Optimizer Problems and Solutions 17
  • 18. Problem 1: The Skewness Problem SELECT acct_no, customer, product, trans_date, amt FROM trans WHERE status='O'; Response time = 16.308 seconds > ANALYZE TABLE TRANS COMPUTE STATISTICS FOR ALL INDEXED COLUMNS Response time reduces to 0.259 seconds 18
  • 19. Problem 2: Analyzing with Wrong Data The cost-based optimizer requires accurate information, including accurate data volumes, to have any chance of creating efficient execution plans otherwise performance will degrade a large scale Example: Before analyze the cardinality is 1000(10000/10) on 10000 rows And now we insert the 10000 new rows with distinct values so what will be the new cardinality? 19
  • 20. Problem 4: Choosing an Inferior Index where business_unit = :5 and ledger = :6 and fiscal_year = :7 and accounting_period = :8 and affiliate = :9 and statistics_code = :10 and project_id = :11 and account = :12 and currency_cd = :13 and deptid = :14 and product = :15 20
  • 21. Problem 4: Choosing an Inferior Index where business_unit = :5 and ledger = :6 and fiscal_year = :7 and accounting_period between 1 and 12 and affiliate = :9 and statistics_code = :10 and project_id = :11 and account = :12 and currency_cd = :13 and deptid = :14 and product = :15 21
  • 22. Problem 4: Choosing an Inferior Index business_unit ledger fiscal_year affiliate statistics_code project_id account currency_cd deptid Product accounting_period 22
  • 23.
  • 24. SELECT prod_category, AVG(amount_sold) FROM o_sales s, o_products p WHERE p.prod_id = s.prod_id GROUP BY prod_category; GET SALES GET PRODUCTS Apply Join Apply Group BY(Sort) Return Result
  • 25. Interpreting Execution plan Level 3 GROUP BY Level 2 JOIN Level 1 O_SALES O_PRODUCT
  • 26. Understanding the Execution Plans Join Order Join Method Join Type Access Method Partition Pruning Cardinality Parallel Execution
  • 27. Cardinality 1. Estimate no of rows coming out of the operation 2. Complex formula used for this calculations 3. Incorrect Cardinality Total Rows: 18 Different val: 3
  • 28. Access Method Table Index Join Full Table Scan Index Unique Scan Hash Joins Row ID Index Range Scan Nested Loops joins Index Skip Scan Sort Merge Joins Full Index Scan Fast Full Index Scan Bitmap Index
  • 29. Hints can be placed in SQL to force optimizer to follow our desired retrieval path rather then calculated by the optimizer Select /* +RULE */ From emp , dept Where….. Select statement instructs the optimizer to use the rule based optimizer rather than the cost based optimizer. Delete /*+RULE*/ . . . . . . . . Update /*+RULE*/ . . . . . . . . 29
  • 30. 1. Data Warehousing using Oracle Dr. P.S.Deshpande 2. The oracle optimizer explain the explain plan Oracle.com 3. Oracle SQL Tuning Pocket Reference Mark Gurry 30
  • 31. 31

Editor's Notes

  1. The driving table is read once and for each row in the driving table, the inner table is processed once. 
  2. When index on same no of columns and not all columns are match then rightmost table index is used
  3. ANALYZE & DBMS_STATS functions collect statistics about tables, clusters, and indexes, and store those statistics in the data dictionary.
  4. COMPUTE: ALL ROWS(Small table), ESTIMATE: SAMPLE ROWS(Large)DBMS_STATS.GATHER_SCHEMA_STATS and GATHER_TABLE_STATS analyze only tables by default, not their indexes
  5. Parameters should be considered for differentiating the good and bad execution plan
  6. Oracle uses histograms for the good cardinality prediction
  7. Index Unique Scan : Only one row will be returned by unique indexIndex Range Scan : =,<,>,LIKE (NON UNIQUE INDEX)Order BY clause has all the columns present in the index and order same as index