File and Index Structure

Slides:



Advertisements
Similar presentations
CpSc 3220 File and Database Processing Lecture 17 Indexed Files.
Advertisements

Introduction to Database Systems1 Records and Files Storage Technology: Topic 3.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part C Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
Quick Review of Apr 10 material B+-Tree File Organization –similar to B+-tree index –leaf nodes store records, not pointers to records stored in an original.
Chapter 11 Indexing and Hashing (2) Yonsei University 2 nd Semester, 2013 Sanghyun Park.
CIS552Indexing and Hashing1 Cost estimation Basic Concepts Ordered Indices B + - Tree Index Files B - Tree Index Files Static Hashing Dynamic Hashing Comparison.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 12: Indexing and.
INDEXING AND HASHING.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 12: Indexing and.
1 Overview of Storage and Indexing Chapter 8 (part 1)
Chapter 8 File organization and Indices.
1 File Organizations and Indexing Module 4, Lecture 2 “How index-learning turns no student pale Yet holds the eel of science by the tail.” -- Alexander.
Data Indexing Herbert A. Evans. Purposes of Data Indexing What is Data Indexing? Why is it important?
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part A Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
1 Overview of Storage and Indexing Yanlei Diao UMass Amherst Feb 13, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Recap of Feb 27: Disk-Block Access and Buffer Management Major concepts in Disk-Block Access covered: –Disk-arm Scheduling –Non-volatile write buffers.
METU Department of Computer Eng Ceng 302 Introduction to DBMS Disk Storage, Basic File Structures, and Hashing by Pinar Senkul resources: mostly froom.
CM20145 File Structure and Indexing Dr Alwyn Barry Dr Joanna Bryson.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 13 Disk Storage, Basic File Structures, and Hashing.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 11: Storage and.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
File Organizations and Indexing Lecture 4 R&G Chapter 8 "If you don't find it in the index, look very carefully through the entire catalogue." -- Sears,
1.1 CAS CS 460/660 Introduction to Database Systems File Organization Slides from UC Berkeley.
1 Overview of Storage and Indexing Chapter 8 1. Basics about file management 2. Introduction to indexing 3. First glimpse at indices and workloads.
Ch12: Indexing and Hashing  Basic Concepts  Ordered Indices B+-Tree Index Files B+-Tree Index Files B-Tree Index Files B-Tree Index Files  Hashing Static.
Layers of a DBMS Query optimization Execution engine Files and access methods Buffer management Disk space management Query Processor Query execution plan.
1.A file is organized logically as a sequence of records. 2. These records are mapped onto disk blocks. 3. Files are provided as a basic construct in operating.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 5, 6 of Elmasri “ How index-learning turns no student.
Chapter 10 Storage and File Structure Yonsei University 2 nd Semester, 2013 Sanghyun Park.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 Disk Storage, Basic File Structures, and Hashing.
©Silberschatz, Korth and Sudarshan11.1Database System Concepts Magnetic Hard Disk Mechanism NOTE: Diagram is schematic, and simplifies the structure of.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
12.1 Chapter 12: Indexing and Hashing Spring 2009 Sections , , Problems , 12.7, 12.8, 12.13, 12.15,
©Silberschatz, Korth and Sudarshan11.1Database System Concepts Chapter 11: Storage and File Structure File Organization Organization of Records in Files.
1 Overview of Storage and Indexing Chapter 8 (part 1)
Basic Concepts Indexing mechanisms used to speed up access to desired data. E.g., author catalog in library Search Key - attribute to set of attributes.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Storage and Indexing Chapter 8.
1 Overview of Storage and Indexing Chapter 8. 2 Data on External Storage  Disks: Can retrieve random page at fixed cost  But reading several consecutive.
Indexing and hashing Azita Keshmiri CS 157B. Basic concept An index for a file in a database system works the same way as the index in text book. For.
Indexing and Hashing By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
File Structures. 2 Chapter - Objectives Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files Hashed Files Dynamic and.
1/14/2005Yan Huang - CSCI5330 Database Implementation – Storage and File Structure Storage and File Structure II Some of the slides are from slides of.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Indexing.
Marwan Al-Namari Hassan Al-Mathami. Indexing What is Indexing? Indexing is a mechanisms. Why we need to use Indexing? We used indexing to speed up access.
Indexing Database Management Systems. Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files File Organization 2.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module D: Hashing.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 12: Indexing and.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 11: Indexing.
Chapter 5 Record Storage and Primary File Organizations
PART 4 DATA STORAGE AND QUERY. Chapter 12 Indexing and Hashing.
CS4432: Database Systems II
Database Management Systems, R. Ramakrishnan and J. Gehrke1 File Organizations and Indexing Chapter 8 Jianping Fan Dept of Computer Science UNC-Charlotte.
Chapter 11 Indexing And Hashing (1) Yonsei University 1 st Semester, 2016 Sanghyun Park.
Storage and File Organization
Module 11: File Structure
Indexing and hashing.
Azita Keshmiri CS 157B Ch 12 indexing and hashing
CS522 Advanced database Systems
Chapter 11: Storage and File Structure
File Organizations Chapter 8 “How index-learning turns no student pale
File organization and Indexing
Chapter 11: Indexing and Hashing
Introduction to Database Systems
Indexing and Hashing Basic Concepts Ordered Indices
Indexing and Hashing B.Ramamurthy Chapter 11 2/5/2019 B.Ramamurthy.
CS222p: Principles of Data Management Lecture #4 Catalogs, File Organizations Instructor: Chen Li.
Chapter 11: Indexing and Hashing
Presentation transcript:

