简体   繁体   中英

Best index strategy for mongodb

I have a document collection - Basically a history dump for an sql table. Whenever a row is changed, the whole row is stored as json in mongodb with the change timestamp so that diff can be calculated easily. So for each document in mongo, I have a key and timestamp ( there can be multiple documents with same key but different timestamps). Given timeframe T1 and T2, get me the last version of document before T1 and last version of document before T2. Basically, I am calculating diff between those documents to find what has changed between T1 and T2.

What should be the ideal index strategy?

  1. One index for key and a different index for timestamp. Then first use key_index to get all docs and then timestamp to get documents within the range.
  2. Single compound index key_timestamp.

Which one will be performance efficient ?

It really depends on how you will be querying your documents.

One index for key and a different index for timestamp. Then first use key_index to get all docs and then timestamp to get documents within the range.

This would work best if you query with only key in 1st query and only timestamp in 2nd query.

Single compound index key_timestamp.

This works best if you query with only key or key + timestamp in ypour query But will not use index if you query only on timestamp .

Adding some useful links :-

https://docs.mongodb.com/manual/tutorial/create-indexes-to-support-queries/

https://docs.mongodb.com/manual/applications/indexes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM