0% found this document useful (0 votes)
18 views5 pages

Query Optimization

Query optimisation
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
18 views5 pages

Query Optimization

Query optimisation
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Query Optimization---->>>> When a query is processed ,

following steps are followed 

 Query Parsing and Translation

a.) Initially, the SQL query is scanned.

b.) Then it is parsed to look for syntactical errors and correctness of data types. If
the query passes this step, the query is decomposed into smaller query blocks.

c.) Each block is then translated to equivalent relational algebra expression.

3.Then alternative execution plans are devised for retrieving results


from the database tables.

4.The process of choosing the most appropriate execution plan for


query processing is called query optimization.

Query Processing
Query processing is a set of all activities starting from query placement to displaying
the results of the query. The steps are as shown in the following diagram −

Relational Algebra :
Relational algebra defines the basic set of operations of
relational database model.

1. A sequence of relational algebra operations forms a


relational algebra expression.
2. The result of this expression represents the result of a
database query.

The basic operations are –


 Projection
 Selection
 Union
 Intersection
 Minus
 Join

1.Project Operation (or ∏)


It projects those column(s) that satisfy any given predicate.
Here B1, B2 , An refer to the attribute names of the relation r.
The notation is − ∏B1, B2, Bn (r)
Remember that duplicate rows are eliminated automatically, since
relation is a set.
Example
∏subject, writer (Novels)
The output would be − Selecting and projecting columns named as
writer as well as the subject from the relation Novels.

2. Selection

Selection operation displays a subset of tuples of a table that satisfies certain


conditions. This gives a horizontal partition of the table.

Select Operation (or σ)


It selects tuples from a relation that satisfy the provided predicate.
The notation is − σp(r)
Here σ stands for the selection predicate while r stands for the
relation. p refers to the prepositional logic formula that may use
connectors such as or, and, and not. Also, these terms may make use of
relational operators such as − =, ≠, ≥, < , >, ≤.
Example ----
σsubject = “information”(Novels)
The output would be − Selecting tuples from the novels wherever the
subject happens to be ‘information’.
σsubject = “information” and cost = “150”(Novels)
The output would be − Selecting tuples from the novels wherever the
subject happens to be ‘information’ and the ‘price’ is 150.
σsubject = “information” and cost = “150” or year > “2015”(Novels)
The output would be − Selecting tuples from the novels wherever the
subject happens to be ‘information’ and the ‘price’ is 150 or those novels
have been published after 2015.

3. Join
Join operation combines related tuples of two different tables (results of
queries) into a single table.
Notation : R ⋈θ S
Where R is the first relation
S is the second relation

Theta is condition to be satisfied.


Let's have a simple example to understand this.
Suppose we want a relation where EXPERIENCE from EMPLOYEE >=

EMPLOYEE ⋈ θ
MIN_EXPERIENCE from DEPARTMENT.
Syntax 
EMPLOYEE.EXPERIENCE>=DEPARTMENT.MIN_EXPERIENCE DEPARTMENT

Steps for Query Optimization

Query optimization involves three steps, namely 

1. query tree generation,


2. plan generation,
3. query plan code generation.

Steps To Make a Query Tree


Step 1: Write the relations you want to execute as the tree’s Leaf
nodes. Here R and S are the relations.
Step 2: Add the condition (here R.P = S.P) with the relational algebra
operator as an internal node (or parent node of these two leaf nodes).
Step 3: Now add the root node that on execution gives the output of
the query.

πp (R ⋈ R.P = S.P S)
Example 1: Consider a relational algebra expression:

Step 1: Write the relations you want to execute as the tree’s Leaf nodes. Here R and S are the relations.

Two Relations R and S

Step 2: Add the condition (here R.P = S.P) with the relational algebra operator as an internal node (or
parent node of these two leaf nodes).
JOIN R and S where R.P = S.P

Step 3: Now add the root node that on execution gives the output of the query.

Step 2 − Query Plan Generation


A query plan also states how the intermediate tables should be passed from one
operator to the next,
how temporary tables should be used and how operations should be
pipelined/combined.

Step 3− Code Generation

Code generation is the final step in query optimization. It is the executable form of
the query, whose form depends upon the type of the underlying operating system.
Once the query code is generated, the Execution Manager runs it and produces the
results.

You might also like