Note that there are some explanatory texts on larger screens.

plurals
  1. POExtremely slow MongoCursor::next() performance
    primarykey
    data
    text
    <p>I was faced with an extremely slow <code>MongoCursor</code> performance under some conditions.</p> <pre class="lang-php prettyprint-override"><code>$mongoClient = new MongoClient('mongodb://127.0.0.1:27017'); $collection = $mc-&gt;mydb-&gt;mycollection; </code></pre> <h3>test1:</h3> <pre class="lang-php prettyprint-override"><code>for($i=0; $i&lt;1000; $i++) { $cursor = $collection-&gt;find()-&gt;limit(2); $cursor-&gt;next(); } </code></pre> <p><em>Elapsed time: 41.78210 sec [<strong>24 ops/s</strong>]</em></p> <h3>test2:</h3> <pre class="lang-php prettyprint-override"><code>for($i=0; $i&lt;1000; $i++) { $cursor = $collection-&gt;find()-&gt;limit(1); $cursor-&gt;next(); } </code></pre> <p><em>Elapsed time: 0.47898 sec [<strong>2 088 ops/s</strong>]</em></p> <h3>test3:</h3> <pre class="lang-php prettyprint-override"><code>for($i=0; $i&lt;1000; $i++) { $cursor = $collection-&gt;find()-&gt;limit(2)-&gt;batchSize(-2); $cursor-&gt;next(); } </code></pre> <p><em>Elapsed time: 0.65439 sec [<strong>1 528 op/s</strong>]</em></p> <p>In <strong>test1</strong> no matter whether <code>limit(2)</code> or <code>limit(200)</code> script does 24 ops/s always;</p> <p>I tested it with:</p> <ul> <li><p>mongo v2.4.5</p></li> <li><p>php driver 1.4.0dev and 1.5.0dev</p></li> <li><p>Debian 6.0.7 and Ubuntu 12.10</p></li> </ul> <p>Is this some issue in php driver?</p> <h2>EDIT1:</h2> <p>I did some logging via MongoLog and got interesting results:</p> <pre><code>MongoLog::setLevel(MongoLog::ALL); MongoLog::setModule(MongoLog::IO); </code></pre> <p><strong>test1:</strong> (limit(N) where N > 1)</p> <ul> <li>1375693731.5349 - IO (FINE): getting reply</li> <li>1375693731.5350 - IO (FINE): getting cursor header</li> <li>1375693731.5351 - IO (FINE): getting cursor body</li> <li>1375693731.5351 - IO (WARN): Killing unfinished cursor 63182808988603511</li> <li>1375693731.5352 - IO (FINE): getting reply</li> <li><strong>1375693731.5353 - IO (FINE): getting cursor header</strong></li> <li><strong>1375693731.5726 - IO (FINE): getting cursor body</strong></li> <li>1375693731.5727 - IO (WARN): Killing unfinished cursor 63182981334616260</li> <li>...</li> </ul> <p><strong>test2:</strong> (limit(1), limit(0) and without limit )</p> <ul> <li>1375693866.2663 - IO (FINE): getting reply</li> <li>1375693866.2663 - IO (FINE): getting cursor header</li> <li>1375693866.2663 - IO (FINE): getting cursor body</li> <li>1375693866.2665 - IO (FINE): getting reply</li> <li>1375693866.2665 - IO (FINE): getting cursor header</li> <li>1375693866.2665 - IO (FINE): getting cursor body</li> <li>...</li> </ul> <p>as you can see when I limit query with N (where N>1) driver does <code>killing unfinished cursor</code> and next time when it tries to get cursor body it spends to much time.</p> <h2>EDIT2:</h2> <p>Yes, that timings concern collection with 20 documents only, but 20K documents will affect only limit(0) and noLimit, they will do kill cursor as you said.</p> <p>Here is the log with logLevel:5. I don't any clues.</p> <p><strong>mongodb.log:</strong> limit(N)</p> <ul> <li>Mon Aug 5 14:43:55.292 [conn3] query mydb.mycollection cursorid:1002459676459826 ntoreturn:2 ntoskip:0 nscanned:3 keyUpdates:0 locks(micros) r:84 nreturned:2 reslen:86 0ms</li> <li>Mon Aug 5 14:43:55.293 [conn3] killcursors: found 1 of 1</li> <li>Mon Aug 5 14:43:55.<strong>293</strong> [conn3] killcursors keyUpdates:0 0ms</li> <li>Mon Aug 5 14:43:55.<strong>332</strong> [conn3] runQuery called mydb.mycollection {}</li> <li>Mon Aug 5 14:43:55.333 [conn3] query mydb.mycollection cursorid:1002631131988423 ntoreturn:2 ntoskip:0 nscanned:3 keyUpdates:0 locks(micros) r:84 nreturned:2 reslen:86 0ms</li> </ul> <p><strong>mongodb.log:</strong> limit(1)</p> <ul> <li>Mon Aug 5 14:45:54.899 [conn3] runQuery called mydb.mycollection {}</li> <li>Mon Aug 5 14:45:54.899 [conn3] query mydb.mycollection ntoreturn:1 ntoskip:0 nscanned:1 keyUpdates:0 locks(micros) r:39 nreturned:1 reslen:53 0ms</li> <li>Mon Aug 5 14:45:54.899 [conn3] runQuery called mydb.mycollection {}</li> <li>Mon Aug 5 14:45:54.899 [conn3] query mydb.mycollection ntoreturn:1 ntoskip:0 nscanned:1 keyUpdates:0 locks(micros) r:35 nreturned:1 reslen:53 0ms</li> </ul>
    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