written 7.9 years ago by |
Indexing is a data structure technique to efficiently retrieve records from the database files based on some attributes on which the indexing has been done. Indexing in database systems is similar to what we see in books.
Indexing is defined based on its indexing attributes. Indexing can be of the following types −
- Primary Index − Primary index is defined on an ordered data file. The data file is ordered on a key field. The key field is generally the primary key of the relation.
- Single – level ordered indexes
Indexing is a data structure technique to efficiently retrieve records from database files based on some attribute on which the indexing is done.
Indexing can be one of the following types:
- Primary index:
- If index is built on ordering ‘keyfield’ of file it is called primary index.
- Generally it is the primary key of the relation.
- The second key is block pointer which gives the address of the disk block.
- Primary indexes are also called sparse indexes.
- The index file contains primary key along with the pointer to the address of data block where records are stored.
- The keys are matched with values stored in primary key-field of data file and the matching record is accessed without having to reach each field in entire file.
- Secondary Indexes
In provides an alternative means to access file that contains database records.
Secondary Index
Secondary index may be generated from a field which is a candidate key and has a unique value in every record, or a non-key with duplicate values.
These indexes are based on either candidate keys containing unique values or non-key attributes containing duplicate values.
- Secondary indexes can be considered as ordered files and having 2 fields.
- The data type of one field is the same to that of a non-ordering indexing field of file containing records.
- The data type of other field is a pointer that points to either a block or a record.
- A number of secondary indexes can be created for same file.
- Clustered indexes
- Clustering indexes are defined on file containing data in an ordered manner.
- These indexes use no key to order the records in the file
- Clustering indexes require every different value of n field to have a distinct entry for itself.
- Clustering indexes are also called sparse indexes.
Clustering Index − Clustering index is defined on an ordered data file. The data file is ordered on a non-key field.
Ordered Indexing is of two types: Dense Index and Sparse Index
3.1. Dense Index
In dense index, there is an index record for every search key value in the database. This makes searching faster but requires more space to store index records itself. Index records contain search key value and a pointer to the actual record on the disk.
3.2. Sparse Index
In sparse index, index records are not created for every search key. An index record here contains a search key and an actual pointer to the data on the disk. To search a record, we first proceed by index record and reach at the actual location of the data. If the data we are looking for is not where we directly reach by following the index, then the system starts sequential search until the desired data is found.
3.3. Multilevel Index
Index records comprise search-key values and data pointers. Multilevel index is stored on the disk along with the actual database files. As the size of the database grows, so does the size of the indices. There is an immense need to keep the index records in the main memory so as to speed up the search operations. If single-level index is used, then a large size index cannot be kept in memory which leads to multiple disk accesses.
Multi-level Index helps in breaking down the index into several smaller indices in order to make the outermost level so small that it can be saved in a single disk block, which can easily be accommodated anywhere in the main memory.
B+ Tree
A B+ tree is a balanced binary search tree that follows a multi-level index format. The leaf nodes of a B+ tree denote actual data pointers. B+ tree ensures that all leaf nodes remain at the same height, thus balanced. Additionally, the leaf nodes are linked using a link list; therefore, a B+ tree can support random access as well as sequential access.
Structure of B+ Tree
Every leaf node is at equal distance from the root node. B+ tree is of the order n where nis fixed for every B+ tree.