ADB Chapter 2 DB Part1
ADB Chapter 2 DB Part1
1. Query Processing:
SQL Server takes four steps to process a query: parsing, translating, optimizing,
and execution. They are shown in Figure 1. The first four steps are all performed
by the Relational Engine. The output of the third step is the optimized plan that
is scheduled, and during which calls are made to the Storage Engine to retrieve
the data that becomes the results of the query you are executing.
Parsing
During the parsing stage SQL Server performs basic checks on the source code
(your T-SQL batch). This parsing looks for invalid SQL syntax, such as incorrect
use of reserved words, column and table names, and so on.
If parsing completes without errors, it generates a parse tree, which is passed
onto the next stage of query processing, binding. The parse tree is an internal
representation of the query. If parsing detects any errors, the process stops and
the errors are returned.
Query
Translation:
It is used to translate the given SQL query to low level language query
using relational algebra operations.
Basic operation:
σ- Selection which is used to select a tuple.
Π- Projection which is used to select an attributes.
Eg:
Select empname from employee where salary>10000;
Π empname (σ (employee))
salary>10000
Optimization:
It is used to optimize the query find low cost query. It maintains statistical
data as size of the table, query size, record size, running time etc with help of
data dictionary.
Execution Plan:
By this execution plan multiple low level language query is generated.
Evaluation:
It is used to determine low cost query.
2. Query optimization:
It is the process of selecting an efficient execution plan for evaluating
the query.
After parsing of the query, parsed query is passed to query optimizer,
which generates different execution plans to evaluate parsed query and
select the plan with least estimated cost.
Catalog manager helps optimizer to choose best plan to execute query
generating cost of each plan.
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: ρ)
Relational Algebra Operations from Set theory
UNION (υ)
INTERSECTION ( ),
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
JOIN
DIVISION
SELECT (σ)
The SELECT operation is used for selecting a subset of the tuples according to a
given selection condition. Sigma(σ)Symbol denotes it. It is used as an expression
to choose tuples which meet the selection condition. Select operator selects
tuples that satisfy a given predicate.
σp(r)
σ is the predicate
p is prepositional logic
Example 1
Projection(π)
The projection eliminates all attributes of the input relation but those mentioned
in the projection list. The projection method defines a relation that contains a
vertical subset of Relation.
Example of Projection:
Rename (ρ)
ρ (a/b)R will rename the attribute ‘b’ of relation by ‘a’. Here a is new attribute
name and b is old attribute name.
Ρ (studid/id) student
Example
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
– Symbol denotes it. The result of A – B, is a relation which includes all tuples
that are in A but not in B.
Example
A-B
Table A – B
column 1 column 2
1 2
Intersection
A∩B
Defines a relation consisting of a set of all tuple that are in both A and B.
However, A and B must be union-compatible.
Visual
Definition of Intersection
Example:
A∩B
Table A ∩ B
column 1 column 2
1 1
Join Operations
JOIN operation also allows joining variously related tuples from different
relations.
Types of JOIN:
Inner Joins:
Outer join:
Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included,
while the rest are excluded. Let’s study various types of Inner Joins:
For example:
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also
include some or all tuples that do not match the criteria.
In the left outer join, operation allows keeping all tuple in the left relation.
However, if there is no matching tuple is found in right relation, then the
attributes of right relation in the join result are filled with null values.
A
Num Square
2 4
3 9
4 16
B
Num Cube
2 8
3 18
5 75
A B
A⋈B
Num Square Cube
2 4 8
3 9 18
4 16 –
In the right outer join, operation allows keeping all tuple in the right relation.
However, if there is no matching tuple is found in the left relation, then the
attributes of the left relation in the join result are filled with null values.
A B
A⋈B
Num Cube Square
2 8 4
3 18 9
5 75 –
Full Outer Join: ( A B)
In a full outer join, all tuples from both relations are included in the result,
irrespective of the matching condition.
A B
A⋈B
Num Cube Square
2 4 8
3 9 18
4 16 –
5 – 75