0% found this document useful (0 votes)
22 views27 pages

CO3 Session 11

The document outlines a session on Query Processing and Optimization in a Database Management System (DBMS) course. It explains the steps involved in query processing, including parsing, translation, optimization, and evaluation, and highlights the importance of query optimization for improving database performance. The session aims to familiarize students with these concepts and their practical applications in data retrieval and management.

Uploaded by

panda.lazy53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views27 pages

CO3 Session 11

The document outlines a session on Query Processing and Optimization in a Database Management System (DBMS) course. It explains the steps involved in query processing, including parsing, translation, optimization, and evaluation, and highlights the importance of query optimization for improving database performance. The session aims to familiarize students with these concepts and their practical applications in data retrieval and management.

Uploaded by

panda.lazy53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Department of

CSE
COURSE NAME: DBMS
COURSE CODE:23AD2102A
Topic: Query Processing and Optimization

Session – 11

1
AIM OF THE
SESSION
To familiarize students with the basic concept of Query Processing and Optimization

INSTRUCTIONAL OBJECTIVES

This Session is designed to: understand how Query processed and Optimized to

produces the output of the query

LEARNING OUTCOMES

At the end of this session, you should be able to: Identify the concept of Query Processing and

Optimization
What is a query?

• Query display a subset of data contained in various tables of the database

• Query is used to retrieve records from the table


How Does Query Work?

• Query can either be a select, an action, or a combination of both. Select


queries can retrieve information from data sources, and action queries
work for data manipulation
Example, to add, change or delete data

• Advanced users can also use query commands to perform various


programming tasks and granting permissions
What are the benefits of using a query?

THE
computerized
COLLECTION
OF physically some computer storage
on medium.
• Review data from multiple tables simultaneously

• Filter records containing only certain fields and of certain criteria

• Automate data management tasks and perform calculations


What is query optimization?

THE
COLLECTION
OF

• Query optimization is the process of improving the performance of a database query

• Performance improving the query itself, improving the indexing of the database, or
improving the database design
What are some typical ways that the optimizer decides which
query plan it should use?
?THE
COLLECTION
OF

• Optimizer typically looks at the cost of each query plan and chooses the one that is cheapest

• Consider other factors such as the number of rows that need to be retrieved, the order in
which the rows need to be retrieved, and the availability of indexes
What is query optimization?

THE
computerized
COLLECTION
OF physically some computer storage
on medium.
• Query processing is the process of translating a high-level query, such as SQL,
into a low-level query that can be executed by the database system

• Involves parsing, validating, and optimizing the query, as well as generating a


query execution plan
QUERY PROCESSING IN DBMS

• Query Processing is the activity performed in extracting data from the database

• Query processing includes certain activities for data retrieval

• Most important tasks of a DBMS is to figure out an efficient evaluation strategy


(access plan) for high level statements

9
QUERY PROCESSING IN DBMS

Query processing is a 3-step process for fetching the data from the database

STEPS involved are:

 Parsing and translation

 Optimization

 Evaluation
10
STEPS IN QUERY PROCESSING

11
STEPS IN QUERY PROCESSING - PARSING &
TRANSLATION
• Computer system needs to translate the query into a human-readable and understandable
language

• User queries get translated in high-level database languages such as SQL

• Relational algebra is well suited for the internal representation of a query

12
STEPS IN QUERY PROCESSING - PARSING &
TRANSLATION
• First step in query processing is Parsing and Translation
• Queries undergo lexical, syntactic, and semantic analysis
• Essentially, the query gets broken down into different tokens and white spaces are removed along with the
comments (Lexical Analysis)

• In the next step, the query gets checked for the correctness, both syntax and semantic wise
Query processor first checks the query if the rules of SQL have been correctly followed or not (Syntactic Analysis)

• Finally, the query processor checks if the meaning of the query is right or not.
Things like if the table(s) mentioned in the query are present in the DB or not? if the column(s) referred from all the
table(s) are
actually present in them or not? (Semantic Analysis)
• Once the above mentioned checks pass, the flow moves to convert all the tokens into relational expressions,
graphs, and trees
• Makes the processing of the query easier for the other parsers
13
PARSING

• When a user executes any query for generating the internal form of the query

