Docsity

Database Management: Indexing and File Organizations, Summaries of Computer science

Toggle sidebar
bg1
Faloutsos SCS 15-415
1
Carnegie Mellon Univ.
Dept. of Computer Science
15-415 - Database Applications
Lecture #10 (R&G ch8)
File Organizations and Indexing
CMU SCS 15-415 2
Faloutsos
Overview
• Review
• Index classification
• Cost estimation
CMU SCS 15-415 3
Faloutsos
Review: Memory, Disks
• Storage Hierarchy: cache, RAM, disk, tape, …
– Can’t fit everything in RAM (usually).
• “Page” or “Frame” - unit of buffer
management in RAM.
• “Page” or “Block” unit of interaction with disk.
• Importance of “locality” and sequential access
for good disk performance.
• Buffer pool management
– Slots in RAM to hold Pages
– Policy to move Pages between RAM & disk
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Database Management: Indexing and File Organizations and more Summaries Computer science in PDF only on Docsity!

Carnegie Mellon Univ.

Dept. of Computer Science

15-415 - Database Applications

Lecture #10 (R&G ch8)

File Organizations and Indexing

Faloutsos CMU SCS 15-415 2

Overview

• Review

• Index classification

• Cost estimation

Faloutsos CMU SCS 15-415 3

Review: Memory, Disks

• Storage Hierarchy: cache, RAM, disk, tape, …

  • Can’t fit everything in RAM (usually).

• “Page” or “Frame” - unit of buffer

management in RAM.

• “Page” or “Block” unit of interaction with disk.

• Importance of “locality” and sequential access

for good disk performance.

• Buffer pool management

  • Slots in RAM to hold Pages
  • Policy to move Pages between RAM & disk

Faloutsos CMU SCS 15-415 4

Review: File Storage

• Page or block is OK when doing I/O, but

higher levels of DBMS operate on

records, and files of records.

• We saw:

– How to organize records within pages.

– How to keep pages of records on disk

• Today we’ll see:

– How to support operations on files of

records efficiently.

Faloutsos CMU SCS 15-415 5

Files

FILE: A collection of pages, each containing a

collection of records.

• Must support:

  • insert/delete/modify record
  • read a particular record (specified using record id)
  • scan all records (possibly with some conditions on the records to be retrieved) Faloutsos CMU SCS 15-415 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 some order,

or for retrieving a `range’ of records.

– Index File Organizations: (will cover shortly…)

Faloutsos CMU SCS 15-415 10

How to accelerate searches?

• A: Indices, like:

Faloutsos CMU SCS 15-415 11

Example: Simple Index on GPA

Directory 2.5 3 3. 1.2 1.7^ 1.8^ 1.9 2.2 2.4 2.7^ 2.7^ 2.9^ 3.2^ 3.3^ 3.3 3.6 3.8 3.9**^ 4.0 2 Data Records An index contains a collection of data entries , and supports efficient retrieval of records matching a given search condition Data entries: (Index File) (Data file) Faloutsos CMU SCS 15-415 12

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 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).

Faloutsos CMU SCS 15-415 13

Index Search Conditions

• Search condition = <search key, comparison

operator>

Examples…

(1) Condition: Department = “CS”

  • Search key: “CS”
  • Comparison operator: equality (=)

(2) Condition: GPA > 3

  • Search key: 3
  • Comparison operator: greater-than (>) Faloutsos CMU SCS 15-415 14

Overview

• Review

• Index classification

  • Representation of data entries in index
  • Clustered vs. Unclustered
  • Primary vs. Secondary
  • Dense vs. Sparse
  • Single Key vs. Composite
  • Indexing technique

• Cost estimation

Faloutsos CMU SCS 15-415 15

Details

• ‘data entries’ == what we store at the bottom

of the index pages

• what would you use as data entries?

• (3 alternatives here)

Faloutsos CMU SCS 15-415 19

Alternatives for Data Entries (Contd.)

Alternative 1:

Actual data record (with key value k )

– Then, this is a clustering/sparse index, and

constitutes a file organization (like Heap

files or sorted files).

– At most one index on a given collection of

data records can use Alternative 1.

– Saves pointer lookups but can be

expensive to maintain with insertions and

deletions.

Faloutsos CMU SCS 15-415 20

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 Alternative 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 data entries even if search keys

are of fixed length.

  • Even worse, for large rid lists the data entry would have to span multiple pages! Faloutsos CMU SCS 15-415 21

Overview

• Review

• Index classification

  • Representation of data entries in index
  • Clustered vs. Unclustered
  • Primary vs. Secondary
  • Dense vs. Sparse
  • Single Key vs. Composite
  • Indexing technique

• Cost estimation

Faloutsos CMU SCS 15-415 22

Indexing - clustered index example

**Clustering/sparse index on ssn

= =** Faloutsos CMU SCS 15-415 23

Indexing - non-clustered

Non-clustering / dense index Faloutsos CMU SCS 15-415 24

Index Classification - clustered

• Clustered vs. unclustered: If order of data

records is the same as, or `close to’, order of

