SlideShare a Scribd company logo
1 of 14
Prof. Neeraj Bhargava
Pooja Dixit
Department of Computer Science
School of Engineering & System Science
MDS, University Ajmer, Rajasthan, India
1
 Query optimization is a function of many relational database management systems. The
query optimizer attempts to determine the most efficient way to execute a given query
by considering the possible query plans.
 Generally, the query optimizer cannot be accessed directly by users: once queries are
submitted to the database server, and parsed by the parser, they are then passed to the
query optimizer where optimization occurs.
 Query optimization is a combination of:-
◦ Query: A query is a request for information from a database.
◦ Query Plans: A query plan (or query execution plan) is an ordered set of steps used to
access data in a SQL relational database management system.
◦ Query Optimization: A single query can be executed through different algorithms or
re-written in different forms and structures. Hence, the question of query
optimization comes into the picture – Which of these forms or pathways is the most
optimal? The query optimizer attempts to determine the most efficient way to execute
a given query by considering the possible query plans.
2
 Importance: The goal of query optimization is to reduce the system resources required to
fulfill a query, and ultimately provide the user with the correct result set faster.
◦ First, it provides the user with faster results, which makes the application seem faster to the user.
◦ Secondly, it allows the system to service more queries in the same amount of time, because each
request takes less time than unoptimized queries.
◦ Thirdly, query optimization ultimately reduces the amount of wear on the hardware (e.g. disk drives),
and allows the server to run more efficiently (e.g. lower power consumption, less memory usage).
 There are broadly two ways a query can be optimized:
◦ Analyze and transform equivalent relational expressions: Try to minimize the tuple and column counts
of the intermediate and final query processes (discussed here).
◦ Using different algorithms for each operation: These underlying algorithms determine how tuples are
accessed from the data structures they are stored in, indexing, hashing, data retrieval and hence
influence the number of disk and block accesses (discussed in query processing).
3
 Query processing refers to the range of activities involved in extracting data from a database.
The activities include translation of queries in high-level database languages into
expressions that can be used at the physical level of the file system, a variety of query-
optimizing transformations, and actual evaluation of queries.
 Overview
 The steps involved in processing a query appear in Figure. The basic steps are:
◦ Parsing and translation.
◦ Optimization.
◦ Evaluation.
4
 Before query processing can begin, the system must translate the query into a usable form. A
language such as SQL is suitable for human use, but is ill suited to be the system’s internal
representation of a query. A more useful internal representation is one based on the
extended relational algebra.
 Given a query, there are generally a variety of methods for computing the answer. For
example, we have seen that, in SQL, a query could be expressed in several different ways.
Each SQL query can itself be translated into a relationalalgebra expression in one of several
ways. Furthermore, the relational-algebra representation of a query specifies only partially
how to evaluate a query; there are usually several ways to evaluate relational-algebra
expressions. As an
select salary from instructor where salary < 75000;
 This query can be translated into either of the following relational-algebra expressions:
5
 Further, we can execute each relational-algebra operation by one of several different
algorithms. For example, to implement the preceding selection, we can search every tuple in
instructor to find tuples with salary less than 75000. If a B+-tree index is available on the
attribute salary, we can use the index instead to locate the tuples.
A Query-Evaluation Plan
6
 A sequence of primitive operations that can be used to evaluate a query is a query-
execution plan or query-evaluation plan.
 The query-execution engine takes a query-evaluation plan, executes that plan, and
returns the answers to the query.
 The query optimizer uses these two techniques to determine which process or
expression to consider for evaluating the query.
 There are two methods of query optimization.
1. Cost based Optimization (Physical)
2. Heuristic Optimization (Logical)
7
 Cost-Based Optimization also known as Cost-Based Query
Optimization or CBO Optimizer) is an optimization technique in
Spark SQL that uses table statistics to determine the most efficient
query execution plan of a structured query (given the logical query
plan).
 Cost-based optimization is disabled by default. Spark SQL uses
