Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Implementation of Relational Operators. 2 Steps of processing a high-level query Parser Query Optimizer StatisticsCost Model QEPParsed Query Database.

Similar presentations


Presentation on theme: "1 Implementation of Relational Operators. 2 Steps of processing a high-level query Parser Query Optimizer StatisticsCost Model QEPParsed Query Database."— Presentation transcript:

1 1 Implementation of Relational Operators

2 2 Steps of processing a high-level query Parser Query Optimizer StatisticsCost Model QEPParsed Query Database High Level Query Query Result Query Evaluator

3 3 Relational Operations v We will consider how to implement: – Selection (  ) Selects a subset of rows from relation. – Projection (  ) Deletes unwanted columns from relation. – Join ( ) Allows us to combine two relations. – Set-difference ( - ) Tuples in reln. 1, but not in reln. 2. – Union ( U ) Tuples in reln. 1 and in reln. 2. – Aggregation (SUM, MIN, etc.) and GROUP BY

4 4 Paradigm v Iteration v Index –B+-tree, Hash u assume index entries to be (rid,pointer) pair –Clustered, Unclustered v Sort v Hash

5 5 Schema for Examples v Reserves (R): –p R tuples per page, M pages. p R = 100. M = 1000. v Sailors (S): –p S tuples per page, N pages. p S = 80. N = 500. v Cost metric: # of I/Os (pages) –We will ignore output costs in the following discussion. Sailors ( sid : integer, sname : string, rating : integer, age : real) Reserves ( sid : integer, bid : integer, day : dates, rname : string)