11-12. File and Index Structure Department of Computer Science and Engineering, HKUST Slide 1

File Organization Database is stored as a collection of files. Each file is a sequence of records. A record is a sequence of fields. one file  one table one record  one tuple a record/tuple has a fixed length Easy to implement but limited by the file system 10 M table  20,000 pages Hard disk File System DBMS 512-byte pages unix Hard disk DBMS Department of Computer Science and Engineering, HKUST Slide 2

Fixed-Length Records Simple approach: store record i starting from byte n(i - 1), where n is the size of each record. Record access is simple but records may cross disk blocks. When record i is deleted, how do you handle the released space? Shifting records,:move records i+1,…,n to i,…, n-1 move record n to i link all free records on a free list Department of Computer Science and Engineering, HKUST Slide 3

Unordered (Heap) Files Simplest file structure contains records in no particular order. As file grows and shrinks, disk pages are allocated and de-allocated. To support record level operations, we must: keep track of the pages in a file keep track of free space in pages keep track of the records in a page There are many alternatives for keeping track of this. 14

Heap File Using a Page Directory Data Page 1 Page 2 Page N Header Page DIRECTORY The entry for a page can include the number of free bytes on the page. The directory is a collection of pages; linked list implementation is just one alternative. Much smaller than linked list of all HF (Heap File) pages! 16

Sequential File Organization Suitable for application that require sequential processing of the entire file The records in the file are ordered by a search-key Department of Computer Science and Engineering, HKUST Slide 6

Sequential File Organization (cont.) Deletion  use pointer chains Insertion  must locate the position in the file where the record is to be record if there is free space insert there if no free space, insert the record in an overflow block In either case, pointer chain must be updated Need to reorganize the file from time to time to restore sequential order Department of Computer Science and Engineering, HKUST Slide 7

Clustering File Organization Simple file structure stores each relation in a separate file can instead store several relations in one file using a clustering file organization E.g., clustering organization of customer and depositor. customer 1 cluster depositor Good for queries involving depositor customer, and for queries involving one single customer and his accounts Bad for queries involving only customer Result in variable size records Department of Computer Science and Engineering, HKUST Slide 8

Records are physically Indexes and Databases Records are physically stored in a hash table based on a selected key A table (conceptual) Index 1 (Ordered indices, B-tree, hash) Index 2 Department of Computer Science and Engineering, HKUST Slide 9

Basic Concepts Indexing mechanisms speed up access to desired data E.g. If you know the call number (Dewey decimal number) of a book, you can go directly to the book shelve in the library; otherwise you have to search through all the book shelves Search key  attributes used to look up records in a file. An index file consists of records (called index entries) of the format: Index files are typically much smaller than the original file Two basic kinds of indices: Ordered indices: search keys are stored in sorted order Hash indices: search keys are distributed uniformly across “buckets” using a “hash function”. Department of Computer Science and Engineering, HKUST Slide 10

Index Evaluation Metrics How are indexing techniques evaluated? What access operations are supported efficiently? E.g., records with a specified value in an attribute (equality queries) or records with an attribute value falling in a specified range of values (range queries) Access time Insertion time Deletion time Space overhead (size of indexes / size of data records) Which one is more important? Department of Computer Science and Engineering, HKUST Slide 11

