SlideShare a Scribd company logo
Topic: Processes in Query
Optimization
Advanced Database Management Systems
Topics to be covered
• Why Query Optimizer ?
• Cost-Based Optimization (CBO)
• Not optimized code
• Execution Plans
• Query Block
• Optimizer Components
• QueryTransformer
• Estimator
• Plan Generator
• AutomaticTuning optimizer
The query optimizer (called simply the optimizer) is built-in database
software that determines the most efficient method for a SQL statement
to access requested data.
Introduction
SQL Server can efficiently filter a data set using indexes via the WHERE
clause or any combination of filters that are separated by an AND
operator. By being exclusive, these operations take data and slice it into
progressively smaller pieces, until only our result set remains.
• Purpose of the Query Optimizer
• Cost-Based Optimization
• Execution Plans
WHY Query Optimizer ?
• The optimizer choose the best plan with the lowest cost
among all considered candidate plans.
• The optimizer uses available statistics to calculate cost.
• For a specific query in a given environment, the cost
computation accounts for factors of query execution such
as I/O, CPU, and communication.
Cost-Based Optimization (CBO)
• The optimizer is free to merge, reorganize, and process in any
order.
• The optimizer determines the optimal plan for a SQL statement by
examining multiple access methods, such as full table scan or
index scans, different join methods such as nested loops and hash
joins, different join orders, and possible transformations.
Cost-Based Optimization
Cost-Based Optimization
SELECT prod_category, AVG(amount_sold)
FROM sales s, products p WHERE
p.prod_id = s.prod_id GROUP BY prod_category
Example
SELECT DISTINCT
PRODUCT.ProductID,
PRODUCT.Name
FROM Production.Product PRODUCT
INNER JOIN Sales.SalesOrderDetail DETAIL
ON PRODUCT.ProductID = DETAIL.ProductID
OR PRODUCT.rowguid = DETAIL.rowguid;
Our expectation would be a table scan on
Product and a table scan on
SalesOrderDetail. Expensive.
Product : 504 rows
SalesOrderDetails : 121317
A singleOr can make
performance worse
Example – Optimized Code
SELECT
PRODUCT.ProductID,
PRODUCT.Name
FROM Production.Product PRODUCT
INNER JOIN Sales.SalesOrderDetail DETAIL
ON PRODUCT.ProductID =
DETAIL.ProductID
UNION
SELECT
PRODUCT.ProductID,
PRODUCT.Name
FROM Production.Product PRODUCT
INNER JOIN Sales.SalesOrderDetail DETAIL
ON PRODUCT.rowguid = DETAIL.rowguid
SELECT DISTINCT
PRODUCT.ProductID,
PRODUCT.Name
FROM Production.Product PRODUCT
INNER JOIN Sales.SalesOrderDetail DETAIL
ON PRODUCT.ProductID = DETAIL.ProductID
OR PRODUCT.rowguid = DETAIL.rowguid;
Execution Plans
In the following graphic, the optimizer generates two possible execution plans for
an input SQL statement, uses statistics to estimate their costs, compares their
costs, and then chooses the plan with the lowest cost.
Execution Plans – Query Block
The input to the optimizer is a parsed representation of a SQL statement.
The following SQL statement consists of two query blocks. The subquery in
parentheses is the inner query block. The outer query block, which is the
rest of the SQL statement, retrieves names of employees in the
departments whose IDs were supplied by the subquery. The query form
determines how query blocks are interrelated.
SELECT first_name, last_name FROM hr.employeesWHERE department_id IN
(SELECT department_id FROM hr.departmentsWHERE location_id = 1800);
Query Subplans
SELECT first_name, last_name FROM hr.employeesWHERE department_id IN
(SELECT department_id FROM hr.departmentsWHERE location_id = 1800);
• The database optimizes query blocks separately from the bottom up approach.
• The database optimizes the innermost query block first and generates a subplan
for it
• The number of possible plans for a query block is proportional to the number of
objects in the FROM clause.
• This number rises exponentially with the number of objects. For example, the
possible plans for a join of five tables are significantly higher than the possible
plans for a join of two tables.
Optimizer Components
Phase Operation Description
1 Query Transformer The optimizer determines
whether it is helpful to change
the form of the query so that
the optimizer can generate a
better execution plan.
2 Estimator The optimizer estimates the
cost of each plan based on
statistics in the data dictionary.
3 Plan Generator The optimizer compares the
costs of plans and chooses the
lowest-cost plan, known as the
execution plan, to pass to the
row source generator.
Optimizer Components
Step 1. QueryTransformer
For some statements, the query transformer determines whether
it is advantageous to rewrite the original SQL statement into a
semantically equivalent SQL statement with a lower cost.
When a viable alternative exists, the database calculates the cost
of the alternatives separately and chooses the lowest-cost
alternative.
Step 2. Estimator
Step 2. Estimator
The estimator is the component of the optimizer that determines the overall cost
of a given execution plan.
The estimator uses three different measures to determine cost:
1. Selectivity
The percentage of rows in the row set that the query selects, with 0 meaning no
rows and 1 meaning all rows.
e.g. WHERE last_name LIKE 'A%'
Assume there are 150 distinct employee last names. For an equality predicate
last_name = 'Smith', selectivity is the reciprocal of the number n of distinct
values of last_name, which is suppose .006 because the query selects rows that
contain 1 out of 150 distinct values.
Step 2. Estimator
2. Cardinality
The cardinality is the number of rows returned by each operation in an execution
plan. This input, which is crucial to obtaining an optimal plan, is common to all
cost functions.
For example, user hr queries the employees table as follows:
SELECT first_name, last_name
FROM employees
WHERE salary='10200';
The employees table contains 107 rows. The current database statistics indicate
that the number of distinct values in the salary column is 58. Therefore, the
optimizer estimates the cardinality of the result set as 2, using the formula
107/58=1.84.
Step 2. Estimator
3. Cost
This measure represents units of work or resource used. The query
optimizer uses disk I/O, CPU usage, and memory usage as units of work.
The cost is an internal numeric measure that represents the estimated
resource usage for a plan.
To estimate cost, the optimizer considers factors such as the following:
• System resources, which includes estimated I/O, CPU, and memory
• Estimated number of rows returned (cardinality)
• Size of the initial data sets
• Distribution of the data
• Access structures
Step 3. Plan Generator
Step 3. Plan Generator
The plan generator explores various plans for a query block by trying
out different access paths, join methods, and join orders.
Many plans are possible because of the various combinations that the
database can use to produce the same result. The optimizer picks the
plan with the lowest cost.
The trace file shows the optimizer first trys table as the outer table in
the join. The optimizer calculates the cost for three different join
methods: nested loops join (NL), sort merge (SM), and hash join (HA).
The optimizer picks the hash join as the most efficient method:
AutomaticTuning Optimizer
The database provides the following types of optimization:
• Normal optimization
The optimizer compiles the SQL and generates an execution plan. The normal
mode generates a reasonable plan for most SQL statements. Under normal
mode, the optimizer operates with strict time constraints, usually a fraction of a
second, during which it must find an optimal plan.
• SQL Tuning Advisor optimization
When SQL Tuning Advisor invokes the optimizer, the optimizer is known as
Automatic Tuning Optimizer. In this case, the optimizer performs additional
analysis to further improve the plan produced in normal mode. The optimizer
output is not an execution plan, but a series of actions, along with their rationale
and expected benefit for producing a significantly better plan.
Some words/phrases/sentences are taken from internet
ThankYou
Ad