• Parser in the system checks the syntax of the query, verifies the name of the relation in the
database, the tuple, and finally the required attribute value

• Once syntax and semantic checks pass, the flow moves to convert all the tokens into
relational expressions, graphs, and trees

• Makes the processing of the query easier for the other parsers

• Parser creates a tree of the query, known as 'parse-tree'


14
QUERY EXAMPLE

User wants to fetch the records of the employees whose salary is greater than or
equal to 20000

15
PARSING

QUERY:
User wants to fetch the records of the employees whose salary is greater than
or equal to 20000

• Query undertaken

select emp_name from Employee where salary > 20000;

16
QUERY WOULD BE DIVIDED INTO TOKENS…

TOKENS ARE

• SELECT
• emp_name
• FROM
• employee
• WHERE
• Salary
• >
• 10000
17
TOKENS(QUERY) GET VALIDATES FOR….

• Name of the queried table is looked into the data dictionary table

• Name of the columns mentioned (emp_name and salary) in the tokens are validated
for existence

• Type of column(s) being compared have to be of the same type


(salary and the value 20000 should have the same data type)

18
TRANSLATION

• Next step is to translate the generated set of tokens into a relational algebra query

• These are easy to handle for the optimizer in further processes

• Gets translated into expressions that can be further used at the physical level of the
file system

19
TRANSLATION

QUERY: user wants to fetch the records of the employees whose salary is greater than or equal to 20000
• To make the system understand the user query, it needs to be translated in the form of relational algebra

Query in the relational algebra form as:

• σsalary>20000 (πsalary (Employee))

• πsalary (σsalary>20000 (Employee))

After translating the given query, we can execute each relational algebra operation by using different algorithms in this
way, a query processing begins its working

20
STEPS IN QUERY PROCESSING - QUERY
EVALUATION
• After translating the user query, the system executes a query evaluation plan

• Once the query processor has the relational forms with it, the next step is to apply certain rules and
algorithms to generate a few other powerful and efficient data structures

• Data structures help in constructing the query evaluation plans

• Addition to the relational algebra translation, it is required to annotate the translated relational algebra
expression with the instructions used for specifying and evaluating each operation

For example: if the relational graph was constructed, there could be multiple paths from source to
destination
A query execution plan will be generated for each of the paths
21
QUERY EVALUATION PLAN

• In order to fully evaluate a query, the system needs to construct a query evaluation plan
• Annotations in the evaluation plan may refer to the algorithms to be used for the particular
index or the specific operations
• Such relational algebra with annotations is referred to as Evaluation Primitives
• Evaluation primitives carry the instructions needed for the evaluation of the operation
• Query evaluation plan defines a sequence of primitive operations used for evaluating a query
• Query evaluation plan is also referred to as the query execution plan
• A query execution engine is responsible for generating the output of the given query
• It takes the query execution plan, executes it, and finally makes the output for the user query

22
STEPS IN QUERY PROCESSING - QUERY OPTIMIZATION

• Cost of the query evaluation can vary for different types of queries

• DMBS picks up the most efficient evaluation plan based on the cost each plan has

• Database system generates an efficient query evaluation plan, which minimizes its cost
Type of task performed by the database system and is known as Query Optimization

• Aim is to minimize the query evaluation time

23
STEPS IN QUERY PROCESSING - QUERY OPTIMIZATION

• Optimizer also evaluates the usage of index present in the table and the
columns being used. It also finds out the best order of subqueries to be
executed so as to ensure only the best of the plans gets executed

• For any query there are multiple evaluation plans to execute it. Choosing the
one which costs the least is called Query Optimization in DBMS

• Although the system is responsible for constructing the evaluation plan, the
user does need not to write their query efficiently
24
FACTORS WEIGHED IN BY THE OPTIMIZER

Factors weighed in by the optimizer to calculate the cost of a query evaluation plan

• CPU time

• Number of tuples to be scanned

• Disk access time

• Number of operations

25
STEPS IN QUERY PROCESSING - QUERY OPTIMIZATION

• For optimizing a query, the query optimizer should have an estimated cost analysis of each
operation.

• Overall operation cost depends on the memory allocations to several operations, execution
costs and so on

• Finally, after selecting an evaluation plan, the system evaluates the query and produces the
output of the query

26
THANK YOU

Team – DBMS

You might also like