spark.sql.cbo.enabled configuration property to control whether
the CBO should be enabled and used for query optimization or not.
 Cost-Based Optimization uses logical optimization rules (e.g.
CostBasedJoinReorder) to optimize the logical plan of a structured
query based on statistics.
8
Heuristic Based Optimization
◦ Heuristic based optimization uses rule-based optimization approaches
for query optimization. These algorithms have polynomial time and
space complexity, which is lower than the exponential complexity of
exhaustive search-based algorithms. However, these algorithms do not
necessarily produce the best query plan.
◦ Some of the common heuristic rules are −
 Perform select and project operations before join operations. This is
done by moving the select and project operations down the query
tree. This reduces the number of tuples available for join.
 Perform the most restrictive select/project operations at first before
the other operations.
 Avoid cross-product operation since they result in very large-sized
intermediate tables.
9
 External sorting is a technique in which the data is stored on the secondary memory, in
which part by part data is loaded into the main memory and then sorting can be done over
there. Then this sorted data will be stored in the intermediate files. Finally, these files will be
merged to get a sorted data. Thus by using the external sorting technique, a huge amount of
data can be sorted easily. In case of external sorting, all the data cannot be accommodated
on the single memory, in this case, some amount of memory needs to be kept on a memory
such as hard disk, compact disk and so on.
 The requirement of external sorting is there, where the data we have to store in the main
memory does not fit into it. Basically, it consists of two phases that are:
 Sorting phase: This is a phase in which a large amount of data is sorted in an intermediate
file.
 Merge phase: In this phase, the sorted files are combined into a single larger file.
10
 One of the best examples of external sorting is external merge sort.
 External merge sort
 The external merge sort is a technique in which the data is stored in intermediate files and
then each intermediate files are sorted independently and then combined or merged to get a
sorted data.
 For example: Let us consider there are 10,000 records which have to be sorted. For this, we
need to apply the external merge sort method. Suppose the main memory has a capacity to
store 500 records in a block, with having each block size of 100 records.
11
 In this example, we can see 5 blocks will be sorted in intermediate files. This
process will be repeated 20 times to get all the records. Then by this, we start
merging a pair of intermediate files in the main memory to get a sorted output.
 Two-Way Merge Sort
 Two-way merge sort is a technique which works in two stages which are as follows
here:
◦ Stage 1: Firstly break the records into the blocks and then sort the individual record with
the help of two input tapes.
◦ Stage 2: In this merge the sorted blocks and then create a single sorted file with the help
of two output tapes.
 By this, it can be said that two-way merge sort uses the two input tapes and two
output tapes for sorting the data.
12
 Algorithm for Two-Way Merge Sort:
 Step 1) Divide the elements into the blocks of size M. Sort each block and then write
on disk.
 Step 2) Merge two runs
◦ Read first value on every two runs.
◦ Then compare it and sort it.
◦ Write the sorted record on the output tape.
 Step 3) Repeat the step 2 and get longer and longer runs on alternates tapes.
Finally, at last, we will get a single sorted list.
13
 Analysis
 This algorithm requires log(N/M) passes with initial run pass.
