R
R
Roman2017-09-18 15:07:39
MongoDB
Roman, 2017-09-18 15:07:39

How to properly build an index for mongodb?

Good afternoon! Tell me what could be the problem and how to do it right?
there is a collection of documents . As documents from this collection are processed, the is_processed: true field is added to the document to
create a sparse index on this field. At first everything worked fine:
db.documents.find({is_processed: {$exists: false}}).limit(200)
later, when processing started, I noticed that the records were pulled out for a very long time (previously 200 records in a couple of seconds, and now I don't even wait until the end.
Killed an index, created on new, does not work. What can be wrong?
mongo is running in docker, the number of documents is about 4,500,000

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Makarov, 2017-09-18
@losaped

When using sparse index in the selection on "heterogeneous" documents, you need to use hint, plus you do not need to add $exists. And of course, we should not forget that an index with 2 values ​​(1:0 / true:false, etc.) is always ineffective.
Documents are heterogeneous when some properties that other documents have are missing, and an index is built on these properties.
or

db.documents.find().sort({is_processed: 1}).hint({is_processed: 1}).limit(200)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question