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

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
PMILebanonChapter
 
Factor label method
Factor label methodFactor label method
Factor label method
stephm32
 
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
Alkis Vazacopoulos
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
kapib57390
 

Similar to Query o (20)

Boolean Algebra
Boolean AlgebraBoolean Algebra
Boolean Algebra
 
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
 

More from 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

Recently uploaded (20)

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

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.