6 6 Equality Joins With One Join Column v In algebra: R S. v Most frequently used operation; very costly operation. v join_selectivity = join_size/(#R tuples  #S tuples) SELECT * FROM Reserves R, Sailors S WHERE R.sid=S.sid

7 7 Join Example

8 8

9 9 Simple Nested Loops Join v For each tuple in the outer relation R, we scan the entire inner relation S. –I/O Cost? –Memory? foreach tuple r in R do foreach tuple s in S do if r.sid == s.sid then add to result

10 10 Simple Nested Loops Join v For each tuple in the outer relation R, we scan the entire inner relation S. –Cost: M + p R * M * N = 1000 + 100*1000*500 I/Os. foreach tuple r in R do foreach tuple s in S do if r.sid == s.sid then add to result

11 11 Simple Nested Loops Join v For each tuple in the outer relation R, we scan the entire inner relation S. –Cost: M + p R * M * N = 1000 + 100*1000*500 I/Os. –Memory: 3 pages! foreach tuple r in R do foreach tuple s in S do if r.sid == s.sid then add to result

12 12 Block Nested Loops Join v Use one page as an input buffer for scanning the inner S, one page as the output buffer, and use all remaining pages to hold ``block’’ of outer R. –For each matching tuple r in R-block, s in S-page, add to result. Then read next R-block, scan S, etc.... R & S Hash table for block of R (k < B-1 pages) Input buffer for S Output buffer... Join Result

13 13 Examples of Block Nested Loops v Cost: Scan of outer + #outer blocks * scan of inner –#outer blocks ?

14 14 Examples of Block Nested Loops v Cost: Scan of outer + #outer blocks * scan of inner –#outer blocks =  no. of pages in outer relation / block size 

15 15 Examples of Block Nested Loops v Cost: Scan of outer + #outer blocks * scan of inner –#outer blocks =  no. of pages in outer relation / block size  v With R as outer, block size of 100 pages: –Cost of scanning R is 1000 I/Os; a total of 10 blocks. –Per block of R, we scan S; 10*500 I/Os. –If block size for just 90 pages of R, scan S 12 times.

16 16 Examples of Block Nested Loops v Cost: Scan of outer + #outer blocks * scan of inner –#outer blocks =  no. of pages in outer relation / block size  v With R as outer, block size of 100 pages: –Cost of scanning R is 1000 I/Os; a total of 10 blocks. –Per block of R, we scan S; 10*500 I/Os. –If block size for just 90 pages of R, scan S 12 times. v With 100-page block of S as outer?

17 17 Examples of Block Nested Loops v Cost: Scan of outer + #outer blocks * scan of inner –#outer blocks =  no. of pages in outer relation / block size  v With R as outer, block size of 100 pages: –Cost of scanning R is 1000 I/Os; a total of 10 blocks. –Per block of R, we scan S; 10*500 I/Os. –If block size for just 90 pages of R, scan S 12 times. v With 100-page block of S as outer: –Cost of scanning S is 500 I/Os; a total of 5 blocks. –Per block of S, we scan R; 5*1000 I/Os.

18 18 Index Nested Loops Join v If there is an index on the join column of one relation (say S), can make it the inner and exploit the index. –Cost: M + ( (M*p R ) * cost of finding matching S tuples) v For each R tuple, cost of probing S index is about 1.2 for hash index, 2-4 for B+ tree. Cost of then finding S tuples (assuming leaf data entries are pointers) depends on clustering. –Clustered index: 1 I/O (typical), unclustered: upto 1 I/O per matching S tuple. foreach tuple r in R do search index of S on sid using S search-key = r.sid for each matching key retrieve s; add to result

19 19 Examples of Index Nested Loops v Hash-index on sid of S (as inner): –Scan R: 1000 page I/Os, 100*1000 tuples. –For each R tuple: 1.2 I/Os to get data entry in index, plus 1 I/O to get (the exactly one) matching S tuple. Total: 220,000 I/Os. v Hash-index on sid of R (as inner)?

20 20 Examples of Index Nested Loops v Hash-index on sid of S (as inner): –Scan R: 1000 page I/Os, 100*1000 tuples. –For each R tuple: 1.2 I/Os to get data entry in index, plus 1 I/O to get (the exactly one) matching S tuple. Total: 220,000 I/Os. v Hash-index on sid of R (as inner): –Scan S: 500 page I/Os, 80*500 tuples. –For each S tuple: 1.2 I/Os to find index page with data entries, plus cost of retrieving matching R tuples. –Assuming uniform distribution, 2.5 reservations per sailor (100,000 / 40,000). Cost of retrieving them is 1 or 2.5 I/Os depending on whether the index is clustered.

21 21 Sort-Merge Join v Sort R and S on the join column, then scan them to do a ``merge’’ (on join col.), and output result tuples. –Advance scan of R until current R-tuple >= current S tuple, then advance scan of S until current S-tuple >= current R tuple; do this until current R tuple = current S tuple. –At this point, all R tuples with same value in Ri (current R group) and all S tuples with same value in Sj (current S group) match; output for all pairs of such tuples. –Then resume scanning R and S. v R is scanned once; each S group is scanned once per matching R tuple. (Multiple scans of an S group are likely to find needed pages in buffer.)

22 22 Example of Sort-Merge Join v Cost?

23 23 Example of Sort-Merge Join v Cost: 2M*K 1 + 2N*K 2 + (M+N) –K 1 and K 2 are the number of passes to sort R and S respectively –The cost of scanning, M+N, could be M*N (very unlikely!)

24 24 Example of Sort-Merge Join v Cost: 2M*K 1 + 2N*K 2 + (M+N) –K 1 and K 2 are the number of passes to sort R and S respectively –The cost of scanning, M+N, could be M*N (very unlikely!) v With 35, 100 or 300 buffer pages, both R and S can be sorted in 2 passes; total join cost: 7500. ( BNL cost: 2500 to 15000 I/Os )

25 25 GRACE Hash-Join v Operates in two phases: –Partition phase u Partition relation R using hash fn h. u Partition relation S using hash fn h. u R tuples in partition i will only match S tuples in partition i. –Join phase u Read in a partition of R u Hash it using h2 (<> h!) u Scan matching partition of S, search for matches.

26 26 GRACE Hash-Join X X X R S 0 1 2 3 0 1 2 3 bucketID = X mod 4

27 27 Partitioning Phase B main memory buffers Disk Original Relation OUTPUT 2 INPUT 1 hash function h B-1 Partitions 1 2 B-1...

28 28 Joining Phase Partitions of R & S Input buffer for Si Hash table for partition Ri (k < B-1 pages) B main memory buffers Disk Output buffer Disk Join Result hash fn h2

29 29 Cost of Hash-Join v In partitioning phase, read+write both relns –2(M+N). v In matching phase, read both relns –M+N I/Os. v In our running example, this is a total of 4500 I/Os.

30 30 Observations on Hash-Join v #partitions k  B-1 (why?), and B-2  size of largest partition to be held in memory. Assuming uniformly sized partitions, and maximizing k, we get: –k= B-1, and M/(B-1)  B-2, i.e., B must be   M v If we build an in-memory hash table to speed up the matching of tuples, a little more memory is needed. v If the hash function does not partition uniformly, one or more R partitions may not fit in memory. Can apply hash-join technique recursively to do the join of this R-partition with corresponding S-partition. v What if B <  M ?

31 31 General Join Conditions v Equalities over several attributes (e.g., R.sid=S.sid AND R.rname=S.sname): –Join on one predicate, and treat the rest as selections; –For Index NL, build index on (if S is inner); use existing indexes on sid or sname. –For Sort-Merge and Hash Join, sort/partition on combination of the two join columns v Inequality join (R.sid < S.sid)?

32 32 Simple Selections v Of the form:  R.attr op value (R) v Size of result = R * selectivity v With no index, unsorted: Must essentially scan the whole relation; cost is M (#pages in R).  With an index on selection attribute: Use index to find qualifying data entries, then retrieve corresponding data records. (Hash index useful only for equality selections.) SELECT * FROM Reserves R WHERE R.rname < ‘C%’

33 33 Using an Index for Selections v 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). u Clustered index? u Unclustered index?

34 34 Using an Index for Selections v 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). u Clustered index: ~ 100 I/Os u Unclustered: upto 10000 I/Os!

