SlideShare a Scribd company logo
1 of 13
QUERY
OPTIMIZATION
P R E S E N T E D BY A B D U L W A H A B
INTRODUCTION
• What is Query Optimization?
• Suppose you were given a chance to visit 15 pre-selected
different cities in Pakistan. The only constraint would be ‘Time’
-> Would you have a plan to visit the cities in any order?
• Plan:
-> Place the 15 cities in different groups based
on their proximity to each other.
-> Start with one group and move on to the
next group.
Important point made over here is that you
would have visited the cities in a more
organized manner, and the ‘Time’ constraint
mentioned earlier would have been dealt with
efficiently.
• Query Optimization works in a similar way:
There can be many different ways to get an answer
from a given query. The result would be same in all
scenarios.
DBMS strive to process the query in the most
efficient way (in terms of ‘Time’) to produce the
answer.
Cost = Time needed to get all answers
STEPS IN A QUERY
OPTIMIZATION
1. Parsing
2. Transformation
3. Implementation
QUERY FLOW
Parser
Optimizer
Code
Generator/I
nterpreter
Processor
SQL
• Query Parser – Verify validity of the SQL statement.
Translate query into an internal structure using
relational calculus.
• Query Optimizer – Find the best expression from
various different algebraic expressions. Criteria used
is ‘Cheapness’
• Code Generator/Interpreter – Make calls for the
Query processor as a result of the work done by the
optimizer.
• Query Processor – Execute the calls obtained from
the code generator.
PROJECTION EXAMPLE:
• Projections produce a result tuple for every argument
tuple.
• What is the change?
• Change in the output size is the change in the length
of tuples
Let’s take a relation ‘R’
Relation (20,000 tuples): R(a, b, c)
Each Tuple (190 bytes): header = 24 bytes, a = 8 bytes,
b = 8 bytes, c = 150 bytes
Each Block (1024): header = 24 bytes
We can fit 5 tuples into 1 block
- 5 tuples * 190 bytes/tuple = 950 bytes can fit
into 1 block
- For 20,000 tuples, we would require 4,000
blocks (20,000 / 5 tuples per block = 4,000
With a projection resulting in elimination of
column c (150 bytes), we could estimate that
each tuple would decrease to 40 bytes (190 –
150 bytes)
Now, the new estimate will be 25 tuples in 1
block.
- 25 tuples * 40 bytes/tuple = 1000 bytes will
be able to fit into 1 block
- With 20,000 tuples, the new estimate is 800
blocks (20,000 tuples / 25 tuples per block =
800 blocks)
Result is reduction by a factor of 5
QUERY OPTIMIZATION: ALGEBRAIC
EXPRESSIONS
If we had the following query-
SELECT count(id) from emp;
Instead of
SELECT count(*) from emp;
Suppose, In DLD, We have that expression.
X = A + ABC +AB + AC + AA’ ------------------(1)
X = A(1+BC+B+C+A’)
X = A(1)
X = A ---------------------------------------------------(2)
So, Tell me Which one is better 1 and 2. Both have
same result.
THANK
YOU

More Related Content

Similar to Query o

Text cnn on acme ugc moderation
Text cnn on acme ugc moderationText cnn on acme ugc moderation
Text cnn on acme ugc moderationMarsan Ma
 
Sensitivity analysis of LP chapter 4.ppt
Sensitivity analysis of LP chapter 4.pptSensitivity analysis of LP chapter 4.ppt
Sensitivity analysis of LP chapter 4.pptJagatShrestha4
 
Ch 7.pptx Linear Optimization for Finance
Ch 7.pptx Linear Optimization for FinanceCh 7.pptx Linear Optimization for Finance
Ch 7.pptx Linear Optimization for FinanceSogirKhandoker
 
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
 
Factor label method
Factor label methodFactor label method
Factor label methodstephm32
 
Job Shop Scheduling with Setup Times Release times and Deadlines
Job Shop Scheduling with Setup Times  Release times and DeadlinesJob Shop Scheduling with Setup Times  Release times and Deadlines
Job Shop Scheduling with Setup Times Release times and DeadlinesAlkis Vazacopoulos
 
CBO choice between Index and Full Scan: the good, the bad and the ugly param...
CBO choice between Index and Full Scan:  the good, the bad and the ugly param...CBO choice between Index and Full Scan:  the good, the bad and the ugly param...
CBO choice between Index and Full Scan: the good, the bad and the ugly param...Franck Pachot
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtrykapib57390
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11Paul Brebner
 

Similar to Query o (20)

Entrepreneurship Quiz 8 Oct
Entrepreneurship Quiz 8 OctEntrepreneurship Quiz 8 Oct
Entrepreneurship Quiz 8 Oct
 
Text cnn on acme ugc moderation
Text cnn on acme ugc moderationText cnn on acme ugc moderation
Text cnn on acme ugc moderation
 
Sensitivity analysis of LP chapter 4.ppt
Sensitivity analysis of LP chapter 4.pptSensitivity analysis of LP chapter 4.ppt
Sensitivity analysis of LP chapter 4.ppt
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Combinatorics.ppt
Combinatorics.pptCombinatorics.ppt
Combinatorics.ppt
 
Ch 7.pptx Linear Optimization for Finance
Ch 7.pptx Linear Optimization for FinanceCh 7.pptx Linear Optimization for Finance
Ch 7.pptx Linear Optimization for Finance
 
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
 
Arrays
ArraysArrays
Arrays
 
Travelling salesman problem
Travelling salesman problemTravelling salesman problem
Travelling salesman problem
 
Factor label method
Factor label methodFactor label method
Factor label method
 
Tn6 facility layout
Tn6 facility layoutTn6 facility layout
Tn6 facility layout
 
Tn6 facility+layout
Tn6 facility+layoutTn6 facility+layout
Tn6 facility+layout
 
Sci 111 math
Sci 111 mathSci 111 math
Sci 111 math
 
Activities in Routing.pptx
Activities in Routing.pptxActivities in Routing.pptx
Activities in Routing.pptx
 
Job Shop Scheduling with Setup Times Release times and Deadlines
Job Shop Scheduling with Setup Times  Release times and DeadlinesJob Shop Scheduling with Setup Times  Release times and Deadlines
Job Shop Scheduling with Setup Times Release times and Deadlines
 
CBO choice between Index and Full Scan: the good, the bad and the ugly param...
CBO choice between Index and Full Scan:  the good, the bad and the ugly param...CBO choice between Index and Full Scan:  the good, the bad and the ugly param...
CBO choice between Index and Full Scan: the good, the bad and the ugly param...
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11
 
Lecture ip or
Lecture ip orLecture ip or
Lecture ip or
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 

More from M Sajid R

Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)M Sajid R
 
