I’m using PHP and mongodb. I’ve turned on system profiling on everything above 20ms, using mongodb 2.0.4. When I load a simple page with every little query, it records in the system profile the same query like 4, 10, sometimes 24 times! And the pages load very slow because of that! Why is that happening??
The query can be as simple as:
$c2 = $things->find();
foreach($c2 as $doc) {
// some code...
}
Thanks!!
EDIT: Full code:
<?php
$m = new Mongo();
$db = $m->selectDB("test");
function getAllData ($db) {
$some = $db->some;
$cursor = $some->find();
$js = "function() {
return ((";
foreach ($cursor as $doc) {
$js .= "this.idE == '" . $doc['_id'] . "' || ";
}
$js = substr($js,0,-4);
$js .= ")";
$js .= ");}";
$names = $db->names;
$c2 = $names->find(array('$where' => $js));
foreach($c2 as $doc) {
echo $doc['name'];
}
}
getAllData ($db);
?>
Which of the two queries is getting multiplied?
I would start modifying the code and see what happens. The first thing that I would do is remove the “foreach” and instead just do one “getNext” and see if this affects the number of times the query is called. My best guess is that it has something to do with the way the Mongo driver fetches results in batches. When you do a query, it doesn’t download all the rows at once, it downloads them in batches. You may be seeing these batch fetches as duplicate queries. I think that there is a mechanism by which you can adjust the batch size, which may help. Also, updating to the latest drivers may help.