Ordered Indices In an ordered index, index entries are stored based on the search key value. E.g., author catalog in library. Indexes are mostly ordered indexes except those based on hash files Primary index: in a sequentially ordered file, the index whose search key specifies the sequential order of the file. Also called clustering index The search key of a primary index is usually but not necessarily the primary key. Secondary index: an index whose search key specifies an order different from the sequential order of the file. Also called non-clustering index. Index-sequential file: ordered sequential file with a primary index. In an employee table, which attributes would you choose as primary or secondary indexes? Department of Computer Science and Engineering, HKUST Slide 12

Index Structure Design: Dense Index Files Every search-key value in the data file is indexed. Dense index Data records Department of Computer Science and Engineering, HKUST Slide 13

Sparse Index Files Not all of the search-key values are indexed Adv: reduce index size Disadv: slower Records in table are sorted by primary key values Search is done in two steps: (i) find the largest possible key (ii) search the record forward Department of Computer Science and Engineering, HKUST Slide 14

Example of Sparse Index Files To locate a record with search-key value K we: find index record with largest search-key value ≤ K search file sequentially starting at the record to which index record points Less space and less maintenance overhead for insertion and deletions. Generally slower than dense index for locating records. Good Tradeoff: sparse index with an index entry for every block in file. The disk must read a block of data into main memory anyway, so searching within the block cost little. Department of Computer Science and Engineering, HKUST Slide 15

Multilevel Index If primary index does not fit in memory, access becomes expensive. (Why?) To reduce number of disk accesses to index records, treat primary index kept on disk as a sequential file and construct a sparse index on it. Outer index  a sparse index of primary index Inner index  the primary index file If even outer index is too large to fit in main memory, yet another level of index can be created, and so on (multi-level indices). Indices at all levels must be updated on insertion or deletion from the file. Department of Computer Science and Engineering, HKUST Slide 16

Multilevel Index (Cont.) Data block 0 Index Block 0 Data block 1 Index Block 1 outer index Data block 2 Inner index Data block 3 Department of Computer Science and Engineering, HKUST Slide 17

Index Update: Deletion If deleted record was the only record in the file with its particular search-key value, the search-key is deleted from the index also. Single-level index deletion: Dense indices  similar to file record deletion Department of Computer Science and Engineering, HKUST Slide 18

Index Update: Deletion in Sparse Indices Sparse indices  if an entry for the search key exists in the index, it is deleted by replacing the entry in the index with the next search-key value in the file (in search-key order). If the next search-key value already has an index entry, the entry is deleted instead of being replaced. Downtown Pointers must be updated (not shown in animation) Department of Computer Science and Engineering, HKUST Slide 19

Index Update: Insertion Single-level index insertion: Perform a lookup using the search-key value appearing in the record to be inserted. Dense indices - if the search-key value does not appear in the index, insert it. Sparse indices - if index stores an entry for each block of the file, no change needs to be made to the index unless a new block is created. In this case, the first search-key value appearing in the new block is inserted into the index. Multilevel insertion (as well as deletion) algorithms are simple extensions of the single-level algorithms. Department of Computer Science and Engineering, HKUST Slide 20

