PHP y MongoDB ridículamente lentos (15 segundos)

I setup Mongo and everything works great. When I use MongoVUE or the shell, everything is fast as lightning.

After installing the PHP Mongo drivers for Windows, though, every query takes upwards of 15 seconds. Even this simple tutorial from PHP.net takes 15 seconds.

Tutorial example:

    $time_start = microtime(true);
    // connect
    $m = new Mongo();

    // select a database
    $db = $m->comedy;

    // select a collection (analogous to a relational database's table)
    $collection = $db->cartoons;

    // add a record
    $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
    $collection->insert($obj);

    // add another record, with a different "shape"
    $obj = array( "title" => "XKCD", "online" => true );
    $collection->insert($obj);

    // find everything in the collection
    $cursor = $collection->find();

    // iterate through the results
    foreach ($cursor as $obj) {
        echo $obj["title"] . "\n";
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;

    echo "\nOperation took $time seconds\n";

Salida:

Calvin and Hobbes XKCD Operation took 15.000731945038 seconds

I'm using Windows 7, WAMP, MongoDB 2.0.7 and the Windows PHP Mongo drivers (v1.1.12) de Github.

In answer to the question in the comments:

Q: Have you tested other PHP code on your system and the duration of it?
A: This is actually a small piece of a bigger web application and everything else works fine. I'd be happy to post any benchmarks you guys think would be useful.

Q: Have you profiled the query in Mongo?
A: Yes. If I run it through the shell, it gets a response instantaneously.

Q: What WAMP version is this?
A: WampServer 2.2

Q: What PHP version is this?
A: 5.3.13

Q: Has the mongodb already got data in it?
A: I dropped all my databases prior to the benchmark I posted, so all it has in it is the data being inserted by the code below.

Q: What is the specs of the computer (memory mainly)?
A: AMD Phenom II X4 840T 2.9GHz processor, 4GB DDR3 RAM, 64 bit OS

preguntado el 28 de agosto de 12 a las 14:08

2 Respuestas

I agree 15 seconds is ridiculous. The tutorial code should be running closer to 0.5 seconds (if that) even with SafeMode on. You have SafeMode turned off as you can see by your insertion code:

// add a record
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj);

// add another record, with a different "shape"
$obj = array( "title" => "XKCD", "online" => true );
$collection->insert($obj);

You are doing async inserts which means that the PHP code does not wait for a return from the DB, so in theory the call to the DB should not be the problem in your code.

Programas de find() could be causing problems though, so I would profile that.

It is still possible that the driver is causing some kind of slow down but this is highly unlikely. To help us further:

  • Have you tested other PHP code on your system and the duration of it?
  • Have you profiled the query in Mongo?
  • What WAMP version is this?
  • What PHP version is this?
  • Has the mongod already got data in it?
  • What is the specs of the computer (memory mainly)?

respondido 28 mar '18, 07:03

I edited the question to answer your clarification questions. - VPel

@VPel Hmm I wonder if something might be up with the dirver, can you try downgrading to 1.2.10? I know for sure that one works well. - sammaye

@VPel I would go to mongodb-user about this, let them know about it :) - sammaye

Insertion into a Un nuevo database will take a while since MongoDB needs to pre-allocated the database files first. Subsequent inserts will be fast. Apart from that: Performing a benchmark with exactly . insert is pointless.

Respondido el 01 de Septiembre de 12 a las 03:09

@esealPsnoroMoN It takes the same amount of time everytime you run this (the DB is created on the first run). Also, a query on an existing database that I created takes 15+ seconds as well. I felt it would add unneeded complexity to post that schema and I tried to boil it down to the easiest possible scenario for help. - VPel

PS @esealPsnoroMoN I'd think asking for clarification would be a more grown up way to handle a response. No need to be condescending. - VPel

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.