11.2 Indexing
11.2 Indexing
MSIT-2K24
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 used to look up records in a file.
An index file consists of records (called index entries) of the form
search-key pointer
Index files are typically much smaller than the original file
INDEXES:
3
INDEX EVALUATION METRICS
Access types supported efficiently.
records with a specified value in the attribute, or
records with an attribute value falling in a specified range of values.
Access time
Insertion time
Deletion time
Space overhead
INDEXING TYPES
INDEXING TYPES
ORDERED INDICES
In an ordered index, index entries are stored sorted on the search key value.
Example: Suppose we have an employee table with thousands of record and each of which is
10 bytes long. If their IDs start with 1, 2, 3....and so on and we have to search student with ID-
543.
In the case of a database with no index, we have to search the disk block from starting till it
reaches 543. The DBMS will read the record after reading 543*10=5430 bytes.
In the case of an index, we will search using indexes and the DBMS will read the record after
reading 542*2= 1084 bytes which are very less compared to the previous case.
ORDERED INDICES
Primary index: in a sequentially ordered file, the index whose search key specifies the
sequential order of the file.
If the index is created on the basis of the primary key of the table, then it is known
as primary indexing. These primary keys are unique to each record and contain 1:1
relation between the records.
The primary index can be classified into two types:
Dense index
Sparse index.
DENSE INDEX
9
DENSE INDEX FILES
Dense index — Index record appears for every search-key value in the file.
E.g. index on ID attribute of instructor relation
SPARSE INDEX
11
SPARSE INDEX FILES WITH EXAMPLE
Sparse Index: contains index records for only some search-key values.
Applicable when records are sequentially ordered on search-key
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 the index record points
Ordered by ID
SPARSE INDEX FILES (CONT.)
Compared to dense indices:
Less space and less maintenance overhead for insertions and deletions.
Generally slower than dense index for locating records.
Good tradeoff: sparse index with an index entry for every block in file,
corresponding to least search-key value in the block.
CLUSTER INDEX
14
CLUSTERING INDEX WITH EXAMPLE
An ordered data file
Sometimes the index is created on non-primary key columns which may not be unique for
each record.
To identify the record faster
group two or more columns to get the unique value and create index out of them.
The records which have similar characteristics are grouped, and indexes are created for these
group.
CLUSTER- EXAMPLE
16
SECONDARY INDEX
17
SECONDARY INDICES EXAMPLE
Index record points to a bucket that contains pointers to all the actual records with that
particular search-key value.
Secondary indices have to be dense
PRIMARY AND SECONDARY INDICES
Indices offer substantial benefits when searching for records.
BUT: Updating indices imposes overhead on database modification --when a
file is modified, every index on the file must be updated,
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
Block fetch requires about 5 to 10 milliseconds, versus about 100
nanoseconds for memory access
MULTILEVEL INDEX
If primary index does not fit in memory, access becomes expensive.
Solution: 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.
Indices at all levels must be updated on insertion or deletion from the file.
MULTILEVEL INDEX (CONT.)
INDEX UPDATE: DELETION
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.
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.
If a new block is created, the first search-key value appearing in the new block is
What would be the best, average and worst scenarios related to index
management?
25
THANK YOU
26