简体   繁体   中英

mysqlnd noticeable slower than libmysql using PDO

I'm just switching to mysqlnd to be able to use http://php.net/manual/en/book.mysqlnd-ms.php but I see some serious performance degradation compared to libmysql.

NewRewlic shows more 100ms spend in PHP after switching to mysqlnd https://db.tt/68r9RfhJ

I've created a benchmark to reproduce the problem, it's quite simple:

$dbh = new PDO("mysql:host=$host; dbname=$dbname;", $user, $pass);

for ($i = 0; $i < 1000; $i++) {
    $sth = $dbh->prepare("SELECT * FROM Orders LIMIT 100");
    $sth->execute();

    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
}

Execution time:

  • libmysql - ~2 seconds
  • mysqlnd - ~3 seconds (50% more)

Version information:

  • PHP Version: PHP 5.5.9-1ubuntu4.9
  • Client API version => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $
  • Client API library version => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $

Can anybody explain me what's the reason behind it and how to solve it?

  1. Slowness occurs for PDO mainly.
  2. mysqli doesn't seem to be that much affected.
  3. I cannot switch to mysqli due to usage of Doctrine.
  4. Only way to catch up libmysql speed is to disable buffering from queries for MySQL ( setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false) ) which I cannot do easily on production because of character of change and possible side-effects.

mysqlnd isn't slower. It's just more efficient on memory. Instead of fetching your results from mysql by copying all of the data over, PHP becomes the same process that talks to mysql and stores the data in memory. So you're ultimately using half the memory, but the added computational cost might be in having to allocate that memory within the PHP process. You likely are just seeing that shift from mysql to PHP in newrelic. I highly doubt you have anything to be concerned about in terms of performance here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM