























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























Oct 2017
9–16 Query Evaluation (1.1.0)
✥ What kind of info does the DBMS store in its catalog?
✥ How does the DBMS answer a query?
✥ What algorithms are used to perform a relational algebra operation?
✥ What are evaluation plans and how are they represented?
✥ Why do we want to get the best evaluation plan?
✥ Each database contains tables about the data contained in it. ✦ Table: Name, attributes, indexes, integrity constraints ✦ Index: Name, structure, search key ✦ View: Name and definition
✥ And also about the DBMS itself: ✦ Size of buffer pool, page size...
✥ Also statistics: ✦ Cardinality : Number of tuples in R: N T uples(R) ✦ Size : Number of pages in R: N P ages(R) ✦ Index Cardinality : Number of distinct key values for index I: N K eys(I ) ✦ Index Size : Number of pages in index (for B-tree, number of leaf pages): I N P ages(I ) ✦ Index Height : The number of non-leaf levels in an index I: I H eight(I ) ✦ Index Range : Maximum and minimum values for the key of an index I: I Low(I ) and I M ax(I )
✥ Indexing: For selection or join, use index
✥ Iteration: Examine each tuple
✥ Partitioning: Partition tuples on a sort key then work on smaller sets (sorting and hashing)
✥ An access path is a way of retrieving tuples from a table
✥ Two ways to do it: ✦ Scan the file ✦ Use an index, retrieve data (for some queries, we might not need to retrieve data)
✥ Every relational operator accepts one or more tables as input; the access paths have a big impact in their cost.
✥ A hash index matches a CNF selection if there is a term of the
form attribute = value for each attribute in the index’s search key
✥ A tree index matches a CNF selection if there is a term of the form attribute op value for each attribute in a prefix of the index’s search key: ✦ If (a) and (a, b) are prefixes of key (a, b, c) ✦ For a tree index we can also evaluate comparisons different than equality , but not for a hash index
✥ For a table with an index we have 2 access paths (and potentially
✥ The selectivity of an access path is the number of pages retrieved
(index pages plus data pages) if we use this access path to retrieve the tuples ✥ The most selective access path is the one that retrieves the fewest pages
✥ The selectivity of an access path depends on its conjuncts
✥ Each conjunct acts as a filter
✥ The fraction of tuples that satisfy a given conjunct is called its
reduction factor
✥ When there are several conjuncts, the fraction of tuples that satisfy all of them can be approximated by the product of their reduction factors ( when is this not true? )
✥ Assume we have a hash index H on Reserves with search key
(rname, bid, sid) ✥ We are given the CNF query: rname =I J oeI ∧ bid = 5 ∧ sid = 3 ✥ The catalog contains the number of distinct keys for H:
N K eys(H ), and the number of pages N P ages(Reserves), so we can approximate the reduction factor of this access plan: N P ages (Reserves) N K eys(H )
✥ Assume now that we have an index on (bid, sid) and the CNF query is bid = 5 ∧ sid = 3
✥ If we know the number of different values for bid we can estimate the reduction factor of the first conjunct
✥ But usually, the DBMS does not, so it approximates to 0.
✥ So we can approximate this query’s selectivity as 0.
✥ But the number of pages retrieved depends on whether the index is clustered or not
For a query σR.attr op value(R) we have the following alternatives:
✥ No index: scan ✥ Index, depends: ✦ it is clustered or unclustered? ✦ what is the reduction factor of the expression? ✥ Rule of thumb: for an unclustered index, if over 5% of tuples are expected to match, then do scan
✥ Simple to implement, except when DISTINCT is used
✥ If no duplicates need to be eliminated: ✦ Simple retrieve the tuples and eliminate unwanted columns ✦ We might be able to do this with an index. How?
✥ If we need to drop duplicates, we need to sort the data ✦ 1. Remove columns, sort, eliminate duplicates ✦ 2. Remove columns and do first scan of sort, then keep doing sort, but in last pass of sort, eliminate duplicates
✥ It is a common and expensive operation ✥ For example: a join of Reserves.sid = Sailors.sid ✥ Suppose we have an index for Sailors on the sid column ✥ We can scan Reserves and find the matching Sailor. ✥ This algorithm is known as index nested loops join ✥ Assume we have a hash-based index using Alternative 2 on sid of Sailors, and takes 1.2 I/Os on average to retrieve index entry. ✦ There is one sailor per sid, hence one tuple to retrieve per reservation ✦ Reserves is 1000 pages long (100 tuples per page) ✦ Total cost: 2.2 ∗ 10 5 I/Os
9–18 Query Evaluation (1.1.0)
✥ Sort both tables, then join ✥ Called Sort-Merge Join ✥ Assume we can sort them in 2 passes each ✥ Cost: ✦ Sailors is 500 pages, Reserves is 1000 ✦ Cost of sorting: 4 ∗ (500 + 1000) = 6000 ✦ One more scan of sorted tables ✦ Total: 7500 I/Os ✥ Cheaper, and data already sorted!
9–19 Query Evaluation (1.1.0)