35 35 Two Approaches to General Selections v First approach: Find the most selective access path, retrieve tuples using it, and apply any remaining terms that don’t 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 could be used; day<8/9/94 must then be checked.

36 36 Intersection of Rids v Second approach (if we have 2 or more matching indexes (assuming leaf data entries are pointers): –Get sets of rids of data records using each matching index. –Then intersect these sets of rids (we’ll discuss intersection soon!) –Retrieve the records and apply any remaining terms. –Consider day<8/9/94 AND bid=5 AND sid=3. If we have a B+ tree index on day and an index on sid, we can retrieve rids of records satisfying day<8/9/94 using the first, rids of recs satisfying sid=3 using the second, intersect, retrieve records and check bid=5.

37 37 The Projection Operation (Duplicate Elimination) v An approach based on sorting: –Modify Pass 0 of external sort to eliminate unwanted fields. Thus, runs are produced, but tuples in runs are smaller than input tuples. (Size ratio depends on # and size of fields that are dropped.) –Modify merging passes to eliminate duplicates. Thus, number of result tuples smaller than input. (Difference depends on # of duplicates.) –Cost: In Pass 0, read original relation (size M), write out same number of smaller tuples. In merging passes, fewer tuples written out in each pass. v Hash-based scheme? SELECT DISTINCT R.sid, R.bid FROM Reserves R

38 38 Set Operations v Intersection and cross-product special cases of join. v Union (Distinct) and Difference similar. v Sorting based approach to union: –Sort both relations (on combination of all attributes). –Scan sorted relations and merge them. v Hash based approach to union?

39 39 Set Operations v Intersection and cross-product special cases of join. v Union (Distinct) and Difference similar. v Sorting based approach to union: –Sort both relations (on combination of all attributes). –Scan sorted relations and merge them. v Hash based approach to union: –Partition R and S using hash function h. –For each S-partition, build in-memory hash table (using h2), scan corr. R- partition and add tuples to table while discarding duplicates.

40 40 Aggregate Operations ( AVG, MIN, etc.) v Without grouping: –In general, requires scanning the relation. –Given index whose search key includes all attributes in the SELECT or WHERE clauses, can do index-only scan. v With grouping: –Sort on group-by attributes, then scan relation and compute aggregate for each group. –Similar approach based on hashing on group-by attributes. –Given tree index whose search key includes all attributes in SELECT, WHERE and GROUP BY clauses, can do index-only scan; if group-by attributes form prefix of search key, can retrieve data entries/tuples in group-by order.

41 41 Iterators for Implementation of Operators v Most operators can be implemented as an iterator v An iterator allows a consumer of the result of the operator to get the result one tuple at a time –Open – starts the process of getting tuples, but does not get a tuple. It initializes any data structures needed. –GetNext – returns the next tuple in the result and adjusts the data structures as necessary to allow subsequent tuples to be obtained. It may calls GetNext one or more times on its arguments. It also signals whether a tuple was produced or there were no more tuples to be produced. –Close – ends the iteration after all tuples have been obtained.

42 42 More on Iterators v Why iterators? –Do not need to materialize intermediate results –Many operators are active at once, and tuples flow from one operator to the next, thus reducing the need to store intermediate results v In some cases, almost all the work would need to be done by the Open function, which is tantamount to materialization v We shall regard Open, GetNext, Close as overloaded names of methods. –Assume that for each physical operator, there is a class whose objects are the relations that can be produced by this operator. If R is a member of such a class, then we use R.Open(), R.GetNext, and R.Close() to apply the functions of the iterator for R.

43 43 An iterator for table-scan operator Open(R) { b := first block of R; t := first tuple of block b; Found := TRUE; } Close(R) { } GetNext(R) { If (t is past the last tuple on b) b := next block If (there is no next block) Found := FALSE; RETURN; Else t := first tuple in b; oldt := t; t := next tuple of b RETURN oldt; }

44 44 An iterator for tuple-based nested-loops join operator Open(R,S) { R.Open(); S.Open(); s := S.GetNext(); } Close(R,S) { R.Close(); S.Close(); } GetNext(R,S) { REPEAT r := R.GetNext(); If (NOT Found) { R.Close(); s := S.GetNext(); IF (NOT Found) Return; R.Open(); r := R.GetNext(); } UNTIL (r and s join); Return the join of r and s; }

45 45 Impact of Buffering v If several operations are executing concurrently, estimating the number of available buffer pages is guesswork. v Repeated access patterns interact with buffer replacement policy. –e.g., Inner relation is scanned repeatedly in Simple Nested Loop Join. With enough buffer pages to hold inner, replacement policy does not matter. Otherwise, MRU is best, LRU is worst (sequential flooding). –Does replacement policy matter for Block Nested Loops? –What about Index Nested Loops? Sort-Merge Join?

46 46 Summary v A virtue of relational DBMSs: queries are composed of a few basic operators; the implementation of these operators can be carefully tuned (and it is important to do this!). v Many alternative implementation techniques for each operator; no universally superior technique for most operators. v Must consider available alternatives for each operation in a query and choose best one based on system statistics, etc. This is part of the broader task of optimizing a query composed of several ops.

47 Overview of Query Optimization v Plan : Tree of R.A. ops, with choice of alg for each op. –Each operator typically implemented using a `pull’ interface: when an operator is `pulled’ for the next output tuples, it `pulls’ on its inputs and computes them. v Two main issues: –For a given query, what plans are considered? u Algorithm to search plan space for cheapest (estimated) plan. –How is the cost of a plan estimated? v Ideally: Want to find best plan. Practically: Avoid worst plans! v We will study the System R approach.

48 Highlights of System R Optimizer v Impact: –Most widely used currently; works well for < 10 joins. v 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. v Plan Space: Too large, must be pruned. –Only the space of left-deep plans is considered. u 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.

49 Schema for Examples v Similar to old schema; rname added for variations. v Reserves: –Each tuple is 40 bytes long, 100 tuples per page, 1000 pages. v Sailors: –Each tuple is 50 bytes long, 80 tuples per page, 500 pages. Sailors ( sid : integer, sname : string, rating : integer, age : real) Reserves ( sid : integer, bid : integer, day : dates, rname : string)

50 Motivating Example v Cost: 500+500*1000 I/Os v By no means the worst plan! v Misses several opportunities: selections could have been `pushed’ earlier, no use is made of any available indexes, etc. v Goal of optimization: To find more efficient plans that compute the same answer. SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating>5 Reserves Sailors sid=sid bid=100 rating > 5 sname Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) (On-the-fly) RA Tree: Plan:

51 Alternative Plans 1 (No Indexes) v Main difference: push selects. v With 5 buffers, cost of plan: –Scan Reserves (1000) + write temp T1 (10 pages, if we have 100 boats, uniform distribution). –Scan Sailors (500) + write temp T2 (250 pages, if we have 10 ratings). –Sort T1 (2*2*10), sort T2 (2*3*250), merge (10+250), total=1830 –Total: 3560 page I/Os. v If we used BNL join, join cost = 10+4*250, total cost = 2770. v If we `push’ projections, T1 has only sid, T2 only sid and sname : –T1 fits in 3 pages, cost of BNL drops to under 250 pages, total < 2000. Reserves Sailors sid=sid bid=100 sname (On-the-fly) rating > 5 (Scan; write to temp T1) (Scan; write to temp T2) (Sort-Merge Join)

52 Alternative Plans 2 With Indexes v With clustered index on bid of Reserves, we get 100,000/100 = 1000 tuples on 1000/100 = 10 pages. v INL with pipelining (outer is not materialized). v Decision not to push rating>5 before the join is based on availability of sid index on Sailors. v Cost: Selection of Reserves tuples (10 I/Os); for each, must get matching Sailors tuple (1000*1.2); total 1210 I/Os. v Join column sid is a key for Sailors. –At most one matching tuple, unclustered index on sid OK. –Projecting out unnecessary fields from outer doesn’t help. Reserves Sailors sid=sid bid=100 sname (On-the-fly) rating > 5 (Use hash index; do not write result to temp) (Index Nested Loops, with pipelining ) (On-the-fly)

53 Query Blocks: Units of Optimization v An SQL query is parsed into a collection of query blocks, and these are optimized one block at a time. v Nested blocks are usually treated as calls to a subroutine, made once per outer tuple. (This is an over- simplification, but serves for now.) SELECT S.sname FROM Sailors S WHERE S.age IN ( SELECT MAX (S2.age) FROM Sailors S2 GROUP BY S2.rating ) Nested blockOuter block v For each block, the plans considered are: – All available access methods, for each relation in FROM clause. – All left-deep join trees (i.e., all ways to join the relations one- at-a-time, with the inner relation in the FROM clause, considering all relation permutations and join methods.)

54 Cost Estimation v For each plan considered, must estimate cost: –Must estimate cost of each operation in plan tree. u Depends on input cardinalities. u We’ve already discussed how to estimate the cost of operations (sequential scan, index scan, joins, etc.) –Must estimate size of result for each operation in tree! u Use information about the input relations. u For selections and joins, assume independence of predicates. v We’ll discuss the System R cost estimation approach. –Very inexact, but works ok in practice. –More sophisticated techniques known now.

55 Statistics and Catalogs v Need information about the relations and indexes involved. Catalogs typically contain at least: –# tuples (NTuples) and # pages (NPages) for each relation. –# distinct key values (NKeys) and NPages for each index. –Index height, low/high key values (Low/High) for each tree index. v Catalogs updated periodically. –Updating whenever data changes is too expensive; lots of approximation anyway, so slight inconsistency ok. v More detailed information (e.g., histograms of the values in some field) are sometimes stored.

56 Size Estimation and Reduction Factors v Consider a query block: v Maximum # tuples in result is the product of the cardinalities of relations in the FROM clause. v Reduction factor (RF) associated with each term reflects the impact of the term in reducing result size. Result cardinality = Max # tuples * product of all RF’s. –Implicit assumption that terms are independent! –Term col=value has RF 1/NKeys(I), given index I on col –Term col1=col2 has RF 1/ MAX (NKeys(I1), NKeys(I2)) –Term col>value has RF (High(I)-value)/(High(I)-Low(I)) SELECT attribute list FROM relation list WHERE term1 AND... AND termk

57 Relational Algebra Equivalences v Allow us to choose different join orders and to `push’ selections and projections ahead of joins. v Selections : ( Cascade ) ( Commute ) v Projections: (Cascade) v Joins:R (S T) (R S) T (Associative) (R S) (S R) (Commute) R (S T) (T R) S + Show that:

58 More Equivalences v A projection commutes with a selection that only uses attributes retained by the projection. v Selection between attributes of the two arguments of a cross-product converts cross-product to a join. v A selection on just attributes of R commutes with join R S. (i.e., (R S) (R) S ) v Similarly, if a projection follows a join R S, we can `push’ it by retaining only attributes of R (and S) that are needed for the join or are kept by the projection.

59 Enumeration of Alternative Plans v There are two main cases: –Single-relation plans –Multiple-relation plans v For queries over a single relation, queries consist of a combination of selects, projects, and aggregate ops: –Each available access path (file scan / index) is considered, and the one with the least estimated cost is chosen. –The different operations are essentially carried out together (e.g., if an index is used for a selection, projection is done for each retrieved tuple, and the resulting tuples are pipelined into the aggregate computation).

60 Cost Estimates for Single-Relation Plans v Index I on primary key matches selection: – Cost is Height(I)+1 for a B+ tree, about 1.2 for hash index. v Clustered index I matching one or more selects: – (NPages(I)+NPages(R)) * product of RF’s of matching selects. v Non-clustered index I matching one or more selects: – (NPages(I)+NTuples(R)) * product of RF’s of matching selects. v Sequential scan of file: – NPages(R). + Note: Typically, no duplicate elimination on projections! (Exception: Done on answers if user says DISTINCT.)

61 Example v If we have an index on rating : –(1/NKeys(I)) * NTuples(R) = (1/10) * 40000 tuples retrieved. –Clustered index: (1/NKeys(I)) * (NPages(I)+NPages(R)) = (1/10) * (50+500) pages are retrieved. (This is the cost.) –Unclustered index: (1/NKeys(I)) * (NPages(I)+NTuples(R)) = (1/10) * (50+40000) pages are retrieved. v If we have an index on sid : –Would have to retrieve all tuples/pages. With a clustered index, the cost is 50+500, with unclustered index, 50+40000. v Doing a file scan: –We retrieve all file pages (500). SELECT S.sid FROM Sailors S WHERE S.rating=8

62 Queries Over Multiple Relations v Fundamental decision in System R: only left-deep join trees are considered. –As the number of joins increases, the number of alternative plans grows rapidly; we need to restrict the search space. –Left-deep trees allow us to generate all fully pipelined plans. u Intermediate results not written to temporary files. u Not all left-deep trees are fully pipelined (e.g., SM join). B A C D B A C D C D B A

63 Enumeration of Left-Deep Plans v Left-deep plans differ only in the order of relations, the access method for each relation, and the join method for each join. v Enumerated using N passes (if N relations joined): –Pass 1: Find best 1-relation plan for each relation. –Pass 2: Find best way to join result of each 1-relation plan (as outer) to another relation. (All 2-relation plans.) –Pass N: Find best way to join result of a (N-1)-relation plan (as outer) to the N’th relation. (All N-relation plans.) v For each subset of relations, retain only: –Cheapest plan overall, plus –Cheapest plan for each interesting order of the tuples.

64 Enumeration of Plans (Contd.) v ORDER BY, GROUP BY, aggregates etc. handled as a final step, using either an `interestingly ordered’ plan or an addional sorting operator. v An N-1 way plan is not combined with an additional relation unless there is a join condition between them, unless all predicates in WHERE have been used up. –i.e., avoid Cartesian products if possible. v In spite of pruning plan space, this approach is still exponential in the # of tables.

65 Example v Pass1: – Sailors : B+ tree matches rating>5, and is probably cheapest. However, if this selection is expected to retrieve a lot of tuples, and index is unclustered, file scan may be cheaper. u Still, B+ tree plan kept (because tuples are in rating order). – Reserves : B+ tree on bid matches bid=500 ; cheapest. Sailors: B+ tree on rating Hash on sid Reserves: B+ tree on bid v Pass 2: – We consider each plan retained from Pass 1 as the outer, and consider how to join it with the (only) other relation. u e.g., Reserves as outer : Hash index can be used to get Sailors tuples that satisfy sid = outer tuple’s sid value. Reserves Sailors sid=sid bid=100 rating > 5 sname

66 Nested Queries v Nested block is optimized independently, with the outer tuple considered as providing a selection condition. v Outer block is optimized with the cost of `calling’ nested block computation taken into account. v Implicit ordering of these blocks means that some good strategies are not considered. The non- nested version of the query is typically optimized better. SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) Nested block to optimize: SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid= outer value Equivalent non-nested query: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103

67 Summary v Query optimization is an important task in a relational DBMS. v Must understand optimization in order to understand the performance impact of a given database design (relations, indexes) on a workload (set of queries). v Two parts to optimizing a query: –Consider a set of alternative plans. u Must prune search space; typically, left-deep plans only. –Must estimate cost of each plan that is considered. u Must estimate size of result and cost for each plan node. u Key issues : Statistics, indexes, operator implementations.

68 Summary (Contd.) v Single-relation queries: –All access paths considered, cheapest is chosen. – Issues : Selections that match index, whether index key has all needed fields and/or provides tuples in a desired order. v Multiple-relation queries: –All single-relation plans are first enumerated. u Selections/projections considered as early as possible. –Next, for each 1-relation plan, all ways of joining another relation (as inner) are considered. –Next, for each 2-relation plan that is `retained’, all ways of joining another relation (as inner) are considered, etc. –At each level, for each subset of relations, only best plan for each interesting order of tuples is `retained’.

69 69 Query Tuning v Two issues: –issue too many disk accesses (eg. Scan for a point query)? –Relevant indexes are not used?

70 70 Queries Settings: employee(ssnum, name, dept, salary, numfriends); student(ssnum, name, course, grade); techdept(dept, manager, location); clustered index i1 on employee (ssnum); nonclustered index i2 on employee (name); nonclustered index i3 on employee (dept); clustered index i4 on student (ssnum); nonclustered index i5 on student (name); clustered index i6 on techdept (dept); –100000 rows in employee, 100000 students, 10 departments; Cold buffer –Dual Pentium II (450MHz, 512Kb), 512 Mb RAM, 3x18Gb drives (10000RPM), Windows 2000.

71 71 Redundant DISTINT SELECT DISTINCT ssum FROM Employee WHERE dept = ‘computer sc’.

72 72 Subqueries SELECT ssum FROM Employee WHERE dept IN (SELECT dept FROM Techdept) SELECT ssnum FROM Employee, Techdept WHERE Employee.dept = Techdept.dept

73 73 Temporary tables SELECT * INTO Temp FROM Employee WHERE Salary > 40000 SELECT ssnum FROM Temp WHERE Temp.dept = ‘information’ -- query optimization problem, updates of catalog etc

74 74 Correlated Query Rewriting SELECT ssum FROM Employee e1 WHERE salary = (SELECT MAX(salary) FROM Employee e2 WHERE e2.dept = e1.dept) Problems?

75 75 Query Rewriting … cont SELECT MAX(salary) as maxsalary, dept INTO Temp From Employee GROUP BY dept SELECT ssnum FROM Employee, Temp WHERE salary = maxsalary AND Employee.dept = Temp.dept

76 76 4 - Relational Systems76 Query Rewriting – Correlated Subqueries v SQL Server 2000 does a good job at handling the correlated subqueries (a hash join is used as opposed to a nested loop between query blocks) –The techniques implemented in SQL Server 2000 are described in “Orthogonal Optimization of Subqueries and Aggregates” by C.Galindo- Legaria and M.Joshi, SIGMOD 2001. > 10000> 1000

77 77 Join on clustering index, and integer SELECT Employee.ssnum FROM Employee, Student WHERE Employee.name = Student.name --> SELECT Employee.ssnum FROM Employee, Student WHERE Employee.ssnum = Student.ssnum

78 78 Having SELECT AVG(salary) as avgsalary, dept FROM Employee GROUP BY dept HAVING dept = ‘information’; SELECT AVG(salary) as avgsalary FROM Employee WHERE dept = ‘information’ GROUP BY dept;

79 79 Idiosyncrasies v OR may stop the index being used –break the query and use UNION v Order of Tables may affect join implementation v View can cause query to be executed inefficiently

80 80 Queries – View on Join View Techlocation: create view techlocation as select ssnum, techdept.dept, location from employee, techdept where employee.dept = techdept.dept; Queries: –Original: select dept from techlocation where ssnum = ?; –Rewritten: select dept from employee where ssnum = ?;

81 81 4 - Relational Systems81 Query Rewriting - Views v All systems expand the selection on a view into a join v The difference between a plain selection and a join (on a primary key-foreign key) followed by a projection is greater on SQL Server than on Oracle and DB2 v7.1.

82 82 Aggregate Maintenance -- data Settings: orders( ordernum, itemnum, quantity, purchaser, vendor ); create clustered index i_order on orders(itemnum); store( vendor, name ); item(itemnum, price); create clustered index i_item on item(itemnum); vendorOutstanding( vendor, amount); storeOutstanding( store, amount); –1000000 orders, 10000 stores, 400000 items; Cold buffer –Dual Pentium II (450MHz, 512Kb), 512 Mb RAM, 3x18Gb drives (10000RPM), Windows 2000.

83 83 Aggregate Maintenance -- triggers Triggers for Aggregate Maintenance create trigger updateVendorOutstanding on orders for insert as update vendorOutstanding set amount = (select vendorOutstanding.amount+sum(inserted.quantity*item.price) from inserted,item where inserted.itemnum = item.itemnum ) where vendor = (select vendor from inserted) ; create trigger updateStoreOutstanding on orders for insert as update storeOutstanding set amount = (select storeOutstanding.amount+sum(inserted.quantity*item.price) from inserted,item where inserted.itemnum = item.itemnum ) where store = (select store.name from inserted, store where inserted.vendor = store.vendor) ;

84 84 Aggregate Maintenance -- transactions Concurrent Transactions: –Insertions insert into orders values (1000350,7825,562,'xxxxxx6944','vendor4'); –Queries (first without, then with redundant tables) select orders.vendor, sum(orders.quantity*item.price) from orders,item where orders.itemnum = item.itemnum group by orders.vendor; vs. select * from vendorOutstanding; select store.name, sum(orders.quantity*item.price) from orders,item, store where orders.itemnum = item.itemnum and orders.vendor = store.vendor group by store.name; vs. select * from storeOutstanding;

85 85 4 - Relational Systems85 Aggregate Maintenance v SQLServer 2000 on Windows 2000 v Using triggers for view maintenance v If queries frequent or important, then aggregate maintenance is good.

86 86 Superlinearity -- data Settings: sales( id, itemid, customerid, storeid, amount, quantity); item (itemid); customer (customerid); store (storeid); A sale is successful if all foreign keys are present. successfulsales(id, itemid, customerid, storeid, amount, quantity); unsuccessfulsales(id, itemid, customerid, storeid, amount, quantity); tempsales( id, itemid, customerid, storeid, amount,quantity);

87 87 Superlinearity -- indexes Settings (non-clustering, dense indexes): index s1 on item(itemid); index s2 on customer(customerid); index s3 on store(storeid); index succ on successfulsales(id); –1000000 sales, 400000 customers, 40000 items, 1000 stores –Cold buffer –Dual Pentium II (450MHz, 512Kb), 512 Mb RAM, 3x18Gb drives (10000RPM), Windows 2000.

88 88 Superlinearity -- queries Queries: –Insert/create indexdelete insert into successfulsales select sales.id, sales.itemid, sales.customerid, sales.storeid, sales.amount, sales.quantity from sales, item, customer, store where sales.itemid = item.itemid and sales.customerid = customer.customerid and sales.storeid = store.storeid; insert into unsuccessfulsales select * from sales; go delete from unsuccessfulsales where id in (select id from successfulsales)

89 89 Superlinearity -- batch queries Queries: –Small batches DECLARE @Nlow INT; DECLARE @Nhigh INT; DECLARE @INCR INT; set @INCR = 100000 set @NLow = 0 set @Nhigh = @INCR WHILE (@NLow <= 500000) BEGIN insert into tempsales select * from sales where id between @NLow and @Nhigh set @Nlow = @Nlow + @INCR set @Nhigh = @Nhigh + @INCR delete from tempsales where id in (select id from successfulsales); insert into unsuccessfulsales select * from tempsales; delete from tempsales; END

90 90 Superlinearity -- outer join Queries: –outerjoin insert into successfulsales select sales.id, item.itemid, customer.customerid, store.storeid, sales.amount, sales.quantity from ((sales left outer join item on sales.itemid = item.itemid) left outer join customer on sales.customerid = customer.customerid) left outer join store on sales.storeid = store.storeid; insert into unsuccessfulsales select * from successfulsales where itemid is null or customerid is null or storeid is null; go delete from successfulsales where itemid is null or customerid is null or storeid is null

91 91 4 - Relational Systems91 Circumventing Superlinearity v SQL Server 2000 v Outer join achieves the best response time. v Small batches do not help because overhead of crossing the application interface is higher than the benefit of joining with smaller tables.

92 92 5 - Tuning the API92 Tuning the Application Interface v 4GL –Power++, Visual basic v Programming language + Call Level Interface –ODBC: Open DataBase Connectivity –JDBC: Java based API –OCI (C++/Oracle), CLI (C++/ DB2), Perl/DBI v In the following experiments, the client program is located on the database server site. Overhead is due to crossing the application interface.

93 93 Looping can hurt -- data Settings: lineitem ( L_ORDERKEY, L_PARTKEY, L_SUPPKEY, L_LINENUMBER, L_QUANTITY, L_EXTENDEDPRICE, L_DISCOUNT, L_TAX, L_RETURNFLAG, L_LINESTATUS, L_SHIPDATE, L_COMMITDATE, L_RECEIPTDATE, L_SHIPINSTRUCT, L_SHIPMODE, L_COMMENT ); –600 000 rows; warm buffer. –Dual Pentium II (450MHz, 512Kb), 512 Mb RAM, 3x18Gb drives (10000RPM), Windows 2000.

94 94 Looping can hurt -- queries v Queries: –No loop: sqlStmt = “select * from lineitem where l_partkey <= 200;” odbc->prepareStmt(sqlStmt); odbc->execPrepared(sqlStmt); –Loop: sqlStmt = “select * from lineitem where l_partkey = ?;” odbc->prepareStmt(sqlStmt); for (int i=1; i<100; i++) { odbc->bindParameter(1, SQL_INTEGER, i); odbc->execPrepared(sqlStmt); }

95 95 5 - Tuning the API95 Looping can Hurt v SQL Server 2000 on Windows 2000 v Crossing the application interface has a significant impact on performance. v Why would a programmer use a loop instead of relying on set-oriented operations: object-orientation?

96 96 Cursors are Death -- data Settings: employees(ssnum, name, lat, long, hundreds1, hundreds2); –100000 rows ; Cold buffer –Dual Pentium II (450MHz, 512Kb), 512 Mb RAM, 3x18Gb drives (10000RPM), Windows 2000.

97 97 Cursors are Death -- queries Queries: –No cursor select * from employees; –Cursor DECLARE d_cursor CURSOR FOR select * from employees; OPEN d_cursor while (@@FETCH_STATUS = 0) BEGIN FETCH NEXT from d_cursor END CLOSE d_cursor go

98 98 5 - Tuning the API98 Cursors are Death v SQL Server 2000 on Windows 2000 v Response time is a few seconds with a SQL query and more than an hour iterating over a cursor.

99 99 Retrieve Needed Columns Only - data Settings: lineitem ( L_ORDERKEY, L_PARTKEY, L_SUPPKEY, L_LINENUMBER, L_QUANTITY, L_EXTENDEDPRICE, L_DISCOUNT, L_TAX, L_RETURNFLAG, L_LINESTATUS, L_SHIPDATE, L_COMMITDATE, L_RECEIPTDATE, L_SHIPINSTRUCT, L_SHIPMODE, L_COMMENT ); create index i_nc_lineitem on lineitem (l_orderkey, l_partkey, l_suppkey, l_shipdate, l_commitdate); –600 000 rows; warm buffer. –Lineitem records are ~ 10 bytes long –Dual Pentium II (450MHz, 512Kb), 512 Mb RAM, 3x18Gb drives (10000RPM), Windows 2000.

100 100 Retrieve Needed Columns Only - queries Queries: –All Select * from lineitem; –Covered subset Select l_orderkey, l_partkey, l_suppkey, l_shipdate, l_commitdate from lineitem;

101 101 5 - Tuning the API101 Retrieve Needed Columns Only v Avoid transferring unnecessary data v May enable use of a covering index. v In the experiment the subset contains ¼ of the attributes. –Reducing the amount of data that crosses the application interface yields significant performance improvement. Experiment performed on Oracle8iEE on Windows 2000.

102 102 Bulk Loading Data Settings: lineitem ( L_ORDERKEY, L_PARTKEY, L_SUPPKEY, L_LINENUMBER, L_QUANTITY, L_EXTENDEDPRICE, L_DISCOUNT, L_TAX, L_RETURNFLAG, L_LINESTATUS, L_SHIPDATE, L_COMMITDATE, L_RECEIPTDATE, L_SHIPINSTRUCT, L_SHIPMODE, L_COMMENT ); –Initially the table is empty; 600 000 rows to be inserted (138Mb) –Table sits one disk. No constraint, index is defined. –Dual Pentium II (450MHz, 512Kb), 512 Mb RAM, 3x18Gb drives (10000RPM), Windows 2000.

103 103 Bulk Loading Queries Oracle 8i sqlldr directpath=true control=load_lineitem.ctl data=E:\Data\lineitem.tbl load data infile "lineitem.tbl" into table LINEITEM append fields terminated by '|' ( L_ORDERKEY, L_PARTKEY, L_SUPPKEY, L_LINENUMBER, L_QUANTITY, L_EXTENDEDPRICE, L_DISCOUNT, L_TAX, L_RETURNFLAG, L_LINESTATUS, L_SHIPDATE DATE "YYYY-MM-DD", L_COMMITDATE DATE "YYYY-MM-DD", L_RECEIPTDATE DATE "YYYY-MM-DD", L_SHIPINSTRUCT, L_SHIPMODE, L_COMMENT )

104 104 5 - Tuning the API104 Direct Path v Direct path loading bypasses the query engine and the storage manager. It is orders of magnitude faster than for conventional bulk load (commit every 100 records) and inserts (commit for each record). Experiment performed on Oracle8iEE on Windows 2000.

105 105 5 - Tuning the API105 Batch Size v Throughput increases steadily when the batch size increases to 100000 records.Throughput remains constant afterwards. v Trade-off between performance and amount of data that has to be reloaded in case of problem. Experiment performed on SQL Server 2000 on Windows 2000.


Download ppt "1 Implementation of Relational Operators. 2 Steps of processing a high-level query Parser Query Optimizer StatisticsCost Model QEPParsed Query Database."

Similar presentations


Ads by Google