Download Query Processing: Evaluation, Cost Estimation, and Join Operations and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Chapter 13
Presentation -- Query Processing
1
Introduction
• We are living in a world that is flooded with
information and the data that companies have to
depend on are mostly gigantic, manage and using
those data are diffcult. In order to use these data
efficiently people will have to learn more about
query processing which refers to activities involved in
extracting data from database.
2
Three Steps of Query Processing
1) The Parsing and translation will first translate the query
into its internal form, then translate the query into
relational algebra and verifies relations.
2) Optimization is to find the most efficient evaluation plan
for a query because there can be more than one way.
3) Evaluation is what the query-execution engine takes a
query-evaluation plan to executes that plan and returns
the answers to the query.
4
Steps of Query Processing
The Parsing and translation will first
translate the query into its internal form, then
translate the query into relational algebra and
verifies relations.
5
Steps of Query Processing
Optimization –
Also, any relational-algebra expression can be evaluated in
many ways. Annotated expression specifying detailed
evaluation strategy is called an evaluation-plan.
E.g. can use an index on balance to find accounts with
balance <2500, or can perform complete relation scan and
discard accounts with balance ≥ 2500
7
Steps of Query Processing
Optimization –
Within all equivalent expressions, programmers
should choose the query that is the most efficient
evaluation-plan to deduct the cost of estimating
evaluation-plan based on statistical information in
the database management system catalog.
8
Estimation Cost of Strategy (Plan)
- b (^) r : number of blocks containing tuples of r.
- n (^) r : number of tuples in relation r.
- f (^) r : blocking factor of r - i.e., the number of tuples of r that fit into one
block.
- s (^) r : size of a tuple of r in bytes.
- V(A, r) : number of distinct values that appear in r for attribute A ; same
as the size of Π A ( r ).
- SC ( A , r ): selection cardinality of attribute A of relation r ; average
number of records that satisfy equality on A.
- If tuples of r are stored together physically in a file, then:
10
r
r r f
n b
Catalog Information about Indices
- fi: average fan-out of internal node of index I , for tree-
structured indices such as B+-trees.
- HTi : number of levels in index i - i.e., the height of i.
- For a balanced tree index (such as B+-tree) on attribute A of relation r ,
- For a hash index, HTi is 1.
- LB (^) i : number of lowest-level index blocks in i - i.e., the number
of blocks at the leaf level of the index.
11
HTi = log (^) fi ( V ( A , r )
Measures of Query Cost
- Cost of algorithms depended on the size of the buffer
in main memory, as having more memory reduces
need for disk access. Thus memory size should be a
parameter while estimating cost; often use worst case
to estimate.
- We refer to the cost estimate algorithm A as E A . We
do not include cost of writing output to disk.
13
Selection Operation
Index structure are referred to as access
paths , since they provide a path through which
data can be located and accessed. A primary
index is an index that allows the records of a file
to be read in an order that corresponds to the
physical order in the file. An index that is not a
primary index is called a secondary index.
14
Selection Operation (primary index)
- File scan is the search algorithms that locate and retrieve
records that fulfill a selection condition.
- Algorithm A1 ( linear search ). Scan each file block and test
all records to see whether they satisfy the selection
condition.
- Cost estimate (number of disk blocks scanned) EA1 = b (^) r
- If selection is on a key attribute, EA1 = ( b (^) r / 2 ) (stop on finding
record)
- Linear search can be applied regardless of
Selection Operation (primary index)
- Algorithm A2 (binary search). Applicable if selection is an
equality comparison on the attribute on which file is
ordered.
- Assume that the blocks of a relation are stored contiguously
- Cost estimate (number of disk blocks to be scanned):
- log 2 (b (^) r ) — cost of locating the first tuple by a binary search on
the blocks
SC(A, r) — numbers of records that will satisfy the selection
SC(A,r)/f (^) r — number of blocks that these records will occupy
- Equality condition on a key attribute: SC(A,r) = 1; estimate
reduces to EA2 = log 2 (b (^) r )
17
1
2 log^2 ( ) −
r
A r f
SC A r E b
Selection Cost Estimate Example
σbranch-name = “Perryridge” (account)
- Number of blocks is b account = 500: 10,000 tuples in the
relation; each block holds 20 tuples.
- Assume account is sorted on branch-name.
- V(branch-name, account) is 50
- 10000/50 = 200 tuples of the account relation pertain to
Perryridge branch
- 200/20 = 10 blocks for these tuples
- A binary search to find the first record would take
- log 2 (500) = 9 block accesses
- Total cost of binary search is 9 +10 –1 = 18 block accesses
(versus 500 for linear scan)
19
Selections Using Indices
- Index scan- search algorithms that use an index; condition is on
search-key of index.
- A3(primary index on candidate key, equality). Retrieve a single
record that satisfies the corresponding equality condition. EA3 =
HTi +
- A4(primary index on nonkey, equality) Retrieve multiple
records. Let the search-key attribute be A.
- A5(equality on search-key of secondary index).
- Retrieve a single record if the search-key is a candidate key
EA5 = HTi + 1
- Retrieve multiple records (each may be on a different block) if the
search-key is not a candidate key. EA5 = HTi + SC(A, r) 20
= + r
A i f
SC A r E HT
( , ) 4