Recommended

PDF
Explaining the explain_plan
arief12H
 
PPTX
Ground Breakers Romania: Explain the explain_plan
Maria Colgan
 
PPTX
Part3 Explain the Explain Plan
Maria Colgan
 
PPTX
Part4 Influencing Execution Plans with Optimizer Hints
Maria Colgan
 
PPTX
Query-porcessing-& Query optimization
Saranya Natarajan
 
PPTX
Query processing and optimization (updated)
Ravinder Kamboj
 
PPTX
Query processing
Ravinder Kamboj
 
PPTX
Query optimization
Pooja Dixit
 
PPTX
Heuristic approch monika sanghani
Monika Sanghani
 
PPTX
Query optimization
Zunera Bukhari
 
PDF
Presentation top tips for getting optimal sql execution
xKinAnx
 
PPT
Query processing-and-optimization
WBUTTUTORIALS
 
PPTX
Query decomposition in data base
Salman Memon
 
PPTX
Advanced functions in PL SQL
Hosein Zare
 
PPT
Query Decomposition and data localization
Hafiz faiz
 
PPTX
Chapter 6 slides
jdpike
 
PDF
Goldilocks and the Three MySQL Queries
Dave Stokes
 
PPTX
Sql subquery
Raveena Thakur
 
PPT
Oracle SQL, PL/SQL best practices
Smitha Padmanabhan
 
