DOC PREVIEW
Berkeley COMPSCI 186 - Implementation of Relational Operations

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Implementation of Relational OperationsIntroductionRelational OperationsSchema for ExamplesSimple SelectionsSimple Selections (cont)Using an Index for SelectionsSelections using Index (cont)General Selection ConditionsTwo Approaches to General SelectionsMost Selective Index - ExampleIntersection of RidsProjection (DupElim)Projection Based on HashingDupElim & IndexesJoinsEquality Joins With One Join ColumnSimple Nested Loops JoinPage-Oriented Nested Loops JoinIndex Nested Loops JoinExamples of Index Nested Loops“Block” Nested Loops JoinExamples of Block Nested LoopsSort-Merge Join (R S)Example of Sort-Merge JoinRefinement of Sort-Merge JoinHash-JoinObservations on Hash-JoinCost of Hash-JoinGeneral Join ConditionsSet OperationsImpact of BufferingSummaryImplementation of Relational Operations CS186, Fall 2005 R&G - Chapter 14First comes thought; then organization of that thought, into ideas and plans; then transformation of those plans into reality. The beginning, as you will observe, is in your imagination. Napoleon HillIntroduction•We’ve covered the basic underlying storage, buffering, and indexing technology.–Now we can move on to query processing.•Some database operations are EXPENSIVE•Can greatly improve performance by being “smart”–e.g., can speed up 1,000,000x over naïve approach•Main weapons are:–clever implementation techniques for operators–exploiting “equivalencies” of relational operators–using statistics and cost models to choose among these.•First: basic operators•Then: join•After that: optimizing multiple operatorsRelational Operations•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 (  ) Tuples in reln. 1 and in reln. 2.–Aggregation (SUM, MIN, etc.) and GROUP BY•Since each op returns a relation, ops can be composed! After we cover the operations, we will discuss how to optimize queries formed by composing them.Schema for Examples•Similar to old schema; rname added for variations.•Reserves:–Each tuple is 40 bytes long, 100 tuples per page, 1000 pages.•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)Simple Selections•Of the form•Question: how best to perform? Depends on:–what indexes/access paths are available–what is the expected size of the result (in terms of number of tuples and/or number of pages)•Size of result (cardinality) approximated as size of R * reduction factor –“reduction factor” is usually called selectivity.–estimate of reduction factors is based on statistics – we will discuss later.SELECT *FROM Reserves RWHERE R.rname < ‘C%’R attr valueopR.( )Simple Selections (cont)•With no index, unsorted: –Must essentially scan the whole relation–cost is M (#pages in R). For “reserves” = 1000 I/Os.•With no index, sorted:–cost of binary search + number of pages containing results. –For reserves = 10 I/Os + selectivity*#pages•With an index on selection attribute: –Use index to find qualifying data entries, –then retrieve corresponding data records. –Cost?Using an Index for Selections•Cost depends on #qualifying tuples, and clustering.–Cost:•finding qualifying data entries (typically small) •plus cost of retrieving records (could be large w/o clustering).–In example “reserves” relation, if 10% of tuples qualify (100 pages, 10000 tuples). •With a clustered index, cost is little more than 100 I/Os;•If unclustered, could be up to 10000 I/Os! –Unless you get fancy…Selections using Index (cont)•Important refinement for unclustered indexes: 1. Find qualifying data entries.2. Sort the rid’s of the data records to be retrieved.3. Fetch rids in order. This ensures that each data page is looked at just once (though # of such pages likely to be higher than with clustering). Index entriesData entriesdirect search for (Index File)(Data file)Data Recordsdata entriesData entriesData RecordsCLUSTEREDGeneral Selection Conditions•Such 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) •We only discuss the case with no ORs (a conjunction of terms of the form attr op value).•A B-tree index matches (a conjunction of) terms that involve only attributes in a prefix of the search key.–Index on <a, b, c> matches a=5 AND b= 3, but not b=3.•(For Hash index, must have all attrs in search key) (day<8/9/94 AND rname=‘Paul’) OR bid=5 OR sid=3Two Approaches to General Selections•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.Most Selective Index - Example•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; –Then, day<8/9/94 must be checked. •How about a B+tree on <rname,day>?•How about a B+tree on <day, rname>?•How about a Hash index on <day, rname>?Intersection of Rids•Second approach: if we have 2 or more matching indexes (w/Alternatives (2) or (3) for data entries):–Get sets of rids of data records using each matching index.–Then intersect these sets of rids.–Retrieve the records and apply any remaining terms.–Consider day<8/9/94 AND bid=5 AND sid=3. With 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. –Note: commercial systems use various tricks to do this:•bit maps, bloom filters, index joinsProjection (DupElim)•Issue is removing duplicates.•Basic approach is to use sorting–1. Scan R, extract only the needed attrs


View Full Document

Berkeley COMPSCI 186 - Implementation of Relational Operations

Documents in this Course
Load more
Download Implementation of Relational Operations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Implementation of Relational Operations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Implementation of Relational Operations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?