55
Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s Guide , 1897

Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Embed Size (px)

Citation preview

Page 1: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Instructor: Marina Gavrilova

If you don’t find it in the index, look very carefully through the entire catalogue.-- Sears, Roebuck, and Co., Consumer’s Guide , 1897

Page 2: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Outline of Presentation1. Data on external storage – review2. Data Structure for file organization

• Heaps and Sorted• Indexes

• B and B+ tree Indexes• Hash based indexes• Index classification

3. Comparing file organization• Assumptions • Cost of operations

4. Summary5. Review Questions

Page 3: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

In this lecture we will study different type of data structures for file organization and discuss difference between them based on operations and cost model analysis.

Goal

Page 4: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

World Largest Databases10. World Data Centre for Climate

This database is controlled and maintained by the German Climate Computing Centre as well as the Max Planck Institute for Meteorology. This database could be examined to find the patterns that led to the severe changes in the climatic conditions.9. National Energy Research Scientific Computing CenterThe National Energy Research Scientific Computing Center is the second largest database of the world. It is controlled by the Lawrence Berkeley National Laboratory in the United States of America. It holds research information related to atomic energy, high energy physics, theories related to various topics, etc.

Page 5: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

8. This is similar to Sprint and is the oldest company that deals with telecommunications. It holds over 310 terabytes of information and almost 2 trillion rows- making the call records extremely extensive. In addition to that, one can also find old records. Therefore, if your grandfather ever made a call using AT&T, the company will probably still have the records

7. Google Google has never made the true size of their database public. However, the type and

amount of information found on the website is overwhelming. According to statistics, over 90 million searches are carried out every day. Google has been called the king of internet databases.

6. Sprint The telecommunication company has over 50 million subscribers. In the past, it

offered long distance packages as well. The records are highly detailed and holds at least 3 trillion rows of database, over 350 call records on a daily basis and 70,000 insertions every second. Sprint is quite notorious and infamous and expands quite rapidly.

Page 6: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

5. ChoicePoint is basically a phone book which contains information about the population residing in the United States of America. It holds criminal histories as well as driving records. It has been said that the database would reach the moon and back at least 75 times. ChoicePoint has helped a number of authorities solve difficult and complicated cases in the past.

4. YouTubeYouTube has been in operation for two years and holds a massive library of videos. A loyal user base follows the website and according to records-over a 100 million clips are watched on a daily basis. The size of this database seems to double every 5 months. Therefore, it goes without saying that the overall statistics are staggering.

3. AmazonThis website holds over 250,000 textbooks and users can comment and interact with other users which makes Amazon the largest community on the web. Amazon has 55 million customers and above 40 terabytes of data.

Page 7: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

2. CIA database collects information on everything ranging from places to things to people. Even though the accurate size of this database is unknown, it holds both private and public information. 100 articles are added to the library every month and includes population statistics, maps as swell as military capabilities.

1. Library of Congress Even after the onset of the digital age, the Library of Congress is still

among the largest databases of the world. It holds over 125 million items which consist of colonial newspapers, cook books and government proceedings. The library expands every day.

http://www.worldsbiggests.com/2010/02/top-10-largest-databases-in-world.html

Page 8: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Data Structures for File Organizations

Many data representations exist, each ideal for some situations, and not so good in others:2.1 Indexes: Data

structures to organize records via trees or hashing. Like sorted files, they speed

up searches for a subset of records, based on values in certain (“search key”) fields

Updates are much faster than in sorted files.

K-d Trees

