Understanding Database Access Paths and Methods

Slide Note
Embed
Share

Explore the fundamentals of database systems, including access paths, external memory models, warm-up exercises with queues and stacks, heap file operations, alternative file organizations, and a cost model for analysis. Learn about different methods and models for efficient data retrieval and storage.


Uploaded on Sep 14, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Database Systems Access Paths and Access Methods Based on slides by Feifei Li, University of Utah

  2. External Memory Model (vs. RAM Model) N = # of items in the problem instance B = # of items per disk block (page) M = # of items that fit in main memory D Block I/O T = # of items in output I/O: Move block between memory and disk M We assume (for convenience) that M >B2 P 2

  3. Fundamental Bounds Internal log External N Scanning: N N log N N 2 B N N log Sorting: M B B B Searching: log N B Note: Linear I/O: O(N/B) N N N log N B factor VERY important: M B B B B Cannot sort optimally with search tree 3

  4. Warm-Up: Queues and Stacks Queue: Maintain push and pop blocks in main memory Push Pop O(1/B) Push/Pop operations (amortized cost analysis) Stack: Maintain push/pop block in main memory O(1/B) Push/Pop operations (amortized cost analysis) 4

  5. Heap File Heapfile: keep blocks of records on disk but must support operations: scan all records search for a record id RID insert new records delete old records 5

  6. Alternative File Organizations Many alternatives exist, each good for some situations, and not so good in others: Heap files: Suitable when typical access is a file scan retrieving all records. Sorted Files: Best for retrieval in search keyorder, or only a `range of records is needed. A search key is a set of attributes (can be a single attribute) that the underlying file is sorted with respect to; it has NOTHING to do with the concept of key . Clustered Files (with Indexes): Coming soon Unclustered Files (with Indexes): Coming soon 6

  7. Cost Model for Analysis We ignore CPU costs, for simplicity: N/B: The number of data blocks (pages) B: Number of records per block D: (Average) time to read or write disk block Measuring number of block I/O s ignores gains of pre-fetching and sequential access; thus, even I/O cost is only loosely approximated. Average-case analysis (unless o/w specified); based on several simplistic assumptions. Good enough to show the overall trends! 7

  8. Some Assumptions in the Analysis Single record insert and delete; unless o/w specified Equality selection - exactly one match; unless o/w specified Heap Files: Insert always appends to end of file. Sorted Files: Two alternatives: No need to compact the file after deletions. Files compacted after deletions. Selections on search key (the attribute(s) used for sorting). 8

  9. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records Equality Search Range Search Insert Delete 9

  10. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search Range Search Insert Delete 10

  11. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search: if we know there s only 1 matching record 0.5N/B IOs, Best Case: 1 IO, Worst Case: N/B IO. log2 (N/B) in the worst case. Best case: 1 IO. Range Search Insert Delete 11

  12. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search: if we don not know how many matching records there are. N/B IOs. log2 (N/B)+ T/B in the worst case. Best case: 1+T/B IO. Range Search Insert Delete 12

  13. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search: if we know there s only 1 matching record 0.5N/B IOs, Best Case: 1 IO, Worst Case: N/B IO. log2 (N/B) in the worst case. Best case: 1 IO. Range Search N/B log2 (N/B) + #match pages = log2 (N/B) + T/B Insert Delete 13

  14. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search: if we know there s only 1 matching record 0.5N/B IOs, Best Case: 1 IO, Worst Case: N/B IO. log2 (N/B) in the worst case. Best case: 1 IO. Range Search N/B log2 (N/B) + #match pages = log2 (N/B) + T/B Insert: no compact for sorted file 2 log2 (N/B) + 1 Delete 14

  15. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search: if we know there s only 1 matching record 0.5N/B IOs, Best Case: 1 IO, Worst Case: N/B IO. log2 (N/B) in the worst case. Best case: 1 IO. Range Search N/B log2 (N/B) + #match pages = log2 (N/B) + T/B Insert: compact for sorted file 2 log2 (N/B) + N/B (read + write for 0.5N/B pages on average) Delete 15

  16. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search: if we know there s only 1 matching record 0.5N/B IOs, Best Case: 1 IO, Worst Case: N/B IO. log2 (N/B) in the worst case. Best case: 1 IO. Range Search N/B log2 (N/B) + #match pages = log2 (N/B) + T/B Insert: compact for sorted file 2 log2 (N/B) + N/B (read + write for 0.5N/B pages on average) Delete: no compact for sorted file 0.5N/B + 1 log2 (N/B) + 1 16

  17. N/B: The number of data pages B: Number of records per page D: (Average) time to read or write disk page Cost of Operations Heap File Sorted File Scan all records N/B IOs, and (N/B)D time. N/B IOs, and (N/B)D time. Equality Search: if we know there s only 1 matching record 0.5N/B IOs, Best Case: 1 IO, Worst Case: N/B IO. log2 (N/B) in the worst case. Best case: 1 IO. Range Search N/B log2 (N/B) + #match pages = log2 (N/B) + T/B Insert: compact for sorted file 2 log2 (N/B) + N/B (read + write for 0.5N/B pages on average) Delete: compact for sorted file 0.5N/B + 1 log2 (N/B) + N/B 17

  18. Context We looked at SQL SQL Query A quick overview of query processing Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB 18

  19. Query Processing Overview The query optimizertranslates SQL to a special internal language Query Plans The query executor is an interpreter for query plans Think of query plans as box-and-arrow dataflow diagrams Each box implements a relational operator Edges represent a flow of tuples (columns as specified) For single-table queries, these diagrams are straight-line graphs SELECT DISTINCT name, gpa FROM Students name, gpa Distinct name, gpa Optimizer Sort name, gpa HeapScan 19

  20. Iterators (the volcano model) iterator The relational operators are all subclasses of the class iterator: class iterator { void init(); tuple next(); void close(); iterator &inputs[]; // additional state goes here } Note: Edges in the graph are specified by inputs (max 2, usually) Encapsulation: any iterator can be input to any other! When subclassing, different iterators will keep different kinds of state information 20

  21. Example of Volcano-style Iterator Interface and Implementation In Volcano-style engine model, an execution engine takes an execution plan (aka evaluation plan), generates a tree of physical operators, evaluates one or more tables through physical operators, and finally results in a query answer. A physical operator is an evaluation algorithm that implements an operator of relational algebra (or logical plan). Physical operators are implemented though Volcano-style iterator interface, Each operator implements an API consisting of open(),next(), and close() methods. open() initializes external resources such as memory and file open. close() releases them. next() call of each physical operator in an operator tree produces one new tuple recursively obtained from next() calls of child physical operators in the tree. 21

  22. Example: Scan class Scan extends iterator { void init(); tuple next(); void close(); iterator inputs[1]; bool_expr filter_expr; proj_attr_list proj_list; } init(): Set up internal state call init() on child often a file open next(): call next() on child until qualifying tuple found or EOF keep only those fields in proj_list return tuple (or EOF -- End of File -- if no tuples remain) close(): call close() on child clean up internal state Note: Scan also applies selection filters and projections (without duplicate elimination) 22

  23. Indexes Sometimes, we want to retrieve records by specifying the values in one or more fields, e.g., Find all students in the CS department Find all students with a gpa > 3 An index on a file is a data structure that speeds up selections on the search key fields for the index. Any subset of the fields of a relation can be the search key for an index on the relation. Search key is not the same as key(e.g. doesn t have to be unique). 23

  24. Indexes An index contains a collection of data entries, and supports efficient retrieval of all data entries k* with a given search key value k. Given a data entry k*, we can find record with key k in possibly one disk I/O. 24

  25. Alternatives for Data Entry k* in Index Three alternatives: Actual data record (with key value k) <k, rid of matching data record> <k, list of rids of matching data records> Choice is orthogonal to the indexing technique. Examples of indexing techniques: B+ trees, hash-based structures, R trees, Typically, index contains auxiliary information that directs searches to the desired data entries Can have multiple (different) indexes per file. E.g. file sorted by age, with a hash index on salary and a B+tree index on name. 25

  26. Alternatives for Data Entries (Contd.) Alternative 1: Actual data record (with key value k) If this is used, index structure is a file organization for data records (like Heap files or sorted files). At most one index on a given collection of data records can use Alternative 1. This alternative saves pointer lookups but can be expensive to maintain with insertions and deletions. 26

  27. Alternatives for Data Entries (Contd.) Alternative 2 <k, rid of matching data record> and Alternative 3 <k, list of rids of matching data records> Easier to maintain than Alt 1. If more than one index is required on a given file, at most one index can use Alternative 1; rest must use Alternatives 2 or 3. Alternative 3 more compact than Alternative 2, but leads to variable sized dataentries even if search keys are of fixed length. Even worse, for large rid lists the data entry would have to span multiple blocks! 27

  28. First Question to Ask About Indexes What kinds of selections do they support? Selections of form field <op> constant Equality selections (op is =) Range selections (op is one of <, >, <=, >=, BETWEEN) More exotic selections: 2-dimensional ranges ( west of Boston and east of Brighton and North of South Boston and South of Summerville ) Or n-dimensional 2-dimensional distances ( within 2 miles of Kenmore Sq ) Or n-dimensional Ranking queries ( 10 restaurants closest to MEB ) Regular expression matches, genome string matches, etc. One common n-dimensional index: R-tree Supported in Oracle and Informix 28

  29. Index Classification What selections does it support Representation of data entries in index i.e., what kind of info is the index actually storing? 3 alternatives here Primary vs. Secondary Indexes Clustered vs. Unclustered Indexes Single Key vs. Composite Indexes Tree-based, hash-based, other 29

  30. Index Examples: B+ Tree Indexes Non-leaf Pages Leaf Pages (Sorted by search key) Leaf pages contain data entries, and are chained (prev & next page ids) Non-leaf pages have index entries; only used to direct searches: one index entry P0 K1 P1 K2 Pm P2 Km 30

  31. Example B+ Tree Note how data entries in leaf level are sorted Root 17 17 Dat Entries <= 17 17 Data Entries > 17 27 5 13 30 Pointers to Actual Data Pages (rid) 33* 34* 38* 39* 2* 3* 5* 7* 8* 22* 24* 27* 29* 14* 16* Heap File for the Data Records Find 28*? 29*? All > 15* and < 30* Insert/delete: Find data entry in leaf, then change it. Need to adjust parent sometimes. And change sometimes bubbles up the tree 31

  32. Hash-Based Indexes Good for equality selections. Index is a collection of buckets. Bucket = primary page plus zero or more overflow pages. Buckets contain data entries. Hashing function h: h(r) = bucket in which (data entry for) record r belongs. h looks at the search key fields of r. No need for index entries in this scheme. 32

  33. Index Classification Primary vs. secondary: If search key contains primary key, then called primary index. Unique index: Search key contains a candidate key. Some also use primary and secondary to represent clustered and unclustered index respectively. 33

  34. Index Classification Clustered vs. unclustered: If order of data records is the same as, or `close to , order of index data entries, then called clustered index. A file can be clustered on at most one search key. Cost of retrieving data records through index varies greatly based on whether index is clustered or not! Using Alternative 1 in a B+-tree implies clustered, but not vice-versa. 34

  35. Clustered vs. Unclustered Index Suppose that Alternative (2) is used for data entries, and that the data records are stored in a Heap file. To build clustered index, first sort the Heap file (with some free space on each block for future inserts). Overflow blocks may be needed for inserts. (Thus, order of data recs is `close to , but not identical to, the sort order.) Index entries direct search for data entries UNCLUSTERED CLUSTERED Data entries Data entries (Index File) (Data file) Data Records 35 Data Records

  36. Unclustered vs. Clustered Indexes What are the tradeoffs? Clustered Pros Efficient for range searches for records: sequential access in a sorted file May be able to do some types of compression Locality benefits Clustered Cons Expensive to maintain (on the fly or sloppy with reorganization) Unclustered Pros: easy and efficient to maintain, allow multiple indecies Cons: expensive for range searches for records: 1 random IO for each matching record. 36

  37. Clustered Files We usually refer a clustered Index using Alternative 1 as clustered files Pages are usually about 67 percent occupancy No. of physical data pages is about 1.5N/B (if N/B pages are required for storing all the records) 37

  38. Static Hashing Index # primary pages fixed, allocated sequentially, never de-allocated; overflow pages if needed. h(k) MOD N= the bucket to which data entry with search key value k belongs. (N = # of buckets) h(key) mod N 1 0 key h N-1 Primary bucket pages Overflow pages 38

  39. Composite Search Keys Search on a combination of fields. Examples of composite key indexes using lexicographic order. Equality query: Every field value is equal to a constant value. E.g. wrt <age, fans> index: age=20 and fans =75 11,80 11 12 Range query: Some field value is not a constant. E.g.: 12,10 age > 20; or age=20 and fans > 10 Data entries in index sorted by search key to support range queries. nameage fans 12,20 12 13,75 bob cal 12 10 80 13 <age, fans> 11 <age> Lexicographic order joe 12 20 Like the dictionary, but on fields, not letters! 10,12 sue 13 75 10 20 75 20,12 75,13 Data records sorted by name 80,11 80 <fans, age> Data entries in index sorted by <fans,age> <fans> Data entries sorted by <fans> 39

  40. Assumptions in Our Analysis Heap Files: Equality selection on key; exactly one match. Sorted Files: Files compacted after deletions. Indexes: Alt (2), (3): data entry size = 10% size of record Hash: No overflow buckets. 80% page occupancy => File size = 1.25 data size (1.25 N/B) 0.125N/B no. of pages for data entries Tree: 67% occupancy (this is typical). Implies file size = 1.5 data size (1.5 N/B) 0.15N/B no. of pages for data entries. 40

  41. Assumptions (contd.) Scans: Leaf levels of a tree-index are chained. Index data-entries plus actual file scanned for unclustered indexes. Range searches: We use tree indexes to restrict the set of data records fetched, but ignore hash indexes. 41

  42. N/B: The number of data pages B: Number of records per page F: average fan-out in the tree-index Sorted File Cost of Operations Heap File Clustered File N/B N/B 1.5 N/B Scan all Scan all records records 0.5 N/B log2 N/B logF (1.5N/B) Equality Equality Search Search N/B log2 N/B + T/B logF (1.5N/B) + 1.5T/B Range Range Search Search 2 log2 N/B +1 logF (1.5 N/B) +1 Insert (no Insert (no compact) compact) 0.5N/B + 1 log2 N/B+1 logF (1.5N/B)+1 Delete (no Delete (no compact) compact) 42

  43. N/B: The number of data pages B: Number of records per page F: average fan-out in the tree-index Cost of Operations Unclustered Alt-2 Tree Idx (Index file: 67% occupancy) (Data file: 100% occupancy) Clustered Alt-2 Tree Index (Index and Data files: 67% occupancy) Scan all records Scan all records N/B 1.5N/B (ignore index) (ignore index) logF (1.5 N/B) + 1 (or logF (0.5 N/B) + 1) If we take into account that the data entry is much smaller than a data record in general, the number of data leaf nodes can be much smaller than 1.5 N/B. For example, if a data entry is 1/3 the size of a record so index leaf level = .33 * 1.5B = 0.5B (logF 1.5 N/B) + #matching_leaf_pages + #match records Equality Search Equality Search unique key unique key 1+ logF(1.5 N/B) (logF 1.5B) + #match_leaf_pgs + #match pages Range Search Range Search Insert Insert (logF 1.5 N/B)+3 (logF 1.5B)+3 Delete Delete same as insert same as insert

  44. Summary Many alternative file organizations exist, each appropriate in some situation. If selection queries are frequent, sorting the file or building an index is important. Hash-based indexes only good for equality search. Sorted files and tree-based indexes best for range search; also good for equality search. (Files rarely kept sorted in practice; B+ tree index is better.) Index is a collection of data entries plus a way to quickly find entries with given key values. 44

  45. Summary (Contd.) Data entries in index can be actual data records, <key, rid> pairs, or <key, rid-list> pairs. Choice orthogonal to indexing structure (i.e. tree, hash, etc.). Usually have several indexes on a given file of data records, each with a different search key. Indexes can be classified as clustered vs. unclustered Differences have important consequences for utility/performance. 45

Related