PPTX
Pl sql best practices document
Ashwani Pandey
 
PPT
Sydney Oracle Meetup - indexes
paulguerin
 
PPTX
Oracle performance tuning for java developers
Saeed Shahsavan
 
PPT
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
baabtra.com - No. 1 supplier of quality freshers
 
PDF
dd presentation.pdf
AnSHiKa187943
 
PPTX
Query processing and optimization on dbms
ar1289589
 
PPT
PASS Summit 2010 Keynote David DeWitt
GraySystemsLab
 
PPTX
SQL Query Optimization: Why Is It So Hard to Get Right?
Brent Ozar
 
PPTX
Beginners guide to_optimizer
Maria Colgan
 
PDF
Managing Statistics for Optimal Query Performance
Karen Morton
 
PPT
Database performance tuning and query optimization
Usman Tariq
 

More Related Content

What's hot (15)

PPTX
Heuristic approch monika sanghani
Monika Sanghani
 
PPTX
Query optimization
Zunera Bukhari
 
PDF
Presentation top tips for getting optimal sql execution
xKinAnx
 
PPT
Query processing-and-optimization
WBUTTUTORIALS
 
PPTX
Query decomposition in data base
Salman Memon
 
PPTX
Advanced functions in PL SQL
Hosein Zare
 
PPT
Query Decomposition and data localization
Hafiz faiz
 
PPTX
Chapter 6 slides
jdpike
 
PDF
Goldilocks and the Three MySQL Queries
Dave Stokes
 
PPTX
Sql subquery
Raveena Thakur
 
PPT
Oracle SQL, PL/SQL best practices
Smitha Padmanabhan
 
PPTX
Pl sql best practices document
Ashwani Pandey
 
PPT
Sydney Oracle Meetup - indexes
paulguerin
 
PPTX
Oracle performance tuning for java developers
Saeed Shahsavan
 
PPT
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
baabtra.com - No. 1 supplier of quality freshers
 
Heuristic approch monika sanghani
Monika Sanghani
 
Query optimization
Zunera Bukhari
 
Presentation top tips for getting optimal sql execution
xKinAnx
 
Query processing-and-optimization
WBUTTUTORIALS
 
Query decomposition in data base
Salman Memon
 
Advanced functions in PL SQL
Hosein Zare
 
Query Decomposition and data localization
Hafiz faiz
 
Chapter 6 slides
jdpike
 
Goldilocks and the Three MySQL Queries
Dave Stokes
 
Sql subquery
Raveena Thakur
 
Oracle SQL, PL/SQL best practices
Smitha Padmanabhan
 
Pl sql best practices document
Ashwani Pandey
 
Sydney Oracle Meetup - indexes
paulguerin
 
