Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize MongoDB Query or Index
    text
    copied!<p>I am running a quite ordinary MongoDB query, nothing really complex or special and I am wondering if the time it takes (> 1 sec) is normal or if there's something wrong with my indexes.</p> <p>I provided an index for this specific query and <code>explain()</code> also tells me it is used, but it makes a full scan of the collection each time and slows down the whole webpage by > 1 sec.</p> <p>The query:</p> <pre><code>db.tog_artikel.find({"art_filter":{"$exists":false},"$where":"this._id == this.art_hauptartikelnr"}) </code></pre> <p>Explained:</p> <pre><code>&gt; db.tog_artikel.find({"art_filter":{"$exists":false},"$where":"this._id == this.art_hauptartikelnr"}).explain() { "cursor" : "BtreeCursor art_filter_1_art_hauptartikelnr_1", "nscanned" : 21306, "nscannedObjects" : 21306, "n" : 21306, "millis" : 1180, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { "art_filter" : [ [ null, null ] ], "art_hauptartikelnr" : [ [ { "$minElement" : 1 }, { "$maxElement" : 1 } ] ] } } </code></pre> <p>The index:</p> <pre><code>{ "v": 1, "key": { "art_filter": 1, "art_hauptartikelnr": 1 }, "ns": "togshop.tog_artikel", "background": true, "name": "art_filter_1_art_hauptartikelnr_1" } </code></pre> <p>Why is the full collection scanned every time? Why is <code>isMultiKey</code> false and how can I optimize this query/index?</p> <p>Environment is a standalone server, MongoDB 2.0.1, 64-Bit Linux, accessed from PHP w/ php-mongo 1.2.6</p>
 

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