简体   繁体   中英

Same mariaDB inserts much more slower in PHP 7.4 than PHP 7.1 with FAT FREE

I'm trying to migrate a legacy PHP/Fat Free project from php 7.1 to 7.4 and I found that some queries take too long (like 10x more time) to finish. Particularly some inserts. I'm running the same project in my localhost with xampp (7.1.32 and 7.4.6) and using the exact same MariaDB server (v10.4.8) with the exact same database always.

The code is something like that:

foreach($ridiculouslyLongArray as $row) //I'm talking about some millons of rows
  $this->db->exec("INSERT INTO a_table (field1, field2, fieldn) VALUES ('".$row['field1']."', '".$row['field2']."', '".$row['fieldn']."')"); 

//Yes, it's open to sql injection, I will fix that too

The definition of $this->db is the next:

$this->db = new DB\SQL('mysql:host=localhost;port=3306;dbname=something', 'dbuser', 'dbpassword', array(\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION));

and is a wrapper of PDO as far as I know.

I've tried to change the code to insert multiple rows per query but the query still taking much more time than in php 7.1.

This is my setup

->Original Project (in which the queries run fine)

  • PHP 7.1.32 (memory limit 2048mb)
  • Fat Free 3.6.4
  • MariaDB 10.4.8

->New Project (in which the queries run slow)

  • PHP 7.4.6 (memory limit 2048mb)
  • Fat Free 3.7.2
  • MariaDB 10.4.8 (same server and db that in the previous one)

Thanks for your time.

EDIT: I Just noticed that the PDO Drivers for MySQL are different between versions

for PHP 7.1:

  • mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $

for PHP 7.4:

  • mysqlnd 7.4.6

Edit 2: The query is in a transaction and it is using the same indexes and same dB engine because is the same insert over the same table in the same database on the same server. Nothing change in the code only the PHP versión.

This wasn't explicitly mentioned in the comments, but something else that may be causing some slowness is query logging.

By default, Fat-Free will log all DB queries. If you are running a gazillion inserts, all those inserts are being logged. If it's not already, I would recommend in production to disable query logging. Wherever your bootstrap/services file is that creates the db connection, I would add this after it:

$f3->set('db', new DB\SQL(/* config stuff */));
if(ENVIRONMENT === 'PRODUCTION') { // or whatever you use to signal it's production
    $f3->db->log(false);
}

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