Oracle performance tuning for java developers
Saeed Shahsavan
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
baabtra.com - No. 1 supplier of quality freshers
 

Similar to Processes in Query Optimization in (ABMS) Advanced Database Management Systems (20)

PDF
dd presentation.pdf
AnSHiKa187943
 
PPTX
Query processing and optimization on dbms
ar1289589
 
PPT
PASS Summit 2010 Keynote David DeWitt
GraySystemsLab
 
PPTX
SQL Query Optimization: Why Is It So Hard to Get Right?
Brent Ozar
 
PPTX
Beginners guide to_optimizer
Maria Colgan
 
PDF
Managing Statistics for Optimal Query Performance
Karen Morton
 
PPT
Database performance tuning and query optimization
Usman Tariq
 
PPT
Oracle query optimizer
Smitha Padmanabhan
 
PPTX
DB
Samchu Li
 
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
Dave Stokes
 
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
Dave Stokes
 
PDF
Brad McGehee Intepreting Execution Plans Mar09
Mark Ginnebaugh
 
PDF
Brad McGehee Intepreting Execution Plans Mar09
guest9d79e073
 
PDF
unit 3 DBMS.docx.pdf geometric transformer in query processing
FallenAngel35
 
PDF
unit 3 DBMS.docx.pdf geometry in query p
FallenAngel35
 
PDF
Cost-Based Optimizer Framework for Spark SQL: Spark Summit East talk by Ron H...
Spark Summit
 
PDF
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Dave Stokes
 
PPTX
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
PPTX
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
PDF
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
TricantinoLopezPerez
 
dd presentation.pdf
AnSHiKa187943
 
Query processing and optimization on dbms
ar1289589
 
PASS Summit 2010 Keynote David DeWitt
GraySystemsLab
 
SQL Query Optimization: Why Is It So Hard to Get Right?
Brent Ozar
 
Beginners guide to_optimizer
Maria Colgan
 
Managing Statistics for Optimal Query Performance
Karen Morton
 
Database performance tuning and query optimization
Usman Tariq
 
Oracle query optimizer
Smitha Padmanabhan
 
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
Dave Stokes
 
MySQL Indexes and Histograms - RMOUG Training Days 2022
Dave Stokes
 
Brad McGehee Intepreting Execution Plans Mar09
Mark Ginnebaugh
 
Brad McGehee Intepreting Execution Plans Mar09
guest9d79e073
 
unit 3 DBMS.docx.pdf geometric transformer in query processing
FallenAngel35
 
unit 3 DBMS.docx.pdf geometry in query p
FallenAngel35
 
Cost-Based Optimizer Framework for Spark SQL: Spark Summit East talk by Ron H...
Spark Summit
 
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Dave Stokes
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
TricantinoLopezPerez
 
Ad

More from gamemaker762 (6)

DOCX
Digital forensics Steps
gamemaker762
 
DOCX
Information security questions
gamemaker762
 
PPTX
Saurmandal in Hindi by Rahul kushwaha
gamemaker762
 
PPTX
Agricultural practices in Science
gamemaker762
 
PPTX
Karak sanskrit (Rahul kushwaha)
gamemaker762
 
PPTX
Global Warming in Science Rahul kushwaha
gamemaker762
 
Digital forensics Steps
gamemaker762
 
Information security questions
gamemaker762
 
Saurmandal in Hindi by Rahul kushwaha
gamemaker762
 
Agricultural practices in Science
gamemaker762
 
Karak sanskrit (Rahul kushwaha)
gamemaker762
 
Global Warming in Science Rahul kushwaha
gamemaker762
 
Ad

Recently uploaded (20)

PPTX
How to use _name_search() method in Odoo 18
Celine George
 
PPTX
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
PPTX
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
PPTX
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
PPTX
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
PPTX
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PPTX
How to Customize Quotation Layouts in Odoo 18
Celine George
 
