Note that there are some explanatory texts on larger screens.

plurals
  1. POmongodb 'count' with query is very slow
    primarykey
    data
    text
    <p>everyone,im use a mongodb 2.4.6 version and in windows 2008 64-bit.</p> <p>i have a collection that have two million records and need to search and paging in client.</p> <pre><code>db.products.find({"catalogs":1205}).skip().limit() is very fast . </code></pre> <p>but when calculate total record count:</p> <pre><code>db.products.find({"catalogs":1205},{"_id":1}).count() is too slow. &gt;&gt; 442312 records. &gt;&gt;[log] Sat Sep 28 00:20:01.566 [conn10] command products.$cmd command: { count: "products", query: { catalogs: 1205.0 }, fields: { _id: 1.0 } } ntoreturn:1 keyUpdates:0 locks(micros) r:460681 reslen:48 460ms </code></pre> <p>this count command elapsed time is <code>460ms</code>,is too slow.if we have a lot of request that very terrible.</p> <p>i created a index for a 'catalogs' field and can't use <code>$inc</code> command because query could be very complex.</p> <p>im googling some like this problem and found this 'count' performance bug already fixed in mongodb 2.4 version.</p> <p>from <a href="http://docs.mongodb.org/manual/release-notes/2.4-overview/">http://docs.mongodb.org/manual/release-notes/2.4-overview/</a> </p> <pre><code>Improvements to count provide dramatically faster count operations. Counting is now up to 20 times faster for low cardinality index based counts. </code></pre> <p>what ways can improve count?thanks.</p> <p><strong>update some information</strong></p> <pre><code>&gt; db.products.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "products.products", "name" : "_id_" }, { "v" : 1, "key" : { "catalogs" : 1, "created" : -1 }, "ns" : "products.products", "name" : "catalogs_1_created_-1" } ] </code></pre> <p>the query and elapsed time:</p> <pre><code>&gt;db.products.find({"catalogs":1205},{"_id":1}).limit(20) &gt;&gt;Tue Oct 01 15:39:19.160 [conn2] query products.products query: { catalogs: 1205.0 } cursorid:277334670708253 ntoreturn:20 ntoskip:0 nscanned:21 keyUpdates:0 locks(micros) W:5045 r:1017 nreturned:20 reslen:704 1ms </code></pre> <p>the query exaplin:</p> <pre><code>&gt;db.products.find({"catalogs":1205},{"_id":1}).explain() { "cursor" : "BtreeCursor catalogs_1_created_-1", "isMultiKey" : true, "n" : 451466, "nscannedObjects" : 451466, "nscanned" : 451466, "nscannedObjectsAllPlans" : 451466, "nscannedAllPlans" : 451466, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 2, "nChunkSkips" : 0, "millis" : 2969, "indexBounds" : { "catalogs" : [ [ 1205, 1205 ] ], "created" : [ [ { "$maxElement" : 1 }, { "$minElement" : 1 } ] ] }, "server" : "WIN-O47CO6C2WXY:27017" </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    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.
    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