0% found this document useful (0 votes)
15 views12 pages

DB Chapter 5

Chapter Five discusses indexing and hashing techniques used in database management systems. It explains various indexing types such as primary, secondary, and clustering indexes, as well as ordered indexing methods like dense and sparse indexes. The chapter also covers hashing techniques, including static and dynamic hashing, and their operations for efficient data retrieval.

Uploaded by

danielarega25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

DB Chapter 5

Chapter Five discusses indexing and hashing techniques used in database management systems. It explains various indexing types such as primary, secondary, and clustering indexes, as well as ordered indexing methods like dense and sparse indexes. The chapter also covers hashing techniques, including static and dynamic hashing, and their operations for efficient data retrieval.

Uploaded by

danielarega25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter Five Indexing and Hashing Techniques

Chapter Five

Indexing and Hashing Techniques

We know that information in the DBMS files is stored in form of records. Every record is
equipped with some key field, which helps it to be recognized uniquely.

Indexing is a data structure technique to efficiently retrieve records from database files based on
some attributes on which the indexing has been done. Indexing in database systems is similar to
the one we see in books.

Indexing is defined based on its indexing attributes. Indexing can be one of the following types:

 Primary Index: If index is built on ordering 'key-field' of file it is called Primary Index.
Generally it is the primary key of the relation.
 Secondary Index: If index is built on non-ordering field of file it is called Secondary
Index.
 Clustering Index: If index is built on ordering non-key field of file it is called Clustering
Index.

Ordering field is the field on which the records of file are ordered. It can be different from
primary or candidate key of a file.

Ordered Indexing is of two types:

 Dense Index
 Sparse Index

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 record contains
search key value and a pointer to the actual record on the disk.

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 1


Chapter Five Indexing and Hashing Techniques

Sparse Index

In sparse index, index records are not created for every search key. An index record here
contains search key and 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 index, the system starts sequential search until the desired
data is found.

Multilevel Index

Index records are comprised of search-key value and data pointers. This index itself is stored on
the disk along with the actual database files. As the size of database grows so does the size of
indices. There is an immense need to keep the index records in the main memory so that the
search can speed up. If single level index is used then a large size index cannot be kept in
memory as whole and this leads to multiple disk accesses.

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 2


Chapter Five Indexing and Hashing Techniques

Multi-level Index helps breaking down the index into several smaller indices in order to make
the outer most level so small that it can be saved in single disk block which can easily be
accommodated anywhere in the main memory.

B+ Tree
B tree is multi-level index format, which is a balanced binary search tree. As mentioned earlier
single level index records becomes large as the database size grows, which also degrades
performance.

All leaf nodes of B+ tree denote actual data pointers. B+ tree ensures that all leaf nodes remain at
the same height, thus balanced. Additionally, all leaf nodes are linked using link list, which
makes B+ tree to support random access as well as sequential access.

Structure of B+ tree

• Properties of a B+ Tree of order v :


 All internal nodes (except root) has at least v keys and at most 2v keys .
 The root has at least 2 children unless it’s a leaf..
 All leaves are on the same level.
 An internal node with k keys has k+1 children

B+ tree: Internal/root node structure:

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 3


Chapter Five Indexing and Hashing Techniques

B+ tree: leaf node structure:

Example: B+ tree with order of 1:

According to the above rule, each node must hold at least 1 entry, and at most 2 entries.

Example: Search in a B+ tree order 2:

• Search: how to find the records with a given search key value?
– Begin at root, and use key comparisons to go to leaf
• Examples: search for 5*, 16*, all data entries >= 24* ...
– The last one is a range search, we need to do the sequential scan, starting from the
first leaf containing a value >= 24.

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 4


Chapter Five Indexing and Hashing Techniques

Inserting 16*, 8* into Example B+ tree of order 2:

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 5


Chapter Five Indexing and Hashing Techniques

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 6


Chapter Five Indexing and Hashing Techniques

Delete 19* and 20*:

For a huge database structure it is not sometime feasible to search index through all its level and
then reach the destination data block to retrieve the desired data. Hashing is an effective
technique to calculate direct location of data record on the disk without using index structure.

It uses a function, called hash function and generates address when called with search key as
parameters. Hash function computes the location of desired data on the disk.

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 7


Chapter Five Indexing and Hashing Techniques

Hash Organization

 Bucket: Hash file stores data in bucket format. Bucket is considered a unit of storage.
Bucket typically stores one complete disk block, which in turn can store one or more
records.
 Hash Function: A hash function h, is a mapping function that maps all set of search-keys
K to the address where actual records are placed. It is a function from search keys to
bucket addresses.

Static Hashing

In static hashing, when a search-key value is provided the hash function always computes the
same address. For example, if mod-4 hash function is used then it shall generate only 5 values.
The output address shall always be same for that function. The numbers of buckets provided
remain same at all times.

Operation:

 Insertion: When a record is required to be entered using static hash, the hash function h,
computes the bucket address for search key K, where the record will be stored.

Bucket address = h(K)

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 8


Chapter Five Indexing and Hashing Techniques

 Search: When a record needs to be retrieved the same hash function can be used to
retrieve the address of bucket where the data is stored.
 Delete: This is simply search followed by deletion operation.

Bucket Overflow:

The condition of bucket-overflow is known as collision. This is a fatal state for any static hash
function. In this case overflow chaining can be used.

 Overflow Chaining: When buckets are full, a new bucket is allocated for the same hash
result and is linked after the previous one. This mechanism is called Closed Hashing.

Linear Probing: When hash function generates an address at which data is already stored, the
next free bucket is allocated to it. This mechanism is called Open Hashing.

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 9


Chapter Five Indexing and Hashing Techniques

For a hash function to work efficiently and effectively the following must match:

 Distribution of records should be uniform


 Distribution should be random instead of any ordering

Dynamic Hashing

Problem with static hashing is that it does not expand or shrink dynamically as the size of
database grows or shrinks. Dynamic hashing provides a mechanism in which data buckets are
added and removed dynamically and on-demand. Dynamic hashing is also known as extended
hashing.

Hash function, in dynamic hashing, is made to produce large number of values and only a few
are used initially.

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 10


Chapter Five Indexing and Hashing Techniques

Organization

The prefix of entire hash value is taken as hash index. Only a portion of hash value is used for
computing bucket addresses. Every hash index has a depth value, which tells it how many bits
are used for computing hash function. These bits are capable to address 2n buckets. When all
these bits are consumed, that is, all buckets are full, then the depth value is increased linearly and
twice the buckets are allocated.

Operation

 Querying: Look at the depth value of hash index and use those bits to compute the
bucket address.
 Update: Perform a query as above and update data.
 Deletion: Perform a query to locate desired data and delete data.
 Insertion: compute the address of bucket
o If the bucket is already full
 Add more buckets

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 11


Chapter Five Indexing and Hashing Techniques

 Add additional bit to hash value


 Re-compute the hash function
o Else
 Add data to the bucket
o If all buckets are full, perform the remedies of static hashing.

Hashing is not favorable when the data is organized in some ordering and queries require range
of data. When data is discrete and random, hash performs the best.

Hashing algorithm and implementation have high complexity than indexing. All hash operations
are done in constant time.

Tulu Tilahun and Tigist Seyum Lecturers at AMU Page 12

You might also like