Primary Index and Cluster Index

In the last blog post we understand how DB Index can improve performance. In this post we will go through Primary Index, Cluster Index.

Primary Index

In a table or a collection a Primary Index in an index on a set of keys that includes the unique primary key. For example, in a user table the id key is unique so database creates a primary index for that key by default. You can create primary index by yourself for key like, username, email etc.

Let’s create a user table.

idnameusernameemail
1Mizanur Lahinlahin31lahin@gmail.com
2Desh Chowdhurydeshdesh@gmail.com
3Mujammal Ahmedmujammalmujammal@gmail.com
4Sadikul Islamsadiksadik@gmail.com

In this table the id is the primary key so the id is set to primary index by default.

How index table look like,

blockid
11
23

The index table holds block reference and another one is the actual id of the user.

This is also called Sparse.

Cluster Index

Cluster Index is an index that need to be sorted and the index value will be repeated to different rows of the database table. Let’s look at an example of it,

idnamedepartment
1LahinComputer Science
2ShajuComputer Science
3SadikEconomics
4NafisaEconomics
5RaselEnglish

As you can see the Computer Science and Economics department is repeated. Doesn’t matter it is repeatable or not, in index table this will create a single row for Computer Science and Economics as well as rest of them. So the index table will be like this,

blockdepartment
1Computer Science
1Economics
2English

Our block and index table,

This is also called Dense.

Since the Economics Department exists in two blocks, Block 1 and Block 2, that’s why Block 1 has a pointer, which is called Block Hanker, that points to Block 2.

In Cluster Index the actual value of index attribute can be repeated and thats why there are multiple reference to a single block from index table. This is why it is called Dense.

Leave a Comment

Your email address will not be published. Required fields are marked *