Showing posts with label Time to Live (TTL) Index. Show all posts
Showing posts with label Time to Live (TTL) Index. Show all posts

Wednesday, February 5, 2014

TTL (Time to Live) Indexes in MongoDB

Time To Live or TTL indexes are special kind of indexes which remove the expired documents (rows) from collection (tables) after a fix time. MongoDB provide this feature by which you can create Index which will take care of removing documents (row) after a certain time is passed in the background.

To create a TTL Index the syntax is following:

db.collectionname.ensureIndex ({fieldname:1 OR -1},{expireAfterSeconds: timeinseconds})

For example to create TTL index on empCalendar collection (table) on CreatedOn filed we can write following command. With this, we have created an Index which will start removing all the documents (row) where value in CreatedOn filed will be less than the (current time + 5 minute).

db.empCalendar.ensureIndex({"CreatedOn":1},{expireAfterSeconds: 300})

I created this Index on 4th Feb. As you can see in the below screen we have two documents where value of CreatedOn field is in future. MongoDB will delete all the documents except the two where CreatedOn value is a future date after 5 minutes.

Only two records were left in the Collection (table) after 5 minute.

With TTL Index we can also set a fix time or fix clock after which each of the document will be removed from the Collection.

To understand this let us take an example. We have following data in empCalendar collection.

As you can see each of the documents has their own CreatedOn field which holds a date and time. To create a TTL Index which will remove each of the document in this collection after date and time in CreatedOn is passed, we can issue below command.

db.empCalendar.ensureIndex({"CreatedOn":1},{expireAfterSeconds:0})

There are certain limitations with TTL Index

  • TTL Index can be created on field which stored date values
  • TTL Index cannot be created on multiple fields
  • If a field already has an existing index, TTL index cannot be created on that.
  • The _Id() field does not support TTL Indexes.


Indexes in MongoDB

Like other RDBMS system such as Oracle, SQL Server, DB2 etc. MongoDB provide support for Indexes. Indexes are mainly use for faster performance of read operations. As you now Collection (table) holds the data in MongoDB. Every time we wish to query the data in collection we ask MongoDB to scan all the data and return it to us. Scanning all the data is time consuming process. The more data the collection (table) holds, the more time it takes to read the data.

Indexes come to help here. Indexes stores small part of large data set stored in collection (table) in an order list.

In MongoDB there are two type of Index

1.     System Defined Index
2.     User Defined Index

System Defined Index

MongoDB by default add _Id() index in all the collection (table). The _Id() field is a unique index. A unique index does not allow duplicate value in the field. The _Id() field is stored with each of the document (row) in a Collection (table). A user cannot drop or delete the _Id() index. The default value in this field is an ObjectID. It is a by default added Primary key by MongoDB in every Collection.

So, every time we issue an Insert() method to add document into collection, MongoDB ensue that document is stored with a _Id() field.

User Defined Indexes

MongoDB has provided 6 user defined indexes which can be created by the users as per their requirement.

1.    Unique Index
2.    Compound Index
3.    Array Index
4.    Time to Live (TTL) Index
5.    Text Index
6.    Geospatial Index



Popular Posts

Real Time Web Analytics