This code takes ~0.1s
// find
$benchmark = Profiler::start ('Testing', 'find()');
$cursor = MongoBG::getInstance ( )->setDatabase ('test')->setCollection ('testcoll')->find();
Profiler::stop ($benchmark);
$benchmark = Profiler::start ('Testing', 'cursor walk');
while ($cursor->hasNext()) {
print_r开发者_如何学Python($cursor->getNext());
}
Profiler::stop ($benchmark);
so "find()" took only 0.000017 seconds but "cursor walk" 0.102812 seconds
Collection is about 100 rows, speed remains the same with 1000 or only 10 items in it.
Some server info: FreeBSD 8.1, PHP 5.3.5 with (mongo/1.1.4), MongoDB version 1.6.6-pre
With such a quick time, it sounds like find
didn't do anything but prepare an object (no communication with the database), and it's only upon using the cursor the actual query was executed and results were read. The cursor is doing the work which is why it's slower.
I know that's how the mongodb drivers for node.js worked. If you looked at it like that, the cursor speed isn't bad for opening a connection, authenticating, sending the query, receiving and buffering the response, then parsing/loading it into an object to return to you.
精彩评论