Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I troubleshoot and improve this slow running query?
    primarykey
    data
    text
    <p>I'm looking to fine tune a string search query that I am using on Mongo. In the SQL Server world, I'd like to believe I have a decent understanding of how indexes work and how to build proper indexes. I tried giving it a shot with Mongo, but, I don't believe that I'm not going about it the right way.</p> <p>My collection has roughly 4.3 million documents. The document structure looks like this:</p> <pre class="lang-javascript prettyprint-override"><code>{ "_id":{ "$oid":"527027456239d1212c07a621" }, "ReleaseId":2451, "Status":"Accepted", "Title":"Hard Rhythmic Motions", "Country":"US", "MasterId":"35976", "Images":[ { "Type":"primary", "URI":"http://api.discogs.com/image/R-2451-1117047026.jpg", "URI150":"http://api.discogs.com/image/R-150-2451-1117047026.jpg", "Height":307, "Width":307 }, { "Type":"secondary", "URI":"http://api.discogs.com/image/R-2451-1117047033.jpg", "URI150":"http://api.discogs.com/image/R-150-2451-1117047033.jpg", "Height":307, "Width":307 } ], "Artists":[ { "_id":2894, "Name":"DJ Hyperactive" } ], "Formats":[ { "Name":null, "Quantity":1 } ], "Genres":[ "Electronic" ], "Styles":[ "Hardcore", "Acid" ] } </code></pre> <p>I am executing a case insensitive search on one of the top-level document properties and on one of the nested document properties:</p> <pre class="lang-javascript prettyprint-override"><code>db.releases.find({$or: [{Title: new RegExp('.*mozart.*',"i")},{'Artists.Name': new RegExp('.*mozart.*',"i")}]}) </code></pre> <p>I tried creating an index; when I execute <code>.getIndexes()</code> I can see the index I created:</p> <pre class="lang-javascript prettyprint-override"><code>{ "v" : 1, "key" : { "Title" : 1, "Artists.Name" : 1 }, "ns" : "discogs.releases", "name" : "Title_1_Artists.Name_1" } </code></pre> <p>At this point I thought that I would be all set. However, the query ends up taking between 28 and 32 seconds to execute. I tried calling <code>.explain()</code> to get a little more insight:</p> <pre class="lang-javascript prettyprint-override"><code>{ "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 4098, "nscannedObjects" : 4292400, "nscanned" : 4292400, "nscannedObjectsAllPlans" : 4292400, "nscannedAllPlans" : 4292400, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 29, "nChunkSkips" : 0, "millis" : 29958, "indexBounds" : { }, "server" : "lambic:27017" } </code></pre> <p>From my limited knowledge of Mongo, this looks like a table scan which is why the query isn't performing very well. However, I don't know how to make this query better! I would expect that the index that I created to cover this query, but, that must not be the case.</p> <p>Now, the last thing I want to point out is that this is certainly not on the most robust server. The hardware specs (including CPU and RAM) are very limited. However, if my analysis is correct and I'm doing a table scan, there must be some performance improvements I can make on the Mongo side.</p>
    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.
    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