A
A
atis //2016-05-27 09:43:37
PHP
atis //, 2016-05-27 09:43:37

Why is MongoDB fetching documents very slowly?

Hello.
Such a thing. Mongo executes the query very quickly, but foreach traversal of the data takes a very long time. I measure time with a function microtime()
The result of the output of the function

microtime(true)
// 1464330248.2163
$MongoCollection = (new \MongoClient())->selectCollection('db', 'coll');
$MongoCursor     = $MongoCollection->find($where, $fields)->sort($sort)->limit(50)->skip($this->offset);
// 1464330248.2165

foreach($MongoCursor as $doc) {

}

// 1464330253.6667

As you can see in the example, the cycle lasts as much as three seconds. This is even if nothing happens in the loop.
I'm using MongoDB v3.2.6

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2016-05-27
@atis2345

Mongo executes the query very quickly, but foreach traversal of the data takes a very long time.

1) The request is executed at the moment when you started to drag data, and the find command simply prepares the request.
2) The query is slow because you do not have the required index, and the monga has nothing to do with it, any database without indexes slows down, because all data is scanned and sorted at the time of the request.

S
spotifi, 2016-05-27
@spotifi

Well, for starters, look at
https://docs.mongodb.com/manual/reference/method/d...
Secondly - why do you need cursors? They have always been a brake point in all DBMS compared to direct queries.
Their use is justified in case of tricky-complex processing on the server - which is not observed in this case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question