ADBMS TypicalQueryOptimizer
ADBMS TypicalQueryOptimizer
INTROUDUCTION:
The DBMS describes the data that it manages, including tables and indexes. This descriptive data or metadata stored in special tables called the system catalogs is used to find the best way to evaluate a query. SQL queries are translated into an extended form of relational algebra, and query evaluation plans are represented as trees of relational operators, along with labels that identify the algorithm to use at each code. Relational operators serve as building blocks for evaluating queries and the implementation of these operators is carefully optimized for good performance.
Queries are composed of several operators, and the algorithms for individual operators can be combined in many ways to evaluate a query. The process of finding a good evaluation plan is called query optimization.
Contd.,
The following information is commonly stored: Cardiality: # of tuples NTuples(R) for each table R Size: # of pages NPages(R) for each table R. Index Cardinality: # of distinct key values NKeys(I) for each index I. Index Size: # of pages INPages(I) for each index I. Index Height: # of nonleap levels IHeight(I) for each tree indiex I. Index Range: The minimum present key value ILow(I) and maximum present key value IHigh(I) for each index I. The database contains the two tables are:
Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid: integer, bid: integer, day: dates, rname: string)
Algorithms for evaluating relational operators use some simple ideas extensively: Indexing: Can use WHERE conditions to retrieve small set of tuples (selections, joins) Iteration: Sometimes, faster to scan all tuples even if there is an index. (And sometimes, we can scan the data entries in an index instead of the table itself.) Partitioning: By using sorting or hashing, we can partition the input tuples and replace an expensive operation by similar operations on smaller inputs.
Access Paths
An access path is a method of retrieving tuples from a table File scan, or index that matches a selection condition(in the query). -A tree index matches (a conjunction of) terms that involve only attributes in a prefix of the search key. E.g., Tree index on <a, b, c> matches the selection a=5 AND b=3, and a=5 AND b>6, but not b=3. -A hash index matches (a conjunction of) terms that has a term attribute = value for every attribute in the search key of the index. E.g., Hash index on <a, b, c> matches a=5 AND b=3 AND c=5; but it does not match b=3, or a=5 AND b=3, or a>5 AND b=3 AND c=5. A Note on Complex Selections -(day<8/9/94 AND rname=Paul) OR bid=5 OR sid=3) -Selection conditions are first converted to conjunctive normal form (CNF) (day<8/9/94 OR bid=5 OR sid=3 ) AND (rname=Paul OR bid=5 OR sid=3)
Contd., Find the most selective access path, retrieve tuples using it, and apply any remaining terms that dont match the index: Most selective access path: An index or file scan that we estimate will require the fewest page I/Os. Terms that match this index reduce the number of tuples retrieved; other terms are used to discard some retrieved tuples, but do not affect number of tuples/pages fetched. Consider day<8/9/94 AND bid=5 AND sid=3. A B+ tree index on day can be used; then, bid=5 and sid=3 must be checked for each retrieved tuple. Similarly, a hash index on <bid, sid> could be used; day<8/9/94 must then be checked.
Cost depends on #qualifying tuples, and clustering. Cost of finding qualifying data entries (typically small)plus cost of retrieving records (could be large w/o clustering). In example, assuming uniform distribution of names,about 10% of tuples qualify (100 pages, 10000 tuples). With a clustered index, cost is little more than 100 I/Os; if unclustered, upto 10000 I/Os! SELECT * FROM Reserves R WHERE R.rname < C%;
As a rule thumb, it is probably cheaper to simply scan the entire table(instead of using an unclustered index) if over 5% of the tuples are to be retrieved.
Contd., 2. Projection: The projection operation requires us to drop certain fields of the input, which is easy to do. The expensive part is removing duplicates. SQL systems dont remove duplicates unless the keyword DISTINCT is specified in a query. SELECT DISTINCT R.sid, R.bid FROM Reserves R; Sorting Approach: Sort on <sid, bid> and remove duplicates. (Can optimize this by dropping unwanted information while sorting.) Hashing Approach: Hash on <sid, bid> to create partitions. Load partitions into memory one at a time, build in-memory hash structure, and eliminate duplicates. If there is an index with both R.sid and R.bid in the search key, may be cheaper to sort data entries!
Contd., 3. Join: Joins are expensive operations and very common. This systems typically support several algorithms to carry out joins. Consider the join of Reserves and Sailors, with the join condition Reserves.sid=Sailors.sid. Suppose one of the tables, say Sailors, has an index on the sid column. We can scan Reserves and for each tuple, use the index to probe Sailors for matching tuples. This approach is called index nested loops join.
Ex: The cost of scanning Reserves and using the index to retrieve the matching Sailors tuple for each Reserves tuple. The cost of scanning Reserves is 1000. There are 100*1000 =100000 tuples in Reserves. For each of these tuples, retrieving the index page containing the rid of the matching Sailors tuple costs 1.2 I/Os(on avg), in addition we have to retrieve the Sailors page containing the qualifying tuple. Therefore we have 100000*(1+1.2) I/Os to retrieve matching Sailors tuples. The total cost is 221000 I/Os.
If we do not have an index that matches the join condition on either table, we cannot use index nested loops. In this case, we can sort both tables on the join column, and then scan them to find matches. This is called sort-merge join.
Ex: We can sort Reserves and Sailors in two passes. Read and Write Reserves in each pass the sorting cost is 2*2*1000=4000 I/Os. Similarly we can sort Sailors at a cost of 2*2*500=2000 I/Os. In addition, the second phase of the sortmerge join algorithm requires an additional scan of both tables. Thus the total cost is 4000+2000+1000+500=7500 I/Os.
Query optimization is one of the most important tasks of a relational DBMS. A more detailed view of the query optimization and execution layer in the DBMS architecture is as shown in Fig(1).Queries are parsed and then presented to query optimizer, which is responsible for identifying an efficient execution plan. The optimizer generates alternative plan and chooses the plan with the least estimated cost.
Query Parser
Parsed query
Query Optimizer
Plan Generator Plan cost Estimator
Catalog Manager
Evaluation Plan
The space of plans considered by a typical Fig(1). Query Parsing, Optimization & Execution relational query optimizer can be carried out on the result of the - algebra expression. Optimizing such a relational algebra expression involves two basic steps: 1. Enumerating alternative plans for evaluating the expression. Typically an optimizer considers a subset of all possible plans because the number of possible plans is very large. 2. Estimating the cost of each enumerated plan and choosing the plan with the lowest estimated cost.
Query Evaluation Plans: A query evaluation plan consists of an extended relational algebra tree, with additional annotations at each node indicating the access methods to use for each table and the implementation method to use for each relational operator. Ex: select s.sname from Reserves r, Sailors s where r.sid=s.sid and r.bid=100 and s.rating>5
Sailors))
bid=100 ^ rating>5
sid=sid
Reserves
Sailors
Contd., The algebra expression partially specifies how to evaluate the query we first compute the natural join of Reserves and Sailors, then perform the selections and finally project the sname field. To obtain a fully specified evaluation plan, we must decide on an implementation for each of the algebra operations involved. For ex, we can use a page-oriented simple nested loops join with Reserves as the outer table and apply selections and projections to each tuple in the result of the join as it is produced; the result of the join before the selections and projections is never stored in its entirety. This query evaluation plan is shown in Fig.(3).
Sname
(on-the-fly)
(File scan)Reserves
Sailors(File scan)
Alternative Plans 1 (No Indexes) as shown in fig(4): Main difference: push selects.
1. With 5 buffers, cost of plan: 2. Scan Reserves (1000) + write temp T1 (10 pages, if we have 100 boats, uniform distribution). 3. Scan Sailors (500) + write temp T2 (250 pages, if we have 10 ratings). 4. Sort T1 (2*2*10), sort T2 (2*3*250), merge (10+250) 5. Total: 3560 page I/Os. If we used BNL join, join cost = 10+4*250, total cost = 2770. If we `push projections, T1 has only sid, T2 only sid and sname: 1. T1 fits in 3 pages, cost of BNL drops to under 250 pages, total < 2000.
Contd.,
Sname
(on-the-fly)
bid=100
rating>5 (scan
(File scan)Reserves
Sailors(File scan)
Alternative Plans 2 With Indexes as shown in fig(5): With clustered index on bid of Reserves, we get 100,000/100 = 1000 tuples on
1000/100 = 10 pages. INL with pipelining (outer is not materialized). - Projecting out unnecessary fields from outer doesnt help. Join column sid is a key for Sailors - At most one matching tuple, unclustered index on sid OK. Decision not to push rating>5 before the join is based on availability of sid index on Sailors. Cost: Selection of Reserves tuples (10 I/Os); for each, must get matching Sailors tuple (1000*1.2); total 1210 I/Os.
Contd.,
Sname
(on-the-fly)
rating>5 (on-the-fly)
bid=100
Contd.,
Impact: -Most widely used currently; works well for < 10 joins. Cost estimation: Approximate art at best. -Statistics, maintained in system catalogs, used to estimate cost of operations and result sizes. -Considers combination of CPU and I/O costs. Plan Space: Too large, must be pruned. -Only the space of left-deep plans is considered. -> Left-deep plans allow output of each operator to be pipelined into the next operator without storing it in a temporary relation. -Cartesian products avoided.
Cost Estimation:
For each plan considered, must estimate cost: Must estimate cost of each operation in plan tree. Depends on input cardinalities. Weve already discussed how to estimate the cost of operations (sequential scan, index scan, joins, etc.) Must also estimate size of result for each operation in tree! Use information about the input relations. For selections and joins, assume independence of predicates.
Contd.,
Summary:
There are several alternative evaluation algorithms for each relational operator. A query is evaluated by converting it to a tree of operators and evaluating the operators in the tree. Must understand query optimization in order to fully understand the performance impact of a given database design (relations, indexes) on a workload (set of queries). Two parts to optimizing a query: Consider a set of alternative plans. - Must prune search space; typically, left-deep plans only. Must estimate cost of each plan that is considered. - Must estimate size of result and cost for each plan node. - Key issues: Statistics, indexes, operator implementations.
EXTERNAL SORTING
Why Sort?
A classic problem in computer science! Data requested in sorted order - e.g., find students in increasing gpa order Sorting is first step in bulk loading B+ tree index. Sorting useful for eliminating duplicate copies in a collection of records (Why?) Sort-merge join algorithm involves sorting. Problem: sort 1Gb of data with 1Mb of RAM. - why not virtual memory?
Fig(1). Two-Way Merge Sort proc 2-way_extsort (file) // Given a file on disk; sorts it using three buffer pages
Contd.,
Contd.,
If the number of pages in the input file is 2k, for some k, then:
Pass 0 produces 2k sorted runs of one page each,
Contd.,
The algorithm is illustrated on an example input file containing Two-way Merge Sort of a seven pages in fig(2).
3,4 3,4 2,3 4,6 2,3 6,2 2,6 9,4 8,7 5,6 3,1 2
4,9
4,7 8,9
7,8
5,6
1,3 5,6
1,3
4,4
6,7 8,9
4-page runs
Pass 3
6,6
7,8 9
8-page runs
The sort takes four passes, and in each pass, we read and write seven pages, for a total of 56 I/Os. This result agrees with the preceding analysis because 2.7([log27]+1)=56. The dark pages in the figure illustrate what would happen on a file of eight pages; the number of passes remains at four([log28]+1=4), but we read and write an additional page in each pass for a total of 64 I/Os. The algorithm requires just three buffer pages in main memory, as shown in fig(3) illustrates. This observation raises an important point: Even if we have more buffer space available, this simple algorithm does not utilize it effectively.
Fig(3) Two-Way Merge Sort with Three buffer pages
Contd.,
1. In pass 0, read in B pages at a time and sort internally to produce [N/B] runs of B pages each (except for the last run, which may contain fewer pages). This modification is illustrated in fig(4), using the input from fig(2) and a buffer pool with four pages.
2. In passes i=1,2, use B-1 buffer pages for input and use the remaining page for output; hence, you do a (B1)-way merge in each pass. The utilization of buffer pages in the merging passes is illustrated in fig(5).
Contd.,
Input file
5,6 3,1 2 8,9 7,8 1,2 3,5 6
Contd.,
Input 1 Input 2
Output
Summary
External sorting is important; DBMS may dedicate part of buffer pool for sorting! External merge sort minimizes disk I/O cost: Pass 0: Produces sorted runs of size B (# buffer pages). Later passes: merge runs. # of runs merged at a time depends on B, and block size. Larger block size means less I/O cost per page. Larger block size means smaller # runs merged. In practice, # of runs rarely more than 2 or 3.