Page 9: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Data Structures for File Organizations2.2 Sorted Files: Best if records must be retrieved in some order, or only a `range’ of records is needed.2.3 Heap (random order) files: Suitable when typical access is a file scan retrieving all records.

Page 10: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

2.1 IndexesAn 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 unique key

(minimal set of fields that uniquely identify a record in a relation).

An index contains a collection of data entries, and supports efficient retrieval of all data entries k* with a given key value k.Given data entry k*, we can find record with

key k in at most one disk I/O.

Page 11: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

A B-Tree is a tree in which each node may have multiple children and multiple keys.

It is specially designed to allow efficient searching for keys.

Like a binary search tree each key has the property that all keys to the left are lower and all keys to the right are greater.

B-Trees

Page 12: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

From node 10 in the tree all keys to the left are less than 10 and all keys to the right are greater than 10 and less than 20.

The key in a given node represents an upper or lower bound on the sets of keys below it in the tree.

B-Trees

B-Tree

Page 13: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

A tree may also have nodes with several ordered keys. For example, if each node can have three keys, then it will also have four references (pointers to children).

In this node (:20:40:60:) the reference to the left of 20 refers to nodes with keys less than 20, the reference between 20 & 40 refers to nodes with keys from 21 to 39, the reference between keys 40 & 60 to nodes with keys between 41 and 59, and finally the reference to the right of 60 refers to nodes with keys with values greater than 61.

B-Trees

Node of a B-Tree

Page 14: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B-Trees

Organizational basis of the B-Tree

For m references there must be (m-1) keys in a given node. Typically a B-tree is specified in terms of the maximum number of successors that a given node may have. This is also equivalent to the number of references that may occupy a single node, also called the order of the tree.

However, sometimes order is defined as the number of keys (but not in this course).

Page 15: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B-Trees

Constraints

For an order m B-tree no node has more than m subtrees.

Every node except the root and the leaves must have at least m/2 subtrees.

A leaf node must have at least m/2 -1 keys.

The root has 0 or >= 2 subtrees.

Terminal or leaf nodes are all at the same depth.

Within a node, the keys are in ascending order

Page 16: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B-Trees

Construction of a B-Tree

The B-tree is built differently than a binary search tree.

The binary search tree is constructed starting at the root and working toward the leaves.

A B-tree is constructed from the leaves and as it grows the tree is pushed upward.

Page 17: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B-Trees

Construction of a B-Tree Suppose, the tree of order 4 and each node can hold a maximum of 3 keys. The keys are always kept in ascending order within a node. Because the tree is of order 4, every node except the root and leaves must have at least 2 subtrees (or one key which has a pointer to a node containing keys which are less than the key in the parent node and a pointer to a node containing key(s) which are greater than the key in the parent node). This essentially defines a minimum number of keys which must exist within any given node.

Page 18: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B-Trees

Construction of a B-Tree (continued)

If random data are used for the insertions into the B-tree, it generally will be within a level of minimum height.

However, as the data become ordered the B-tree degenerates.

The worst case is for data which is sorted in which case an order 4 B-tree becomes an order 2 tree or a binary search tree. This obviously results in much wasted space and a substantial loss of search efficiency.

Page 19: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

19

B-Tree insertion example

Page 20: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Steps for Insertion If after inserting the node into the appropriate

sorted order, no inner node is over its key capacity, the process is finished.

If some node has more than the maximum amount of child nodes then it is split into two nodes, each with the minimum amount of child nodes. This process continues action recursively in the parent node.

20

B-Tree insertion

Page 21: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B-Trees

Deletions from B-Trees

Deletions also must be done from the leaves.

Simple Deletion: Remove some key from the leaf and there are still enough keys in the leaf so that there are (m/2-1) keys in total. The removal of keys from the leaves can occur under two circumstances: - when the key actually exists in the leaf of the tree, and - when the key exists in an internal leaf and must be moved to a leaf by determining which leaf position contains the key closest to the one to be removed.

Page 22: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Locate the in-order successor of the key to remove and replace it with the key

If the leaf node is in legal state (min capacity not violated) then finished.

If some inner node is in an illegal state then: Redistribute Its siblings node (a child of the same

parent node) can transfer one of its keys to the current node.

Concatenate Its siblings does not have an extra key to share. In that case both these nodes are merged into a single node (together with a key from a parent) and pointers updated accordingly. The process continues until the parent node remains in a legal state or until the root node is reached.

22

B-Tree deletion

Page 23: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B-Trees

Efficiency of B-Trees

Height: Same as the height of a binary tree.

In binary tree, the height of a binary tree is related to the number of nodes through log2.

Here, the height of a B-Tree is related through log m where m is the order of the tree:

height = logm n + 1

Page 24: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Summary

A B-Tree is a tree in which each node may have multiple children and multiple keys.

It is specially designed to allow efficient searching for keys and is much more compact than BST tree.

B-tree insertion involves splitting the node Insertion is easier than deletion operation

Page 25: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

B and B+ Tree Indexes

Leaf pages contain data entries, and are chained (prev & next) Non-leaf pages have index entries; only used to direct searches:

P0 K 1 P 1 K 2 P 2 K m P m

index entry

Non-leaf

Pages

Pages (Sorted by search key)

Leaf

Page 26: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Example B+ Tree

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 propagates to the root!

2* 3*

Root

17

30

14* 16* 33* 34* 38* 39*

135

7*5* 8* 22* 24*

27

27* 29*

Entries <= 17 Entries > 17

Note how data entriesin leaf level are sorted

Page 27: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

27

Approaches to Search

1.Sequential and list methods (lists, tables, arrays).

2. Direct access by key value (hashing)

3. Tree indexing methods.

Hash-Based Indexes

Page 28: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

28

Definition

Hashing is the process of mapping a key value to a position in a table.

A hash function maps key values to positions.

A hash table is an array that holds the records.

Searching in a hash table can be done in O(1) regardless of the hash table size.

Page 29: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

29

Page 30: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

30

Applications of Hashing

Compilers use hash tables to keep track of declared variables

A hash table can be used for on-line spelling checkers — if

misspelling detection (rather than correction) is important, an entire dictionary can be hashed and words checked in constant time

Game playing programs use hash tables to store seen positions,

thereby saving computation time if the position is encountered

again

Hash functions can be used to quickly check for inequality — if

two elements hash to different values they must be different

Storing sparse data

Page 31: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Hash-Based IndexesIndex 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.

Page 32: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Alternatives for Data Entry k* in IndexIn a data entry k* we can store:

Data record with key value k, or <k, rid of data record with search key value k>,

or <k, list of rids of data records with search key k>

Choice of alternative for data entries depends on the indexing technique used to locate data entries with a given key value k.Examples of indexing techniques: B+ trees, hash-

based structuresTypically, index contains auxiliary information that

directs searches to the desired data entries

Page 33: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Alternatives for Data Entries (Contd.)Alternative 1: Data record with key value k

If this is used, index structure is a file organization for data records.

At most one index on a given collection of data records can use Alternative 1. (Otherwise, large data records are duplicated, leading to redundant storage and potential inconsistency.)

If data records are very large, # of pages containing data entries is high. Implies size of auxiliary information in the index is also large, typically.

Page 34: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Alternatives for Data Entries

Alternative 2 <k, rid of data record with search key value k>:

Data entries typically much smaller than data records. So, better than Alternative 1 with large data records, especially if search keys are small.

Alternative 3 <k, list of rids of data records with search key k>

Alternative 3 more compact than Alternative 2, but leads to variable sized data entries even if search keys are of fixed length.

Page 35: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Index ClassificationPrimary vs. secondary: If search key contains

primary key, then called primary index.Unique index: Search key contains a candidate key.

Clustered vs. un-clustered: If order of data records is the same as, or `close to’, order of 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!