index data entries, then called clustered index.

Index entries Data entries direct search for (Index File) (Data file) Data Records data entries Data entries Data Records CLUSTERED UNCLUSTERED

Faloutsos CMU SCS 15-415 28

Clustered vs. Unclustered Index

• Cost of retrieving records found in range scan:

  • Clustered: cost = # pages in file w/matching records
  • Unclustered: cost ≈ # of matching index data entries

• What are the tradeoffs????

  • Clustered Pros:
    • Efficient for range searches
    • May be able to do some types of compression
  • Clustered Cons:
    • Expensive to maintain (on the fly or sloppy with reorganization) Faloutsos CMU SCS 15-415 29

Overview

• Review

• Index classification

  • Representation of data entries in index
  • Clustered vs. Unclustered
  • Primary vs. Secondary
  • Dense vs. Sparse
  • Single Key vs. Composite
  • Indexing technique

• Cost estimation

Faloutsos CMU SCS 15-415 30

Primary vs. Secondary Index

• Primary: index key includes the file’s primary

key

• Secondary: any other index

  • Sometimes confused with Alt. 1 vs. Alt. 2/
  • Primary index never contains duplicates
  • Secondary index may contain duplicates
    • If index key contains a candidate key, no duplicates => unique index

Faloutsos CMU SCS 15-415 31

Overview

• Review

• Index classification

  • Representation of data entries in index
  • Clustered vs. Unclustered
  • Primary vs. Secondary
  • Dense vs. Sparse
  • Single Key vs. Composite
  • Indexing technique

• Cost estimation

Faloutsos CMU SCS 15-415 32

Dense vs. Sparse Index

• Dense: at least one

data entry per key

value

• Sparse: an entry per

data page in file

  • Every sparse index is clustered!
  • Sparse indexes are smaller; however, some useful optimizations are based on dense indexes. Ashby, 25, 3000 Smith, 44, 3000 Ashby Cass Smith 22 25 30 40 (^4444) 50 Sparse Index on Name Data File Dense Index on Age 33 Bristow, 30, 2007^ Basu, 33, 4003 Cass, 50, 5004 Tracy, 44, 5004 Daniels, 22, 6003 Jones, 40, 6003 Faloutsos CMU SCS 15-415 33

Overview

• Review

• Index classification

  • Representation of data entries in index
  • Clustered vs. Unclustered
  • Primary vs. Secondary
  • Dense vs. Sparse
  • Single Key vs. Composite
  • Indexing technique

• Cost estimation

Faloutsos CMU SCS 15-415 37

Overview

• Review

• Index classification

  • Representation
  • …

• Cost estimation

Faloutsos CMU SCS 15-415 38

Cost estimation

  • Heap file
  • Sorted
  • Clustered
  • Unclustured tree index
  • Unclustered hash index

Methods Operations(?)

Faloutsos CMU SCS 15-415 39

Cost estimation

  • Heap file
  • Sorted
  • Clustered
  • Unclustured tree index
  • Unclustered hash index
    • scan
    • equality search
    • range search
    • insertion
    • deletion

Methods Operations

• Consider only I/O cost;

• suppose file spans B pages

Faloutsos CMU SCS 15-415 40

Cost estimation

Assume that:

  • Clustered index spans 1.5 B pages (due to empty space)
  • Data entry= 1/10 of data record Faloutsos CMU SCS 15-415 41

Cost estimation

Faloutsos CMU SCS 15-415 42

Cost estimation

  • heap: seq. scan
  • sorted: binary search
  • index search …

#B

Faloutsos CMU SCS 15-415 46

Cost estimation

index – cost?

  • levels of index +
  • blocks w/ qual. tuples …

#B

sec. key – non-clust. index h’ + #qual-records (actually, a bit less...) h’ Faloutsos CMU SCS 15-415 47

Cost estimation

m: # of qualifying pages

m’: # of qualifying records

Faloutsos CMU SCS 15-415 48

Cost estimation

Faloutsos CMU SCS 15-415 49

Cost estimation - big-O notation:

Faloutsos CMU SCS 15-415 50

Index specification in SQL:

CREATE INDEX IndAgeRating ON Students

WITH STRUCTURE=BTREE,

KEY = (age, gpa)

Faloutsos CMU SCS 15-415 51

Summary

• To speed up selection queries: index.

• Terminology:

  • Clustered / non-clustered index
  • primary / secondary index

• Typically, B-tree index

• hashing is only good for equality search

• At most one clustered index per table

  • many non-clustered ones are possible
  • composite indexes are possible