CH 02
CH 02
1
Introduction
Query Processing
Activities involved in retrieving data from the database.
2
Query Processing…
3
Basic Steps in Query Processing
1. Parsing and translation
2. Optimization
3. Evaluation
3-4
Parsing and translation
Scanner: The scanner specifies and recognizes the language tokens such as
SQL Keywords, attribute names, and relation names in the text of the query.
Validation: The query must be validated by checking that all attributes and
relation names are valid and semantically meaningful names in the schema of
3-5
Parsing and translation
as branches
Each operator has implementation choices.
6
Translating SQL Queries into Relational Algebra
Query block:
The basic unit that can be translated into the algebraic operators
and optimized.
Nested queries:
7
Translation Example
balance(balance<2500(account))
3-8
Translating SQL Queries into Relational Algebra
9
Translating SQL Queries into Relational Algebra
10
Translating SQL Queries into Relational Algebra
Relational Algebra:
Bala( bala>100(Account))
bala>100( Bala (Account)) both are equivalent query i.e. they display the
same results.
query optimization.
13
Evaluation
When the query came how the database answer it?
3-14
Relational Algebra: overview
Project (unary)
A1 A 2 A 3 … A n A1 A 2… A k
i j
...
...
n K, n≥k 15
Project
16
Relational Algebra: Select
Select or Restrict
<predicate> (R)
17
Pictorially
Movie title year length filmType
Star Wars 1977 124 color
Mighty result set
1991 104 color
Ducks
Wayne’s
1992 95 color
World
A1 A 2 A 3 … A n A1 A 2 A 3 … A n
i j, i j
...
...
# of selected tuples is referred to as the selectivity of the condition
18
Cartesian Product
R x S
19
Example
R S
A B B C D A R.B S.B C D
1 2 2 5 6 1 2 2 5 6
x
3 4 4 7 8 1 2 4 7 8
9 10 11 1 2 9 10 11
3 4 2 5 6
3 4 4 7 8
3 4 9 10 11
20
Join Operations
21
Example
R S
A B B C D A B C D
1 2 2 5 6 1 2 5 6
join
3 4 4 7 8 3 4 7 8
9 10 11
22
Optimization
A relational algebra expression may have many equivalent expressions
E.g.,salary75000(salary(instructor)) is equivalent to
salary(salary75000(instructor))
3-24
Three Key Concepts in QPO
1. Building blocks
Similarly, most DBMS have few building blocks:
• select (point query, range query), join, sorting, ...
SQL query is decomposed in building blocks
balancebalance<2500(account))
balance
balance<2500
account
3-27
Making An Evaluation Plan
Annotate Query Tree with evaluation instructions:
balance
account
The query can now be executed by the query execution engine.
3-28
Tree Representation of Relational Algebra
A1,,,,Anp( R1 x,….Rk))
A1,,,An
P
x Rk
x
R1 R3
R2 3-29
Why Learn about QPO?
Why learn about QPO in a DBMS?
Identify performance bottleneck for a query
• is it the physical data model or QPO ?
How to help QPO speed up processing of a query ?
• providing hints, rewriting query, etc.
How to enhance physical data model to speed up
queries?
• add indices, change file- structures, …
3-30
Measures of Query Cost
Cost is generally measured as total elapsed time for answering
query
Many factors contribute to time cost
3-31
Algorithms for select operations
Implementing the SELECT Operations
32
Search Methods for Simple Selection
S1.Linear search (brute force algorithm) : Retrieve every
record in the file, and test whether its attribute values
satisfy the selection condition.
33
Search Methods for Simple Selection
S3. Using a primary index : If the selection condition involves
an equality comparison on a key attribute with a primary index.
for example, Eid = ‘123’ use the primary index to retrieve the
record. Note that this condition retrieves a single record (at
most).
3-35
Sorting
Efficient evaluation for many operations
Implementations
Internal sorting (if records fit in main memory)
External sorting
36
Why Sort?
A classic problem in computing
accesses. 3-37
External Sorting
Refers to sorting algorithms that are suitable for large
files of records stored on disk that do not fit entirely in
main memory.
38
Basic External Sorting Algorithm
Assume unsorted data is on disk at start
Let M = maximum number of records that can be stored & sorted in internal
memory at one time
Algorithm
Sort phase:
First divide the file into runs such that the size of runs small enough to
fit into main memory.
1. Read M records into main memory & sort internally.
2. Write this sorted sub-list onto disk. (This is one “run”).
Until all data is processed into runs
Merge phase:
1. Merge two runs into one sorted run by reading the first block of runs.
2. Pass first recodes to buffer blocks till buffer block is full
3. Write this output back to disk
4. When a block of a run is exhausted next block of the run is read
39
2-Way Sort: Requires 3 Buffers
Phase 1: PREPARE.
Read a page, sort it, write it. Main memory
only one buffer page is used
1 buffer OUTPUT
INPUT 2
3-40
Basic External Sorting
81 94 11 96 12 35 17 99 28 58 41 75 15
Unsorted Data on Disk
Assume M = 3 (M would actually be much larger, of course.)
First step is to read 3 data items at a time into main memory,
sort them and write them back to disk as runs of length 3.
11 81 94 17 28 99 15
12 35 96 41 58 75
41
Basic External Sorting
11 81 94 17 28 99 15
12 35 96 41 58 75
11 12 35 81 94 96
17 28 41 58 75 99
15
42
Basic External Sorting
11 12 35 81 94 96 15
17 28 41 58 75 99
Next step is to merge the runs of length 6 into runs of length 12.
11 12 17 28 35 41 58 75 81 94 96 99
15
43
Basic External Sorting
11 12 17 28 35 41 58 75 81 94 96 99
15
Next step is to merge the runs of length 12 into runs of length 24. Here we
have less than 24, so we’re finished.
11 12 15 17 28 35 41 58 75 81 94 96 99
44
Example 2
18 11 16 13 12 17 21 15 19 20 14
45
Sort-Merge Example
pass run
a 12
d 95 a 12
d 95 a 12
a 12 R1 d 95
x 44 b 38
x 44 f 12
d 95
s 95 f 12 o 73
e 87
f 12 d 95 o 73 s 95
R2 f 12
o 73 a 12 s 95 x 44 pass
n 67
t 45 x 44 o 73
file n 67 e 87 b 38
memory s 95
e 87 n 67 e 87
R3 t 45
z 11 t 45 n 67
v 22
v 22 t 45
b 38 x 44
b 38 v 22
R4 v 22 z 11
z 11
z 11 46
Implementing the join operation
3-48
Methods for Implementing Joins
3-49
Methods for implementing joins
J4. Hash-join: The records of files R and S are both hashed to the
same hash file, using the same hashing function on the join attributes A
of R and B of S as hash keys.
First, a single pass through the file with fewer records (say, R) hashes
its records to the hash file buckets; this is called the partitioning
phase, since the records of R are partitioned into the hash buckets.
In the second phase, called the probing phase, a single pass through the
other file (S) then hashes each of its records to probe the appropriate
bucket, and that record is combined with all matching records from R in
that bucket.
This simplified description of hash-join assumes that the smaller of the
two files fits entirely into memory buckets after the first phase.
3-50
methods of query optimization
There are two methods of query optimization.
1. Cost based Optimization (Physical)
This is based on the cost of the query. The query can use
different paths based on indexes, constraints, sorting
methods etc.
This method mainly uses the statistics like record size,
number of records, number of records per block, number of
blocks, table size, whether whole table fits in a block,
organization of tables, uniqueness of column values, size of
columns etc
3-51
methods of query optimization cont…
2. Rule based optimization:
Use heuristics, called query rewrite rules
eliminate many of the really bad plans
Rules that will improve performance with very high
probability
Getting queries into a form that we know how to handle best
This method creates relational tree for the given query
based on the equivalence rules.
When these equivalence rules provide an alternative way of
writing and evaluating the query, gives the better path to
evaluate the query.
3-52
Query Rewrite Rules
Transform one logical plan into another
Do not use statistics
Equivalences in relational algebra
Push-down predicates
Write projects early
Avoid cross-products if possible
Use left-deep trees
Use of constraints, e.g., uniqueness
3-53
Query Rewrite Rules
First, move SELECT operations down the query tree
Second, perform the more restrictive SELECT operations
first
Third, replace CARTESIAN PRODUCT and SELECT
combinations with JOIN operations
Finally, move PROJECT operations down the query tree
This is called heuristic optimization
3-54
Example Query
Select B,D
From R,S
Where R.A = “c” R.C=S.C
3-55
Initial Logical Plan
B,D
Select B,D
From R,S R.A = “c” Λ R.C = S.C
Where R.A = “c”
R.C=S.C X
R S
3-56
Apply Rewrite Rule (1)
B,D B,D
R.C = S.C
R.A = “c” Λ R.C = S.C
R.A = “c”
X
X
R S
R S
3-58
Apply Rewrite Rule (2)
B,D B,D
R.C = S.C
R.A = “c” R.C = S.C
R.A = “c” S
X
R S R
B,D [ R.C=S.C [R.A=“c”(R)] X S]
3-59
• How do we execute this query?
Select B,D
From R,S
Where R.A = “c” S.E = 2
R.C=S.C
- Do Cartesian product
- Select tuples
One idea - Do projection
3-60
R A B C S C D E
a 1 10 10 x 2
b 1 20 20 y 2
c 2 10 30 z 2
d 2 35 40 x 1
e 3 45 50 y 3
Select B,D
From R,S Answer B D
Where R.A = “c” S.E = 2
R.C=S.C
2 x
3-61
An Example (cont.)
Plan 1
Cross product of R & S
Select tuples using WHERE conditions
Project on B & D
Algebra expression
B,D
R S
62
RXS R.A R.B R.C S.C S.D S.E
Select B,D
a 1 10 10 x 2
From R,S
S.E
Where R.A = “c”
= 2 R.C=S.C
a 1 10 20 y 2
.
.
Found!
c 2 10 10 x 2
Got one... .
.
3-63
An Example (cont.)
Plan 2
Select R tuples with R.A=“c”
Select S tuples with S.E=2
Natural join
Project B & D
R S
64
Relational Algebra Primer
3-65
Another idea:
Plan II B,D
natural join
R.A = “c” S.E = 2
R(A,B,C) S(C,D,E)
Select B,D
From R,S
Where R.A = “c” S.E = 2
R.C=S.C
3-66
Query Evaluation
How to evaluate individual relational operation?
Selection: find a subset of rows in a table
Join: connecting tuples from two tables
Other operations: union, projection, …
67
Algebraic Laws
Commutative and Associative Laws
R U S = S U R, R U (S U T) = (R U S) U T
R ∩ S = S ∩ R, R ∩ (S ∩ T) = (R ∩ S) ∩ T
3-68
Algebraic Laws
Laws involving selection:
C AND C’ (R) = C( C’(R)) = C(R) ∩ C’(R)
C OR C’ (R) = C(R) U C’(R)
C (R U S) = C (R) U C (S)
• When C involves only attributes of R
C (R
S) = (R) S
C
C (R – S) = C (R) – S
C (R ∩ S) = C (R) ∩ S
3-69
Transformation Rules for RA Operations
70
3-70
Transformation Rules for RA Operations
Commutativity of Selection.
p(q(R)) = q(p(R))
For example:
branchNo='B003'(salary>15000(Staff)) =
salary>15000(branchNo='B003'(Staff))
71
3-71
Disk Structure
72
3-72
The Storage Hierarchy
Smaller, Faster
Bigger, Slower
3-73
Disks and Files
DBMS stores information on disks.
This has major implications for DBMS design!
READ: transfer data from disk to main memory (RAM).
WRITE: transfer data from RAM to disk.
Both are high-cost operations, relative to in-memory
operations, so must be planned carefully!
3-74
Components of a Disk Spindle
Tracks
Disk head
moved in or out to
position a head on a
desired track.
Tracks under heads Arm movement
Platters
make a cylinder
(imaginary!).
Arm assembly
Block size is a multiple
of sector size (which is fixed).
75
Disks
3-76
Accessing a Disk Page
Time to access (read/write) a disk block:
seek time (moving arms to position disk head on track)
rotational delay (waiting for block to rotate under head)
transfer time (actually moving data to/from disk surface)
3-77
Index and index structure
Index is
Mechanism for efficiently locating rows without having to
scone entire table. Ex author catalog in library
Based on a search key: rows having a particular value for
the search key attributes can be quickly located.
Candidate key-set of attributes, quarantines uniqueness.
Search key:- sequence of attributes, does not guarantee
uniqueness.
This minimize the no of disk access required or it’s the way
of optimizing the performance of database
3-78
Structure of index
Search Key - attribute to set of attributes used to look
up records in a file.
An index file consists of records (called index entries) of
the form
Index files are typically much smaller than the original
file
search-key pointer
Space overhead- how much extra byte need for the index
itself.
3-80
Classification of Indexing
In an ordered index, index entries are stored sorted on the search
key value
Eg. Author catalog in library
Primary index: in a sequentially ordered file, the index whose search
key specifies the sequential order of the actual file. Also called
clustering index.
Index entry is created for first record of each block
No of index entries= no of blocks
Secondary index: an index whose search key specifies an order
different from the sequential order of the file. Also called non-
clustering index. Number of entry in index file = number of entry in
main file
3-81
Primary Dense Index Files
Dense index — Index record appears for every search-key value in the file. Or
every entry for possible search key values. Faster but it requires more space to
store index itself.
E.g. index on ID
3-82
Dense Index Files (Cont.)
Dense index on dept_name , with instructor file sorted on dept_name
Don’t have a pointer to every records but one which has for search key
3-83
Primary Sparse Index Files
Sparse Index: contains index records for only some search-
key values
To locate a record with search-key value K :
Find index record with largest search-key value < K
Search file sequentially starting at the record to which the
index record points
You reach to the nearest record the follow pointer.
3-84
Sparse Index Files (Cont.)
Compared to dense indices:
Less space and less maintenance overhead for
insertions and deletions.
Generally slower than dense index for locating
records.
Good tradeoff: sparse index with an index entry for every
block in file, corresponding to least search-key value in the
block.
3-85
Problems with simple indexes
Ex 100,000 entries
If we create desen index it will have very large index
If create sparse index we may have 50,000 sparse
index.
Solution: create multiple sparse index
3-86
Multilevel Index
If primary index does not fit in memory, access becomes
expensive.
Solution: treat primary index kept on disk as a sequential
file and construct a sparse index on it.
outer index – a sparse index of primary index
inner index – the primary index file
If even outer index is too large to fit in main memory, yet
another level of index can be created, and so on.
Indices at all levels must be updated on insertion or
deletion from the file.
3-87
Multilevel Index (Cont.)
3-88
Multilevel Index B+-Tree Index Files
All the leafs have pointer/links with each other for faster
accesses like link list
Keys are used for directing a search to the proper key
3-89
B+-Tree Node Structure
Typical node
3-90
B+-Tree Node Structure
Advantage of B+-tree index files:
Automatically reorganizes itself with small, local
changes, in the time of insertions and deletions.
Reorganization of entire file is not required to maintain
performance.
Disadvantage of B+-trees:
Extra insertion and deletion overhead, space overhead.
B+-trees are used extensively
3-91
B+-Tree Node Structure
Internal nodes:
Internal (non-leaf) nodes contain at least ⌈n/2⌉
pointers, except the root node.
At most, an internal node can contain n pointers
Leaf nodes:
Leaf nodes contain at least ⌈n/2⌉ record pointers and
⌈n/2⌉ key values.
At most, a leaf node can contain n record pointers
and n key values.
Every leaf node contains one block pointer P to point to
next leaf node and forms a linked list
3-92
B+ Tree Insertion
B+ tree are filled from bottom and each entry is done at
the leaf node.
If a leaf node overflows −
• Split node into two parts.
• Partition at i = ⌊(m + 1)/2⌋.
• First i entries are stored in one node.
• Rest of the entries i + 1 on wards are
moved to a new node.
• ith key is duplicated at the parent of the leaf.
3-93
B+ Tree Insertion
If a non-leaf node overflows −
Split node into two parts.
Partition the node at i = ⌈m + 1/2⌉.
Entries up to i are kept in one node.
Rest of the entries are moved to a new
3-94
B+ Tree node structure
A B
3-95
B+ Tree Deletion
B+ tree entries are deleted at the leaf nodes.
The target entry is searched and deleted.
If it is an internal node, delete and replace with the
entry from the left position.
After deletion, underflow is tested,
If underflow occurs, distribute the entries from the
nodes left to it.
If distribution is not possible from left, then
Distribute from the nodes right to it.
If distribution is not possible from left or from right,
then
Merge the node with left and right to it.
3-96
Rules for Constructing a B+ Tree
Key values are max node of elements in the node
The number of key values contained in a non-leaf node is 1 less
than the number of pointers
So if n is the order key is M=n-1.
If the tree has order n, the number of occupied key values in a
leaf node must be between (n-1)/2 and n-1.
If (n-1)/2 is not an integer, round up to determine the minimum
number of occupied key values.
The tree must be balanced, that is, every path from the root node
must have the same length.
3-97
Rules for Constructing a B+ Tree
Example n=5
Node Min Node Max Node Min Node Max Node
Root 1 1
Internal N/2 N 3 5
node
Leaf node N/2 N-1 3 4
3-98
Searching
Since no structure change in a B+ tree during a searching
process, so just compare the key value with the data in the
tree, then give the result back.
3-99
Searching
Result:
1. For the value of 45, not found.
2. For the value of 15, return the position where
the pointer located.
3-100
Insertion
Since insert a value into a B+ tree may cause the tree
unbalance, so rearrange the tree if needed.
Example #1: insert 28 into the below tree.
25 28 30
3-101
Insertion
Result:
3-102
Insertion
Example #2: insert 70 into below tree
3-103
Insertion
Process: split the tree
50 55 60 65 70
50 55 60 65 70
104
Insertion
Result: chose the middle key 60, and place it
in the index page between 50 and 75.
3-105
Insertion
Exercise: add a key value 95 to the below
tree.
75 80 85 90 95
75 80 85 90 95 25 50 60 75 85
3-106
Insertion
Result: again put the middle key 60 to the
index page and rearrange the tree.
3-107
Deletion
Same as insertion, the tree has to be rebuild if the
deletion result violate the rule of B+ tree.
Example #1: delete 70 from the tree
This is OK.
60 65
3-108
Deletion
Result:
3-109
Deletion
Example #2: delete 25 from below tree, but 25
appears in the index page.
But…
28 30
This is OK.
3-110
Deletion
Result: replace 28 in the index page.
Add 28
3-111
Deletion
Example #3: delete 60 from the below tree
65
50 55 65 3-112
Deletion
Result: delete 60 from the index page and
combine the rest of index pages.
3-113
Example 1
Create B +Tree for the following data.
Insert: 50,75,100,120
M=2
3-114
Insert 50,75
50* 75* a
3-115
Insert 100
* 75 * a
b c
* 50 * * 75 *100*
3-116
Insert 8
* 75 *
* 50 * * 100 *
* 75 * 100* 120 *
3-117
Exercise 1
B+Tree of order n=4 then key=3
Insert: 2,4,7,10,17,21,28
3-118
Exercise 1
B+Tree of order n=6
Insert: 10,20,30,40,50,60,70,80,90
M=5
3-119
Exercise 2
B+Tree of order n=4
Insert: 20,15,5,1,3,9,2
M=3
3-120
Exercise 3
B+ Tree of order 3
Insert: 5,8,4,16,23,10,15
M=2
3-121
Conclusion
For a B+ Tree:
It is easy to maintain it’s balance.
The searching time is short than most of other types of
trees.
3-122
Query Execution Plans
3-123
Evaluation
evaluate multiple operations in a plan
materialization
πname
pipelining
σcoursename=Advanced DBs
courseid; index-nested
loop
course
cid; hash join
student takes
124
Materialization
create and read temporary relations
create implies writing to disk
πname
more page writes
σcoursename=Advanced DBs
courseid; index-nested
loop
course
cid; hash join
student takes
125
Pipelining (1/2)
creating a pipeline of operations
reduces number of read-write operations
implementations πname
demand-driven - data pull
producer-driven - data push σcoursename=Advanced DBs
ccourseid; index-
nested loop
course
cid; hash join
student takes
126
Pipelining (2/2)
can pipelining always be used?
any algorithm?
cost of R S
materialization and hash join: BR + 3(BR+BS)
pipelining and indexed nested loop join: NR *
HTi pipelined courseid
materialized
R S
cid
σcoursename=Advanced DBs
127