Table of contents
1.
Introduction
2.
What are Multikey Indexes?
2.1.
Creating a Multikey Index
2.2.
MongoDB Terminal
2.3.
MongoDB Terminal
2.4.
MongoDB Terminal
2.5.
MongoDB Terminal
2.6.
Indexing With Embedded Document
2.7.
MongoDB Terminal
2.8.
MongoDB Terminal
3.
Benefits of Multikey Indexes
4.
Limitations of Multikey Indexes
5.
Frequently Asked Questions
5.1.
What is Indexing in DBMS?
5.2.
What are Multikey Indexes in DBMS?
5.3.
What are some limitations of Multikey Indexes in DBMS?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Multikey Indexes

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Database is a collection of the necessary data and information. MongoDB is a NoSQL kind of database management system. It can store and handle large volumes of unstructured or semi-structured data with no fixed schema. In it, indexing is required to fetch data quickly. But, there are different kinds of indexing in dbms.

multikey indexes

In this article, we will study Multikey Indexes.

What are Multikey Indexes?

First of all, let us understand indexing in dbms. Indexes are unique data structures that store the mapping of the data addresses from their table to the original database. It helps fetch the data quickly instead of scanning the central database thoroughly. We must browse the index table and find our corresponding location in the database.

Database management systems like MongoDB allow indexing a database field with various elements. It achieves this by creating an index key for all the elements in the array. It is called multikey indexing. It allows indexing over both scalar values and nested documents.

Some points to note when dealing with multikey indexes are as follows:

  • If the indexed field turns out to be an array, MongoDB automatically creates a multikey index.
     
  • Hashed indexes are not multikey indexes in MongoDB. Also, the multikey index does not support the $expr operator.
     
  • When scanning a multikey index field, MongoDB checks for the first matching value, retrieves documents matching it, then reaches for other values in those retrieved documents rather than on all the records.

Creating a Multikey Index

Now, let us see how to create a multikey index. First, we must create some entries in our database named students. We create two different entries in it using the following commands:

  • MongoDB Terminal

MongoDB Terminal

db.students.insertOne({
   name:"Ram", hobbies:["football", "singing"], languages:[{name1: "C++"}, {name2:"Python"
   }]
 })
db.students.insertOne({
   name:"Shyam", hobbies:["cricket", "novels"]
 })

 

Then, using the following command, we can see the entries:

  • MongoDB Terminal

MongoDB Terminal

db.students.find().pretty()

 

OUTPUT

ram entry in students
shyam entry in students

Next, we create an index on the field hobbies using the following command:

  • MongoDB Terminal

MongoDB Terminal

db.students.createIndex({hobbies:1})

 

OUTPUT

creating multikey index

Next, we will check the index using the following command:

  • MongoDB Terminal

MongoDB Terminal

db.students.getIndexes()

 

OUTPUT

created multikey index

Here, we can see that there are multikey indexes for hobbies.

Indexing With Embedded Document

Now, we will create a multikey index on an array field that contains a nested document or objects using the following command:

  • MongoDB Terminal

MongoDB Terminal

db.students.createIndex({"languages.name1":1, "languages.name2":1})

 

OUTPUT

creating multikey index in embedded document

We create it on the languages field that has different objects as name1 and name2.

Now, let us check the indexes with the following command:

  • MongoDB Terminal

MongoDB Terminal

db.students.getIndexes()

 

OUTPUT

embedded document multikey index

Benefits of Multikey Indexes

Let us note down some points of advantages of multikey indexes:-

  • Query Performance:- Multikey indexes improve the query performance. It helps in finding tuples with specific column entries.
     
  • Ordering:- Multikey indexes can support queries with ordering and sorting on different columns. It helps avoid expensive sorting computations.
     
  • Data Integrity:- Multikey indexes, using unique constraints on various columns, ensure that the combinations of elements are unique. It maintains data integrity and prevents inconsistencies.

Limitations of Multikey Indexes

Let us note down some limitations of multikey indexes:

  • You cannot insert documents that may break restrictions if a multikey index exists.
     
  • Multikey indexes increase the size of indexes, increasing storage requirements.
     
  • It makes some queries complex. Thus, it becomes difficult to optimize the queries.
     
  • When the field is an array of documents, you can index an embedded document, but at most, one indexed field can be an array.

Frequently Asked Questions

What is Indexing in DBMS?

Indexes are unique data structures that store the mapping of the data addresses from their table to the original database. It helps fetch the data quickly instead of scanning the original database thoroughly. We just need to browse the index table and find our corresponding location in the database.

What are Multikey Indexes in DBMS?

Database management systems like MongoDB allow indexing a database field with various elements. It achieves this by creating an index key for all the elements in the array. It is called multikey indexing. It allows indexing over both scalar values and nested documents.

What are some limitations of Multikey Indexes in DBMS?

Some limitations of multikey indexes in DBMS are it restricts inserting documents after an index is made, which may break restrictions, demands an increase in storage space, makes some complex queries, etc.

Conclusion

Indexing in DBMS is an essential aspect of it. It helps in fetching the data quickly. It has certain types of it. In this article, we discussed Multikey Indexes. We started with its definition, then we looked at its implementation thoroughly. Finally, we examined its advantages and disadvantages.

If you want to dive deeper into this field, we recommend you read the following articles:-

 

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Codestudio. Also, you can enroll in our courses and check out the mock test and problems available. Please check out our interview experiences and interview bundle for placement preparations. 

Happy Coding!

Live masterclass