PPTX
How to Add New Item in CogMenu in Odoo 18
Celine George
 
PPTX
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
PDF
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
PPTX
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
PDF
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
PDF
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
PPT
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
PPTX
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
How to use _name_search() method in Odoo 18
Celine George
 
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
How to Customize Quotation Layouts in Odoo 18
Celine George
 
How to Add New Item in CogMenu in Odoo 18
Celine George
 
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 

Processes in Query Optimization in (ABMS) Advanced Database Management Systems

  • 1. Topic: Processes in Query Optimization Advanced Database Management Systems
  • 2. Topics to be covered • Why Query Optimizer ? • Cost-Based Optimization (CBO) • Not optimized code • Execution Plans • Query Block • Optimizer Components • QueryTransformer • Estimator • Plan Generator • AutomaticTuning optimizer
  • 3. The query optimizer (called simply the optimizer) is built-in database software that determines the most efficient method for a SQL statement to access requested data. Introduction SQL Server can efficiently filter a data set using indexes via the WHERE clause or any combination of filters that are separated by an AND operator. By being exclusive, these operations take data and slice it into progressively smaller pieces, until only our result set remains. • Purpose of the Query Optimizer • Cost-Based Optimization • Execution Plans
  • 4. WHY Query Optimizer ? • The optimizer choose the best plan with the lowest cost among all considered candidate plans. • The optimizer uses available statistics to calculate cost. • For a specific query in a given environment, the cost computation accounts for factors of query execution such as I/O, CPU, and communication.
  • 5. Cost-Based Optimization (CBO) • The optimizer is free to merge, reorganize, and process in any order. • The optimizer determines the optimal plan for a SQL statement by examining multiple access methods, such as full table scan or index scans, different join methods such as nested loops and hash joins, different join orders, and possible transformations.
  • 7. Cost-Based Optimization SELECT prod_category, AVG(amount_sold) FROM sales s, products p WHERE p.prod_id = s.prod_id GROUP BY prod_category
  • 8. Example SELECT DISTINCT PRODUCT.ProductID, PRODUCT.Name FROM Production.Product PRODUCT INNER JOIN Sales.SalesOrderDetail DETAIL ON PRODUCT.ProductID = DETAIL.ProductID OR PRODUCT.rowguid = DETAIL.rowguid; Our expectation would be a table scan on Product and a table scan on SalesOrderDetail. Expensive. Product : 504 rows SalesOrderDetails : 121317 A singleOr can make performance worse
  • 9. Example – Optimized Code SELECT PRODUCT.ProductID, PRODUCT.Name FROM Production.Product PRODUCT INNER JOIN Sales.SalesOrderDetail DETAIL ON PRODUCT.ProductID = DETAIL.ProductID UNION SELECT PRODUCT.ProductID, PRODUCT.Name FROM Production.Product PRODUCT INNER JOIN Sales.SalesOrderDetail DETAIL ON PRODUCT.rowguid = DETAIL.rowguid SELECT DISTINCT PRODUCT.ProductID, PRODUCT.Name FROM Production.Product PRODUCT INNER JOIN Sales.SalesOrderDetail DETAIL ON PRODUCT.ProductID = DETAIL.ProductID OR PRODUCT.rowguid = DETAIL.rowguid;
  • 10. Execution Plans In the following graphic, the optimizer generates two possible execution plans for an input SQL statement, uses statistics to estimate their costs, compares their costs, and then chooses the plan with the lowest cost.
  • 11. Execution Plans – Query Block The input to the optimizer is a parsed representation of a SQL statement. The following SQL statement consists of two query blocks. The subquery in parentheses is the inner query block. The outer query block, which is the rest of the SQL statement, retrieves names of employees in the departments whose IDs were supplied by the subquery. The query form determines how query blocks are interrelated. SELECT first_name, last_name FROM hr.employeesWHERE department_id IN (SELECT department_id FROM hr.departmentsWHERE location_id = 1800);
  • 12. Query Subplans SELECT first_name, last_name FROM hr.employeesWHERE department_id IN (SELECT department_id FROM hr.departmentsWHERE location_id = 1800); • The database optimizes query blocks separately from the bottom up approach. • The database optimizes the innermost query block first and generates a subplan for it • The number of possible plans for a query block is proportional to the number of objects in the FROM clause. • This number rises exponentially with the number of objects. For example, the possible plans for a join of five tables are significantly higher than the possible plans for a join of two tables.
  • 13. Optimizer Components Phase Operation Description 1 Query Transformer The optimizer determines whether it is helpful to change the form of the query so that the optimizer can generate a better execution plan. 2 Estimator The optimizer estimates the cost of each plan based on statistics in the data dictionary. 3 Plan Generator The optimizer compares the costs of plans and chooses the lowest-cost plan, known as the execution plan, to pass to the row source generator.
  • 15. Step 1. QueryTransformer For some statements, the query transformer determines whether it is advantageous to rewrite the original SQL statement into a semantically equivalent SQL statement with a lower cost. When a viable alternative exists, the database calculates the cost of the alternatives separately and chooses the lowest-cost alternative.
  • 17. Step 2. Estimator The estimator is the component of the optimizer that determines the overall cost of a given execution plan. The estimator uses three different measures to determine cost: 1. Selectivity The percentage of rows in the row set that the query selects, with 0 meaning no rows and 1 meaning all rows. e.g. WHERE last_name LIKE 'A%' Assume there are 150 distinct employee last names. For an equality predicate last_name = 'Smith', selectivity is the reciprocal of the number n of distinct values of last_name, which is suppose .006 because the query selects rows that contain 1 out of 150 distinct values.
  • 18. Step 2. Estimator 2. Cardinality The cardinality is the number of rows returned by each operation in an execution plan. This input, which is crucial to obtaining an optimal plan, is common to all cost functions. For example, user hr queries the employees table as follows: SELECT first_name, last_name FROM employees WHERE salary='10200'; The employees table contains 107 rows. The current database statistics indicate that the number of distinct values in the salary column is 58. Therefore, the optimizer estimates the cardinality of the result set as 2, using the formula 107/58=1.84.
  • 19. Step 2. Estimator 3. Cost This measure represents units of work or resource used. The query optimizer uses disk I/O, CPU usage, and memory usage as units of work. The cost is an internal numeric measure that represents the estimated resource usage for a plan. To estimate cost, the optimizer considers factors such as the following: • System resources, which includes estimated I/O, CPU, and memory • Estimated number of rows returned (cardinality) • Size of the initial data sets • Distribution of the data • Access structures
  • 20. Step 3. Plan Generator
  • 21. Step 3. Plan Generator The plan generator explores various plans for a query block by trying out different access paths, join methods, and join orders. Many plans are possible because of the various combinations that the database can use to produce the same result. The optimizer picks the plan with the lowest cost. The trace file shows the optimizer first trys table as the outer table in the join. The optimizer calculates the cost for three different join methods: nested loops join (NL), sort merge (SM), and hash join (HA). The optimizer picks the hash join as the most efficient method:
  • 22. AutomaticTuning Optimizer The database provides the following types of optimization: • Normal optimization The optimizer compiles the SQL and generates an execution plan. The normal mode generates a reasonable plan for most SQL statements. Under normal mode, the optimizer operates with strict time constraints, usually a fraction of a second, during which it must find an optimal plan. • SQL Tuning Advisor optimization When SQL Tuning Advisor invokes the optimizer, the optimizer is known as Automatic Tuning Optimizer. In this case, the optimizer performs additional analysis to further improve the plan produced in normal mode. The optimizer output is not an execution plan, but a series of actions, along with their rationale and expected benefit for producing a significantly better plan.
  • 23. Some words/phrases/sentences are taken from internet