Secondary Indices You can organize a file (say, into indexed sequential file) based on one attribute only (e.g., emp#), but it is not adequate in practice Frequently, one wants to find all the records whose values in a certain field (which is not the search-key of the primary index) satisfy some condition. Example 1: if the account database stored sequentially by account number, we may want to find all accounts in a particular branch. Example 2: as above, but where we want to find all accounts with a specified balance or range of balances. We can have a secondary index with an index record for each search-key value: an index record points to a bucket that contains pointers to all the actual records with that particular search-key value. Department of Computer Science and Engineering, HKUST Slide 21

Secondary Index on Balance Field of Account Note: the file is already indexed based on branch-name Department of Computer Science and Engineering, HKUST Slide 22

Primary and Secondary Indices Secondary indices have to be dense because records are not sorted by the secondary index values Indices offer substantial benefits when searching for records, always select some attributes to index and re-examine the selection periodically When a file is modified, every index on the file must be updated. Updating indices imposes overhead on database modification Sequential scan using primary index is efficient, but a sequential scan using a secondary index is expensive (each record access may fetch a new block from disk.) Department of Computer Science and Engineering, HKUST Slide 23

Hash Files In previous data structure course, you may learn hash table, which is main-memory resident A hash method includes a hash function and a collision handling mechanism In main memory, the unit of access is based on byte or word sizes In disk-based hash file, the unit of access of based on disk block size (from 512 to 2048 bytes, depending on OS) Question: compare the performance of double hashing and chaining methods when the hash table is stored in main memory and on disk. Department of Computer Science and Engineering, HKUST Slide 24

Static External Hashing A hash file consists of M buckets of the same size: bucket0, bucket1,... bucketM-l Bucket 0 Bucket i i = h(K) Record to be inserted with key K Hash function h() Overflow bucket Collisions occur when a new record hashes to a bucket that is already full. A new bucket is created and chained to the overflowed bucket To reduce overflow records, a hash file is typically kept 70-80% full Department of Computer Science and Engineering, HKUST Slide 25

Static External Hashing – Properties The hash function h() should distribute the records uniformly among the buckets; otherwise, search time will increase due to many overflow records An ideal hash function will assign roughly the same number of records to each bucket irrespective of the actual distribution of search-key values in the file consider this class of students, what might be the difference between using HKID number or student ID number as the hash attribute? The number of buckets M must be fixed when the file is created; not good when the file size changes widely As a design criteria, you need to determine the load factor Ordered access on the hash key is very inefficient: retrieve all records bucket by bucket and then sort the records Department of Computer Science and Engineering, HKUST Slide 26

Static External Hashing – Example Hash file organization of account file, using branch-name as key. An example of a hash function defined on a set of charaters in a file organization: There are 10 buckets Take the ASCII code of each character as an integer Sum up the binary representations of the characters and then applies modulo 10 to the sum to get the bucket number Department of Computer Science and Engineering, HKUST Slide 27

Example of hash File Organization Bucket 4 Bucket 7 Bucket 5 Bucket 8 Collision without causing bucket overflow Department of Computer Science and Engineering, HKUST Slide 28

data file (e.g., hash or index sequential on emp#) Hash Indices Hashing can be used not only for file organization, but also for index-structure creation. A hash index organizes the search keys, with their associated record pointers, into a hash file structure. Hash indices are always secondary indices – the file itself may be organized as an external hash file, with secondary hash indices. hash index (e.g., on salary) data file (e.g., hash or index sequential on emp#) Department of Computer Science and Engineering, HKUST Slide 29

Hash Index on account-number Example of hash Index Primary index Used as a secondary index Overflow bucket Bucket 0 Bucket 1 Bucket 2 Bucket 3 Bucket 4 Bucket 5 Bucket 6 Hash Index on account-number Department of Computer Science and Engineering, HKUST Slide 30

Simplistic Analysis We ignore CPU costs, for simplicity: B: Is the number of data pages in the file Measuring number of page I/O’s ignores gains of pre-fetching blocks of pages; thus, even I/O cost is only approximated. Average-case analysis; based on several simplistic assumptions: Single record insert and delete. Heap Files: Equality selection on key; exactly one match. Insert always at end of file. Sorted Files: Files compacted after deletions. Selections on sort field(s). Hashed Files: No overflow buckets, 80% page occupancy. 3

Cost of Operations Several assumptions underlie these (rough) estimates! 6

Data Dictionary Storage Data dictionary (also called system catalog) stores metadata: that is, data about data, such as Information about relations names of relations names and types of attributes of each relation names and definitions of views integrity constraints User and accounting information, including passwords Statistical and descriptive data number of tuples in each relation Physical file organization information How relation is stored (sequential/hash/…) Physical location of relation operating system file name or disk addresses of blocks containing records of the relation Information about indices

Data Dictionary Storage (Cont.) Catalog structure: can use either specialized data structures designed for efficient access a set of relations, with existing system features used to ensure efficient access The latter alternative is usually preferred A possible catalog representation: Relation-metadata = (relation-name, number-of-attributes, storage-organization, location) Attribute-metadata = (attribute-name, relation-name, domain-type, position, length) User-metadata = (user-name, encrypted-password, group) Index-metadata = (index-name, relation-name, index-type, index-attributes) View-metadata = (view-name, definition)

Comparison of Ordered Indexing and Hashing Issues to consider: Cost of periodic re-organization Relative frequency of insertions and deletions Is it desirable to optimize average access time at the expense of worst-case access time? Expected type of queries: Hashing is generally better at retrieving records having a specified value of the key. If range queries are common, ordered indices are preferred. Department of Computer Science and Engineering, HKUST Slide 35