Download presentation
Presentation is loading. Please wait.
1
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Email: pusheng@cs.umn.edupusheng@cs.umn.edu Feb 3, 2004
2
15.2 CSCI 5708, Spring 2004. University of Minnesota Outline Basic Steps in Query Processing Measures of Query Cost Strategies for relational operations and Cost Analysis Select Operation External Sorting Join Operation Other Operations Evaluation of Expressions
3
15.3 CSCI 5708, Spring 2004. University of Minnesota Basic Steps in Query Processing FIGURE 15.1 PP 494 Statistics about Data
4
15.4 CSCI 5708, Spring 2004. University of Minnesota Basic Steps in Query Processing (Cont.) Scanning, Parsing, and Validating translate the query into its internal form. This is then translated into relational algebra. Parser checks syntax, verifies relations Evaluation The query-execution engine takes a query-evaluation plan, executes that plan, and returns the answers to the query.
5
15.5 CSCI 5708, Spring 2004. University of Minnesota Basic Steps in Query Processing : Optimization A relational algebra expression may have many equivalent expressions E.g., SALARY < 50K ( SALARY (EMPLOYEE)) is equivalent to SALARY ( SALARY < 50K (EMLOYEE)) Each relational algebra operation can be evaluated using one of several different algorithms Correspondingly, a 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 SALARY to find employees with SALARY < 50k, or can perform complete relation scan and discard employees with SALARY 50k
6
15.6 CSCI 5708, Spring 2004. University of Minnesota Basic Steps: Optimization (Cont.) Query Optimization: Amongst all equivalent evaluation plans choose the one with lowest cost. Cost is estimated using statistical information from the database catalog e.g. number of tuples in each relation, size of tuples, etc. In query processing we study How to measure query costs Algorithms for evaluating relational algebra operations How to combine algorithms for individual operations in order to evaluate a complete expression In query optimization We study how to optimize queries, that is, how to find an evaluation plan with lowest estimated cost. Heuristic rules and systematically estimating
7
15.7 CSCI 5708, Spring 2004. University of Minnesota Measures of Query Cost Cost is generally measured as total elapsed time for answering query Many factors contribute to time cost disk accesses, CPU, or even network communication Typically disk access is the predominant cost, and is also relatively easy to estimate. Measured by taking into account Number of seeks * average-seek-cost Number of blocks read * average-block-read-cost Number of blocks written * average-block-write-cost Cost to write a block is greater than cost to read a block –data is read back after being written to ensure that the write was successful
8
15.8 CSCI 5708, Spring 2004. University of Minnesota Measures of Query Cost (Cont.) For simplicity we just use number of block transfers from disk as the cost measure We ignore the difference in cost between sequential and random I/O for simplicity We also ignore CPU/communication costs for simplicity I/O cost DEPENDS on Search criteria: point/range query on a ordering / other fields File structures: heap, sorted, hashed Index types: primary, clustering, secondary, B+ tree, multilevel, … Other factors: e.g., buffering, disk placement, materialization, overflow / free space mgmt, … We do not include cost to writing output to disk in our cost formulae
9
15.9 CSCI 5708, Spring 2004. University of Minnesota Catalog Information for Cost Estimation n r : number of tuples in a relation r. b r : number of blocks containing tuples of r. s r : size of a tuple of r. bfr: blocking factor of r — i.e., the number of tuples of r that fit into one block. d(A, r): number of distinct values that appear in r for attribute A. sl(A,r): selectivity (fraction of records satisfying an equality condition in r for attribute A. sc(A,r): selection cardinality of attribute A of relation r; average number of records that satisfy equality on A x: number of index levels e = Pr[a record in overflow area] * E(overflow chain length)
10
15.10 CSCI 5708, Spring 2004. University of Minnesota Selection Operation File scan – search algorithms that locate and retrieve records that fulfill a selection condition. Algorithm S1 (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) = b r b r denotes number of blocks containing records from relation r If selection is on a key attribute, cost = ( b r /2) stop on finding record Linear search can be applied regardless of selection condition or ordering of records in the file, or availability of indices
11
15.11 CSCI 5708, Spring 2004. University of Minnesota Selection Operation (Cont.) S2 (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) / bfr - 1 — Plus number of other blocks containing records that satisfy selection condition If selection is on a key attribute, cost = log 2 (b r )
12
15.12 CSCI 5708, Spring 2004. University of Minnesota Selections (cont.) Index scan – search algorithms that use an index selection condition must be on search-key of index. S3a (primary index, equality). Retrieve a single record that satisfies the corresponding equality condition Cost = x + 1 S3b (hash key). Retrieve a single record Cost = e + 1 S4 (primary index, comparison). (Relation is sorted on A) For A V (r) use index to find first tuple v and scan relation sequentially from there. Rough cost = x + (b r /2), For A V (r) just scan relation sequentially till first tuple > v; do not use index.
13
15.13 CSCI 5708, Spring 2004. University of Minnesota Selection (cont.) S5 (equality on clustering index to retrieve multiple records) Cost = x + sc(A,r) / bfr S6a (equality on search-key of secondary index). Retrieve a single record if the search-key is a candidate key Cost = x + 1 Retrieve multiple records if search-key is not a candidate key Cost = x + number of records retrieved –Can be very expensive! each record may be on a different block – one block access for each retrieved record – worst cost = x + sc(A,r)
14
15.14 CSCI 5708, Spring 2004. University of Minnesota Selections (cont.) S6b (secondary index, comparison). For A V (r) use index to find first index entry v and scan index sequentially from there, to find pointers to records. For A V (r) just scan leaf pages of index finding pointers to records, till first entry > v In either case, retrieve records that are pointed to –requires an I/O for each record – Linear file scan may be cheaper if many records are to be fetched!
15
15.15 CSCI 5708, Spring 2004. University of Minnesota Implementation of Complex Selections Conjunction: 1 2 ... n (r) S7 (conjunctive selection using one index). Select a combination of i and algorithms S2 through S6 that results in the least cost for i (r). Test other conditions on tuple after fetching it into memory buffer. S8 (conjunctive selection using multiple-key index). Use appropriate composite (multiple-key) index if available. S9 (conjunctive selection by intersection of identifiers). Requires indices with record pointers. Use corresponding index for each condition, and take intersection of all the obtained sets of record pointers. Then fetch records from file If some conditions do not have appropriate indices, apply test in memory.
16
15.16 CSCI 5708, Spring 2004. University of Minnesota Algorithms for Complex Selections Disjunction: 1 2 ... n (r). S10 (disjunctive selection by union of identifiers). Applicable if all conditions have available indices. Otherwise use linear scan. Use corresponding index for each condition, take union of all the obtained sets of record pointers, and eliminate duplicates Fetch data from file Negation: (r) Use linear scan on file If very few records satisfy , and an index is applicable to Find satisfying records using index and fetch from file
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.