Page 36: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Clustered vs. Unclustered IndexSuppose 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 page for future inserts).

Overflow pages may be needed for inserts. (Thus, order of data recs is `close to’, but not identical to, the sort order.)

Index entries

Data entries

direct search for

(Index File)

(Data file)

Data Records

data entries

Data entries

Data Records

CLUSTERED UNCLUSTERED

Page 37: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

37

Clustering DefinitionClustering Definition

Clustering is the unsupervised classification of patterns (observations, data items or feature vectors) into groups (clusters).

– A.K. Jain, M. N. Murty,

P. J. Flynn, Data Clustering: A Review

Clustering a collection of points

Page 38: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

38

Clustering Properties Linear increase in processing time with increase in size of

dataset (Scalability).

Ability to detect clusters of different shapes and densities.

Minimal input parameter.

Robust with regard to noise.

Insensitive to data input order.

Extensible to higher dimensions.

Osmar R. Zaΐane, Andrew Foss, Chi-Hoon Lee, Weinan Wang, “On Data Clustering Analysis: Scalability, Constraints and Validation”, Advances in Knowledge Discovery and Data Mining, Springer-Verlag, 2002.

Page 39: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

39

Clusters

t7.10k dataset (9 visible clusters, n = 10,000)

Page 40: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

40

A

B

C

D

E

F

G

H

AA K-MeansK-Means k = 9k = 9

BB CURECURE k = 9, k = 9, αα = 0.3 and = 0.3 and 10 representative 10 representative points per clusterpoints per cluster

CC ROCKROCK θθ = 0.975 and k = = 0.975 and k = 10001000

DD CHAMELEONCHAMELEON K-NN = 10, MinSize K-NN = 10, MinSize = 2.5%, k = 9= 2.5%, k = 9

EE DBSCANDBSCAN єє = 5.9, MinPts = 4 = 5.9, MinPts = 4

FF DBSCANDBSCAN єє = 5.5, MinPts = 4 = 5.5, MinPts = 4

GG WaveClusterWaveCluster Resolution = 5, Resolution = 5, ГГ = = 1.51.5

HH WaveClusterWaveCluster Resolution = 5, Resolution = 5, ГГ = = 1.9993971.999397Clustering results on t7.10k dataset

Osmar R. Zaΐane, Andrew Foss, Chi-Hoon Lee, Weinan Wang, “On Data Clustering Analysis: Scalability, Constraints and Validation”

Page 41: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Example: Indexing & Searching One of the World's Largest Compound Databasehttp://accelrys.com/resource-center/case-studies/pharmacopeia-database.html

The Data Challenge

Pharmacopeia's corporate compound collection contains over seven million molecules. These are typically small drug-like molecules: organic compounds, of which 99% fall within a range of molecular weights range from 250 to 750. A typical biotechnology company has a database of only hundreds of thousands of compounds, while major pharmaceuticals may have collections approaching the same order of magnitude as Pharmacopeia's.

A Single, Integrated, Open System

Pharmacopeia has tested the DS Accord Chemistry Cartridge for use in data mining and subsequent analysis to assess novel libraries of compounds that are planned for synthesis. Creating such a library can take over a man-year. To enable such library construction and analysis, Accord users will need to routinely conduct sophisticated and varied searches on the multi-million compound database. Pharmacopeia's developers took just a couple of hours to build a prototype of a customized client user interface using Oracle forms and PL/SQL. This enabled substructure searching within their internal compound collection.

Page 42: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Cost Model for Our AnalysisWe ignore CPU costs, for simplicity:

B: The number of data pagesR: Number of records per pageD: (Average) time to read or write disk pageMeasuring number of page I/O’s ignores

gains of pre-fetching a sequence of pages; thus, even I/O cost is only approximated.

Average-case analysis; based on several simplistic assumptions.

Good enough to show the overall trends!

Page 43: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Comparing File Organizations Heap files (random order; insert at eof) Sorted files, sorted on <age, sal> Clustered B+ tree file, Alternative (1),

search key <age, sal> Heap file with unclustered B + tree index on

search key <age, sal> Heap file with unclustered hash index on

search key <age, sal>

Page 44: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Operations to Compare Scan: Fetch all records from disk Equality search Range selection Insert a record Delete a record

Page 45: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Assumptions in Our AnalysisHeap 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

Tree: 67% occupancy (this is typical). Implies file size = 1.5 data size

Page 46: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

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.

Page 47: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Cost of Operations (a) Scan (b)

Equality (c ) Range (d) Insert (e) Delete

(1) Heap

(2) Sorted

(3) Clustered

(4) Unclustered Tree index

(5) Unclustered Hash index

Several assumptions underlie these (rough) estimates!

B: The number of data pagesR: Number of records per pageD: (Average) time to read or write disk page

Page 48: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Cost of Operations (a) Scan (b) Equality (c ) Range (d) Insert (e) Delete

(1) Heap BD 0.5BD BD 2D Search +D

(2) Sorted BD Dlog 2B D(log 2 B + # pgs with match recs)

Search + BD

Search +BD

(3) Clustered

1.5BD Dlog F 1.5B D(log F 1.5B + # pgs w. match recs)

Search + D

Search +D

(4) Unclust. Tree index

BD(R+0.15) D(1 + log F 0.15B)

D(log F 0.15B + # pgs w. match recs)

Search + 2D

Search + 2D

(5) Unclust. Hash index

BD(R+0.125) 2D BD Search + 2D

Search + 2D

Several assumptions underlie these (rough) estimates!

Page 49: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Choice of IndexesWhat indexes should we create?

Which relations should have indexes? What field(s) should be the search key? Should we build several indexes?

For each index, what kind of an index should it be?Clustered? Hash/tree?

Page 50: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Choice of Indexes (Contd.)One approach: Consider the most important

queries in turn. Consider the best plan using the current indexes, and see if a better plan is possible with an additional index. If so, create it.Obviously, this implies that we must understand how

a DBMS evaluates queries and creates query evaluation plans!

For now, we discuss simple 1-table queries.Before creating an index, must also consider the

impact on updates in the workload!Trade-off: Indexes can make queries go faster,

updates slower. Require disk space, too.

Page 51: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Index Selection GuidelinesAttributes in WHERE clause are candidates for index

keys.Exact match condition suggests hash index.Range query suggests tree index.

Clustering is especially useful for range queries; can also help on equality queries if there are many duplicates.

Multi-attribute search keys should be considered when a WHERE clause contains several conditions. Order of attributes is important for range queries. Such indexes can sometimes enable index-only strategies for

important queries. For index-only strategies, clustering is not important!

Try to choose indexes that benefit as many queries as possible. Since only one index can be clustered per relation, choose it based on important queries that would benefit the most from clustering.

Page 52: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

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

Page 53: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Summary (Contd.)Data entries can be actual data records,

<key, rid> pairs, or <key, rid-list> pairs.Choice orthogonal to indexing technique used

to locate data entries with a given key value.Can have several indexes on a given file of

data records, each with a different search key.

Indexes can be classified as clustered vs. unclustered, primary vs. secondary. Differences have important consequences for utility/performance.

Page 54: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Summary (Contd.)Understanding the nature of the workload for

the application, and the performance goals, is essential to developing a good design.What are the important queries and updates?

What attributes/relations are involved? Indexes must be chosen to speed up important

queries (and perhaps some updates!).Index maintenance overhead on updates to key

fields.Choose indexes that can help many queries, if

possible.Build indexes to support index-only strategies.Clustering is an important decision; only one index

on a given relation can be clustered!

Page 55: Instructor: Marina Gavrilova If you don’t find it in the index, look very carefully through the entire catalogue. -- Sears, Roebuck, and Co., Consumer’s

Review Questions 1. What is an external storage and name at least three of them.2. How does a DBMS organize files of data records on disk to minimize I/O costs?3. How can we compare different file data structures.4. What is hash based indexes ?5. What are different ways of index classification.6. How can we apply cost model analysis in file data structure comparison?7. What is an index, and why is it used?8. Explain with example B+ tree indexes.9. Why is I/O cost so important for database operations?10. What are important properties of indexes?