Therefore, at each pass the N records are processed and at last we
will get a time complexity as O(N log(N/M).
14

More Related Content

What's hot

Transaction management DBMS
Transaction  management DBMSTransaction  management DBMS
Transaction management DBMSMegha Patel
 
Validation based protocol
Validation based protocolValidation based protocol
Validation based protocolBBDITM LUCKNOW
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraintsmadhav bansal
 
Query processing and Query Optimization
Query processing and Query OptimizationQuery processing and Query Optimization
Query processing and Query OptimizationNiraj Gandha
 
Distributed database management system
Distributed database management  systemDistributed database management  system
Distributed database management systemPooja Dixit
 
Transaction Properties in database | ACID Properties
Transaction Properties in database | ACID PropertiesTransaction Properties in database | ACID Properties
Transaction Properties in database | ACID Propertiesnomanbarki
 
Query processing-and-optimization
Query processing-and-optimizationQuery processing-and-optimization
Query processing-and-optimizationWBUTTUTORIALS
 
File organization 1
File organization 1File organization 1
File organization 1Rupali Rana
 
8 query processing and optimization
8 query processing and optimization8 query processing and optimization
8 query processing and optimizationKumar
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structureschauhankapil
 
Transaction management in DBMS
Transaction management in DBMSTransaction management in DBMS
Transaction management in DBMSMegha Sharma
 
Transaction management
Transaction managementTransaction management
Transaction managementrenuka_a
 
Lock based protocols
Lock based protocolsLock based protocols
Lock based protocolsChethanMp7
 

What's hot (20)

Transaction management DBMS
Transaction  management DBMSTransaction  management DBMS
Transaction management DBMS
 
Validation based protocol
Validation based protocolValidation based protocol
Validation based protocol
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
Query processing and Query Optimization
Query processing and Query OptimizationQuery processing and Query Optimization
Query processing and Query Optimization
 
Distributed database management system
Distributed database management  systemDistributed database management  system
Distributed database management system
 
Transaction Properties in database | ACID Properties
Transaction Properties in database | ACID PropertiesTransaction Properties in database | ACID Properties
Transaction Properties in database | ACID Properties
 
Query processing-and-optimization
Query processing-and-optimizationQuery processing-and-optimization
Query processing-and-optimization
 
Query processing
Query processingQuery processing
Query processing
 
Distributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query ProcessingDistributed DBMS - Unit 6 - Query Processing
Distributed DBMS - Unit 6 - Query Processing
 
serializability in dbms
serializability in dbmsserializability in dbms
serializability in dbms
 
Acid properties
Acid propertiesAcid properties
Acid properties
 
File organization 1
File organization 1File organization 1
File organization 1
 
Database recovery
Database recoveryDatabase recovery
Database recovery
 
8 query processing and optimization
8 query processing and optimization8 query processing and optimization
8 query processing and optimization
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Database fragmentation
Database fragmentationDatabase fragmentation
Database fragmentation
 
Transaction management in DBMS
Transaction management in DBMSTransaction management in DBMS
Transaction management in DBMS
 
Transaction management
Transaction managementTransaction management
Transaction management
 
Query optimization
Query optimizationQuery optimization
Query optimization
 
Lock based protocols
Lock based protocolsLock based protocols
Lock based protocols
 

Similar to Query optimization

Implementation of query optimization for reducing run time
Implementation of query optimization for reducing run timeImplementation of query optimization for reducing run time
Implementation of query optimization for reducing run timeAlexander Decker
 
Final report group2
Final report group2Final report group2
Final report group2George Sam
 
A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...Editor IJCATR
 
A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...Editor IJCATR
 
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptxLECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptxAthosBeatus
 
Design of file system architecture with cluster
Design of file system architecture with clusterDesign of file system architecture with cluster
Design of file system architecture with clustereSAT Publishing House
 
Implementing sorting in database systems
Implementing sorting in database systemsImplementing sorting in database systems
Implementing sorting in database systemsunyil96
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluationavniS
 
RDBMS
RDBMSRDBMS
RDBMSsowfi
 
Automated Essay Grading using Features Selection
Automated Essay Grading using Features SelectionAutomated Essay Grading using Features Selection
Automated Essay Grading using Features SelectionIRJET Journal
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptxShafii8
 
Query Execution Time and Query Optimization.
Query Execution Time and Query Optimization.Query Execution Time and Query Optimization.
Query Execution Time and Query Optimization.Radhe Krishna Rajan
 
Issues in Query Processing and Optimization
Issues in Query Processing and OptimizationIssues in Query Processing and Optimization
Issues in Query Processing and OptimizationEditor IJMTER
 
Query processing in Distributed Database System
Query processing in Distributed Database SystemQuery processing in Distributed Database System
Query processing in Distributed Database SystemMeghaj Mallick
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cRonald Francisco Vargas Quesada
 

Similar to Query optimization (20)

Measures of query cost
Measures of query costMeasures of query cost
Measures of query cost
 
dd presentation.pdf
dd presentation.pdfdd presentation.pdf
dd presentation.pdf
 
Query processing
Query processingQuery processing
Query processing
 
Implementation of query optimization for reducing run time
Implementation of query optimization for reducing run timeImplementation of query optimization for reducing run time
Implementation of query optimization for reducing run time
 
Final report group2
Final report group2Final report group2
Final report group2
 
A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...
 
A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...A Review of Data Access Optimization Techniques in a Distributed Database Man...
A Review of Data Access Optimization Techniques in a Distributed Database Man...
 
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptxLECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
LECTURE_06_DATABASE PROCESSING & OPTIMAZATION.pptx
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query optimizer
 
Design of file system architecture with cluster
Design of file system architecture with clusterDesign of file system architecture with cluster
Design of file system architecture with cluster
 
Implementing sorting in database systems
Implementing sorting in database systemsImplementing sorting in database systems
Implementing sorting in database systems
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluation
 
RDBMS
RDBMSRDBMS
RDBMS
 
Automated Essay Grading using Features Selection
Automated Essay Grading using Features SelectionAutomated Essay Grading using Features Selection
Automated Essay Grading using Features Selection
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Query Execution Time and Query Optimization.
Query Execution Time and Query Optimization.Query Execution Time and Query Optimization.
Query Execution Time and Query Optimization.
 
Issues in Query Processing and Optimization
Issues in Query Processing and OptimizationIssues in Query Processing and Optimization
Issues in Query Processing and Optimization
 
Query processing in Distributed Database System
Query processing in Distributed Database SystemQuery processing in Distributed Database System
Query processing in Distributed Database System
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
 
Data warehouse physical design
Data warehouse physical designData warehouse physical design
Data warehouse physical design
 

More from Pooja Dixit

Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptxPooja Dixit
 
number system.pptx
number system.pptxnumber system.pptx
number system.pptxPooja Dixit
 
Multiplexer.pptx
Multiplexer.pptxMultiplexer.pptx
Multiplexer.pptxPooja Dixit
 
Logic Gates.pptx
Logic Gates.pptxLogic Gates.pptx
Logic Gates.pptxPooja Dixit
 
Karnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxKarnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxPooja Dixit
 
Half Subtractor.pptx
Half Subtractor.pptxHalf Subtractor.pptx
Half Subtractor.pptxPooja Dixit
 
De-multiplexer.pptx
De-multiplexer.pptxDe-multiplexer.pptx
De-multiplexer.pptxPooja Dixit
 
DeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxDeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxPooja Dixit
 
Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptxPooja Dixit
 
Boolean Algebra.pptx
Boolean Algebra.pptxBoolean Algebra.pptx
Boolean Algebra.pptxPooja Dixit
 
Binary Multiplication & Division.pptx
Binary Multiplication & Division.pptxBinary Multiplication & Division.pptx
Binary Multiplication & Division.pptxPooja Dixit
 
Binary addition.pptx
Binary addition.pptxBinary addition.pptx
Binary addition.pptxPooja Dixit
 
Basics of Computer Organization.pptx
Basics of Computer Organization.pptxBasics of Computer Organization.pptx
Basics of Computer Organization.pptxPooja Dixit
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithmPooja Dixit
 

More from Pooja Dixit (20)

Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
number system.pptx
number system.pptxnumber system.pptx
number system.pptx
 
Multiplexer.pptx
Multiplexer.pptxMultiplexer.pptx
Multiplexer.pptx
 
Logic Gates.pptx
Logic Gates.pptxLogic Gates.pptx
Logic Gates.pptx
 
K-Map.pptx
K-Map.pptxK-Map.pptx
K-Map.pptx
 
Karnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxKarnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptx
 
Half Subtractor.pptx
Half Subtractor.pptxHalf Subtractor.pptx
Half Subtractor.pptx
 
Gray Code.pptx
Gray Code.pptxGray Code.pptx
Gray Code.pptx
 
Flip Flop.pptx
Flip Flop.pptxFlip Flop.pptx
Flip Flop.pptx
 
Encoder.pptx
Encoder.pptxEncoder.pptx
Encoder.pptx
 
De-multiplexer.pptx
De-multiplexer.pptxDe-multiplexer.pptx
De-multiplexer.pptx
 
DeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxDeMorgan’s Theory.pptx
DeMorgan’s Theory.pptx
 
Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
Boolean Algebra.pptx
Boolean Algebra.pptxBoolean Algebra.pptx
Boolean Algebra.pptx
 
Binary Multiplication & Division.pptx
Binary Multiplication & Division.pptxBinary Multiplication & Division.pptx
Binary Multiplication & Division.pptx
 
Binary addition.pptx
Binary addition.pptxBinary addition.pptx
Binary addition.pptx
 
Basics of Computer Organization.pptx
Basics of Computer Organization.pptxBasics of Computer Organization.pptx
Basics of Computer Organization.pptx
 
Decoders
DecodersDecoders
Decoders
 
Three Address code
Three Address code Three Address code
Three Address code
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 

Recently uploaded

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
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
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
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
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
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 

Recently uploaded (20)

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
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
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
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
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
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
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
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 

Query optimization

  • 1. Prof. Neeraj Bhargava Pooja Dixit Department of Computer Science School of Engineering & System Science MDS, University Ajmer, Rajasthan, India 1
  • 2.  Query optimization is a function of many relational database management systems. The query optimizer attempts to determine the most efficient way to execute a given query by considering the possible query plans.  Generally, the query optimizer cannot be accessed directly by users: once queries are submitted to the database server, and parsed by the parser, they are then passed to the query optimizer where optimization occurs.  Query optimization is a combination of:- ◦ Query: A query is a request for information from a database. ◦ Query Plans: A query plan (or query execution plan) is an ordered set of steps used to access data in a SQL relational database management system. ◦ Query Optimization: A single query can be executed through different algorithms or re-written in different forms and structures. Hence, the question of query optimization comes into the picture – Which of these forms or pathways is the most optimal? The query optimizer attempts to determine the most efficient way to execute a given query by considering the possible query plans. 2
  • 3.  Importance: The goal of query optimization is to reduce the system resources required to fulfill a query, and ultimately provide the user with the correct result set faster. ◦ First, it provides the user with faster results, which makes the application seem faster to the user. ◦ Secondly, it allows the system to service more queries in the same amount of time, because each request takes less time than unoptimized queries. ◦ Thirdly, query optimization ultimately reduces the amount of wear on the hardware (e.g. disk drives), and allows the server to run more efficiently (e.g. lower power consumption, less memory usage).  There are broadly two ways a query can be optimized: ◦ Analyze and transform equivalent relational expressions: Try to minimize the tuple and column counts of the intermediate and final query processes (discussed here). ◦ Using different algorithms for each operation: These underlying algorithms determine how tuples are accessed from the data structures they are stored in, indexing, hashing, data retrieval and hence influence the number of disk and block accesses (discussed in query processing). 3
  • 4.  Query processing refers to the range of activities involved in extracting data from a database. The activities include translation of queries in high-level database languages into expressions that can be used at the physical level of the file system, a variety of query- optimizing transformations, and actual evaluation of queries.  Overview  The steps involved in processing a query appear in Figure. The basic steps are: ◦ Parsing and translation. ◦ Optimization. ◦ Evaluation. 4
  • 5.  Before query processing can begin, the system must translate the query into a usable form. A language such as SQL is suitable for human use, but is ill suited to be the system’s internal representation of a query. A more useful internal representation is one based on the extended relational algebra.  Given a query, there are generally a variety of methods for computing the answer. For example, we have seen that, in SQL, a query could be expressed in several different ways. Each SQL query can itself be translated into a relationalalgebra expression in one of several ways. Furthermore, the relational-algebra representation of a query specifies only partially how to evaluate a query; there are usually several ways to evaluate relational-algebra expressions. As an select salary from instructor where salary < 75000;  This query can be translated into either of the following relational-algebra expressions: 5
  • 6.  Further, we can execute each relational-algebra operation by one of several different algorithms. For example, to implement the preceding selection, we can search every tuple in instructor to find tuples with salary less than 75000. If a B+-tree index is available on the attribute salary, we can use the index instead to locate the tuples. A Query-Evaluation Plan 6
  • 7.  A sequence of primitive operations that can be used to evaluate a query is a query- execution plan or query-evaluation plan.  The query-execution engine takes a query-evaluation plan, executes that plan, and returns the answers to the query.  The query optimizer uses these two techniques to determine which process or expression to consider for evaluating the query.  There are two methods of query optimization. 1. Cost based Optimization (Physical) 2. Heuristic Optimization (Logical) 7
  • 8.  Cost-Based Optimization also known as Cost-Based Query Optimization or CBO Optimizer) is an optimization technique in Spark SQL that uses table statistics to determine the most efficient query execution plan of a structured query (given the logical query plan).  Cost-based optimization is disabled by default. Spark SQL uses spark.sql.cbo.enabled configuration property to control whether the CBO should be enabled and used for query optimization or not.  Cost-Based Optimization uses logical optimization rules (e.g. CostBasedJoinReorder) to optimize the logical plan of a structured query based on statistics. 8
  • 9. Heuristic Based Optimization ◦ Heuristic based optimization uses rule-based optimization approaches for query optimization. These algorithms have polynomial time and space complexity, which is lower than the exponential complexity of exhaustive search-based algorithms. However, these algorithms do not necessarily produce the best query plan. ◦ Some of the common heuristic rules are −  Perform select and project operations before join operations. This is done by moving the select and project operations down the query tree. This reduces the number of tuples available for join.  Perform the most restrictive select/project operations at first before the other operations.  Avoid cross-product operation since they result in very large-sized intermediate tables. 9
  • 10.  External sorting is a technique in which the data is stored on the secondary memory, in which part by part data is loaded into the main memory and then sorting can be done over there. Then this sorted data will be stored in the intermediate files. Finally, these files will be merged to get a sorted data. Thus by using the external sorting technique, a huge amount of data can be sorted easily. In case of external sorting, all the data cannot be accommodated on the single memory, in this case, some amount of memory needs to be kept on a memory such as hard disk, compact disk and so on.  The requirement of external sorting is there, where the data we have to store in the main memory does not fit into it. Basically, it consists of two phases that are:  Sorting phase: This is a phase in which a large amount of data is sorted in an intermediate file.  Merge phase: In this phase, the sorted files are combined into a single larger file. 10
  • 11.  One of the best examples of external sorting is external merge sort.  External merge sort  The external merge sort is a technique in which the data is stored in intermediate files and then each intermediate files are sorted independently and then combined or merged to get a sorted data.  For example: Let us consider there are 10,000 records which have to be sorted. For this, we need to apply the external merge sort method. Suppose the main memory has a capacity to store 500 records in a block, with having each block size of 100 records. 11
  • 12.  In this example, we can see 5 blocks will be sorted in intermediate files. This process will be repeated 20 times to get all the records. Then by this, we start merging a pair of intermediate files in the main memory to get a sorted output.  Two-Way Merge Sort  Two-way merge sort is a technique which works in two stages which are as follows here: ◦ Stage 1: Firstly break the records into the blocks and then sort the individual record with the help of two input tapes. ◦ Stage 2: In this merge the sorted blocks and then create a single sorted file with the help of two output tapes.  By this, it can be said that two-way merge sort uses the two input tapes and two output tapes for sorting the data. 12
  • 13.  Algorithm for Two-Way Merge Sort:  Step 1) Divide the elements into the blocks of size M. Sort each block and then write on disk.  Step 2) Merge two runs ◦ Read first value on every two runs. ◦ Then compare it and sort it. ◦ Write the sorted record on the output tape.  Step 3) Repeat the step 2 and get longer and longer runs on alternates tapes. Finally, at last, we will get a single sorted list. 13
  • 14.  Analysis  This algorithm requires log(N/M) passes with initial run pass. Therefore, at each pass the N records are processed and at last we will get a time complexity as O(N log(N/M). 14