Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is a single index faster than a compound index in a query for two keys? (MongoDB, multi-key)
    primarykey
    data
    text
    <p>I have created 4 indexes to test query performance in my collection when quering for two fields of the same document, one of which is an array (needs a multi-key index). Two of the indexes are single and two compound.</p> <p>I am surpised because of getting better performance with one of the single indexes than with the compound ones. I was expecting to obtain the best performace with a compound index, because I understand that it indexes the two fields allowing for faster querying.</p> <p>These are my indexes:</p> <pre><code>{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "bt_twitter.mallorca.mallorca", "name" : "_id_" }, { "v" : 1, "key" : { "epoch_creation_date" :1 }, "ns" : "bt_twitter.mallorca.mallorca", "name" : "epoch_creation_date_1" }, { "v" : 1, "key" : { "related_hashtags" : 1 }, "ns" : "bt_twitter.mallorca.mallorca", "name" : "related_hashtags_1" }, { "v" : 1, "key" : { "epoch_creation_date" : 1, "related_hashtags" : 1 }, "ns" : "bt_twitter.mallorca.mallorca", "name" : "epoch_creation_date_1_related_hashtags_1" } </code></pre> <p>My queries and performance indicators are (hint parameter shows the index used at each query):</p> <p>QUERY 1:</p> <pre><code>active_collection.find( {'epoch_creation_date': {'$exists': True}}, {"_id": 0, "related_hashtags":1} ).hint([("epoch_creation_date", ASCENDING)]).explain() </code></pre> <p>millis: 237</p> <p>nscanned: 101226</p> <p>QUERY 2:</p> <pre><code>active_collection.find( {'epoch_creation_date': {'$exists': True}}, {"_id": 0, "related_hashtags": 1} ).hint([("related_hashtags", ASCENDING)]).explain() </code></pre> <p>millis: 1131</p> <p>nscanned: 306715</p> <p>QUERY 3:</p> <pre><code>active_collection.find( {'epoch_creation_date': {'$exists': True}}, {"_id": 0, "related_hashtags": 1} ).hint([("epoch_creation_date", ASCENDING), ("related_hashtags", ASCENDING)]).explain() </code></pre> <p>millis: 935</p> <p>nscanned: 306715</p> <p>QUERY 4:</p> <pre><code>active_collection.find( {'epoch_creation_date': {'$exists': True}}, {"_id": 0, "related_hashtags": 1} ).hint([("related_hashtags", ASCENDING),("epoch_creation_date", ASCENDING)]).explain() </code></pre> <p>millis: 1165</p> <p>nscanned: 306715</p> <p>QUERY 1 scans less documents, what is probably the reason to be faster. Can somebody help me to understand why is it performing better than queries with compound indexes? Therefore, when is better to use a compound index than a single one?</p> <p>I am reading mongo documentation but these concepts are resulting hard for me to digest.</p> <p>Thanks in advance.</p> <p>UPDATED question (in response to Sammaye and Philipp)</p> <p>This is the result of a full explain()</p> <pre><code>"cursor" : "BtreeCursor epoch_creation_date_1", "isMultiKey" : false, "n" : 101226, "nscannedObjects" : 101226, "nscanned" : 101226, "nscannedObjectsAllPlans" : 101226, "nscannedAllPlans" : 101226, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 242, "indexBounds" : {u'epoch_creation_date': [[{u'$minElement': 1}, {u'$maxElement': 1}]] }, "server" : "vmmongodb:27017" </code></pre> <p>for the following query:</p> <pre><code>active_collection.find( {'epoch_creation_date': {'$exists': True}}, {"_id": 0, "related_hashtags":1}) .hint([("epoch_creation_date", ASCENDING)]).explain() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload