VPel VPel - 3 months ago 14
PHP Question

PHP and MongoDB Rediculously Slow (15 seconds)

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";


Output:

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) from 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

Answer

I agree 15 seconds is rediculous. The tutorial code should be running closer to 0.5 seconds (if that) even with safe mode on. Your have safe mode 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 dong 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.

The 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)?
Comments