Transport layer
Transport layerTransport layer
Transport layerM Sajid R
 
JavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentationJavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentationM Sajid R
 
Network And Topology
Network And Topology Network And Topology
Network And Topology M Sajid R
 

More from M Sajid R (6)

Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Transport layer
Transport layerTransport layer
Transport layer
 
Novartis
NovartisNovartis
Novartis
 
JavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentationJavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentation
 
Network And Topology
Network And Topology Network And Topology
Network And Topology
 
Toyota
ToyotaToyota
Toyota
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

Query o

  • 1. QUERY OPTIMIZATION P R E S E N T E D BY A B D U L W A H A B
  • 2. INTRODUCTION • What is Query Optimization? • Suppose you were given a chance to visit 15 pre-selected different cities in Pakistan. The only constraint would be ‘Time’ -> Would you have a plan to visit the cities in any order?
  • 3. • Plan: -> Place the 15 cities in different groups based on their proximity to each other. -> Start with one group and move on to the next group. Important point made over here is that you would have visited the cities in a more organized manner, and the ‘Time’ constraint mentioned earlier would have been dealt with efficiently.
  • 4. • Query Optimization works in a similar way: There can be many different ways to get an answer from a given query. The result would be same in all scenarios. DBMS strive to process the query in the most efficient way (in terms of ‘Time’) to produce the answer. Cost = Time needed to get all answers
  • 5. STEPS IN A QUERY OPTIMIZATION 1. Parsing 2. Transformation 3. Implementation
  • 7. • Query Parser – Verify validity of the SQL statement. Translate query into an internal structure using relational calculus. • Query Optimizer – Find the best expression from various different algebraic expressions. Criteria used is ‘Cheapness’ • Code Generator/Interpreter – Make calls for the Query processor as a result of the work done by the optimizer. • Query Processor – Execute the calls obtained from the code generator.
  • 8. PROJECTION EXAMPLE: • Projections produce a result tuple for every argument tuple. • What is the change? • Change in the output size is the change in the length of tuples Let’s take a relation ‘R’ Relation (20,000 tuples): R(a, b, c) Each Tuple (190 bytes): header = 24 bytes, a = 8 bytes, b = 8 bytes, c = 150 bytes Each Block (1024): header = 24 bytes
  • 9. We can fit 5 tuples into 1 block - 5 tuples * 190 bytes/tuple = 950 bytes can fit into 1 block - For 20,000 tuples, we would require 4,000 blocks (20,000 / 5 tuples per block = 4,000 With a projection resulting in elimination of column c (150 bytes), we could estimate that each tuple would decrease to 40 bytes (190 – 150 bytes)
  • 10. Now, the new estimate will be 25 tuples in 1 block. - 25 tuples * 40 bytes/tuple = 1000 bytes will be able to fit into 1 block - With 20,000 tuples, the new estimate is 800 blocks (20,000 tuples / 25 tuples per block = 800 blocks) Result is reduction by a factor of 5
  • 11. QUERY OPTIMIZATION: ALGEBRAIC EXPRESSIONS If we had the following query- SELECT count(id) from emp; Instead of SELECT count(*) from emp;
  • 12. Suppose, In DLD, We have that expression. X = A + ABC +AB + AC + AA’ ------------------(1) X = A(1+BC+B+C+A’) X = A(1) X = A ---------------------------------------------------(2) So, Tell me Which one is better 1 and 2. Both have same result.