Query Processing and Optimization: Chapter - 2
Query Processing and Optimization: Chapter - 2
Query Processing and Optimization: Chapter - 2
Query processing requires that the DBMS identify and execute a strategy for retrieving
the results of the query. Query optimization is necessary to determine the optimal
alternative to process a query.
The first approach is to use a rule based or heuristic method for ordering the
operations in a query execution strategy.
The second approach estimates the cost of different execution strategies and chooses
the best solution.
The scanner identifies the query tokens such as SQL keywords, attribute names,
and relation names that appear in the text of the query.
The query must also be validated by checking that all attribute and relation names are
valid and semantically meaningful names in the schema of the particular database
being queried.
An internal representation of the query is then created, usually as a tree data structure
called a query tree.
It is also possible to represent the query using a graph data structure called a query
graph.
The DBMS must then devise an execution strategy or query plan for retrieving the
results of the query from the database files.
A query typically has many possible execution strategies and the process of
choosing a suitable one for processing a query is known as query optimization.
The query optimizer module has the task of producing a good execution plan.
The runtime database processor has the task of running (executing) the query
code, whether in compiled or interpreted mode, to produce the query result.
Typically, SQL queries are decomposed into query blocks, which form the
basic units that can be translated into the algebraic operators and optimized.
Before retrieving, updating or deleting data in database, a query goes through a series of query
compilation steps. These steps are known as execution plan.
In query processing, the first phase is transformation in which parser first checks the syntax of
query and also checks the relations and attributes used in the query that are defined in the
database.
After checking the syntax and verifying the relations, query is transformed into equivalent
expression that are more efficient to execute.
Transformation, depends upon various factors like existence of certain database structures,
presence of different indexes, file is sorted or not, cost of transformation, physical
characteristics of data, etc.
The next step is to validate the user privileges and ensure that the query does not disobey the
relevant integrity constraints.
Query processor first checks the syntax and existence of relations and their attributes
in database.
After validations, query processor transform it into equivalent and more efficient
expression; for example, query will be converted into a standard internal format that
parser can manipulate.
Internal form may be relational algebra, relational calculus, any low-level language,
operator graphs etc.
It gives the sequence of operations that can be performed. It is easy to understand the
query represented by operator graphs.
(iii) Response time and Data characteristics consideration: Data characteristics like
length of records, expected sizes of both intermediate and final results, size of
relations etc., are also considered for optimizing the query.
Order of tokens are also maintained to make sure that all the rules
of language grammars are followed.
(ii) Query Decomposition: In this step, query is decomposed into query blocks
It starts with the high-level query that is transformed into low level operations
For example, SQL query is decomposed into blocks like Select block, From
(A) Query Analysis: In the query analysis stage, programming language compiler
checks that the query is lexically and syntactically correct.
After analysis a correct query is converted into some internal representation, which is
more efficient for processing.
The type specification of the query qualifier and result is also checked at this stage.
A query tree is constructed using tree data structure that corresponds to the relational algebra
expression.
⧫ Internal nodes – represent intermediate relation that is the output of applying an operation in the
algebra.
Query Graph Notation: Graph data structure is also used for internal
representation of query.
In this phase, query is converted into normalized form that can be easily
manipulated.
A set of equivalency rules are applied to query and simplify the projection and
selection operations to avoid redundancy.
Query can be converted into one of the following two normal forms:
(1) Conjunctive normal form: It is a sequence of conjuncts that are connected with ‘AND’ operator.
A conjunctive selection consists only those tuples that satisfy all conjuncts.
(2) Disjunctive normal forms: It is a sequence of disjuncts that are connected with ‘OR’ operator.
A disjunctive selection contains those tuples that satisfy anyone of the disjunct.
Disjunctive normal form is more useful as it allows the query to break into a series of independent
sub-queries linked by union.
(C) Semantic Analyzer: The semantic analyzer performs the following tasks :
It makes sure that each object in query is referenced correctly according to its
data type.
(D) Query Simplifier: The major tasks of query simplifier are as follows:
It eliminates query that voids any integrity constraint without accessing the database.
(iii) Query Optimization: The aim of the query optimization step is to choose the best
possible query execution plan with minimum resources required to execute that plan.
(iv) Execution Plan: It is the basic algorithm used for each operation in the query.
Execution plans are classified into following four types: (Check From Page 6 of the PDF file)
(v) Query Code Generator: After selecting the best possible execution
(vi) Runtime Database Processor: It deals directly with main database and
user.
Compiled by: Temesgen Tilahun 24
Query Optimization
Query Optimization means converting a query into an equivalent form which is more
efficient to execute.
These are heuristic query optimization and cost based query optimization.
In cost based query optimization, optimizer estimates the cost of running of all
alternatives of a query and choose the optimum alternative.
The alternative which uses the minimum resources is having minimum cost.
The cost of a query operation is mainly depend on its selectivity i.e., the
proportion of the input relations that forms the output.
(a) Access cost of Secondary Storage: Access cost to secondary storage consists of cost
of database manipulation operations which includes searching, writing, reading of data
blocks stored in the secondary memory.
The cost of searching depends upon the type of indexes (primary, secondary, hashed),
type of file structure and ordering of relation.
In addition to physical storage location like file blocks are allocated contiguously on
the same disk or scattered on the disk.
(b) Storage cost: Storage cost consists of cost of storing intermediate results (tables or
files) that are generated by the execution strategy for the query.
(d) Memory usage cost: It consists of cost of relating to the number of memory
buffers needed during query execution.
(e) Communication cost: It consists of the cost of transferring query and its result
from database location to the location of terminal where the query is originated.
From all the above components, the most important is access cost to secondary storage
because secondary storage is comparatively slower than other devices.
Optimizer try to minimize computation cost for small databases as most of the data
files are stored in main memory.
For large database, it try to minimize the access cost to secondary storage and
For distributed databases, it tries to minimize the communication cost because various
sites are involved for data transfer.
To estimate the cost of execution strategies, optimizer access statistical data stored in
DBMS catalog.
D. Primary access method for each file and attributes for each file.
E. Number of levels for each multi-level index for an attribute A given as IA.
single relation in relation algebra and retrieves the records that satisfy the
given condition.
given below.
Compiled by: Temesgen Tilahun 33
The Estimated Cost of Strategies for Selection Operation is as given below:
A secondary index on non-key attributes Age with 2 levels and 4 first level
index blocks.
(iii) This query has a conjunctive selection condition: To estimate the cost of
use using anyone of the two components of selection condition, to retrieve the
records plus the linear search.
The linear search costs 2000 and condition salary > 9000 first gives cost
estimate of Isalary + (B/2) = 2 + 1000 = 1002 and
Cost Function for Join Operation: Join operation is the most time
R1 = 7, 2, 9, 8, 3, 9, 1, 3, 6
R2 = 8, 4, 2, 1, 3, 2, 7, 3
?
Compiled by: Temesgen Tilahun 42