简体   繁体   English

相同的 mariaDB 在 PHP 7.4 中插入比在 FAT FREE 的 PHP 7.1 中慢得多

[英]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.我正在尝试将旧的 PHP/Fat Free 项目从 php 7.1 迁移到 7.4,但我发现某些查询需要很长时间(比如 10 倍多的时间)才能完成。 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.我在本地主机中使用 xampp(7.1.32 和 7.4.6)运行相同的项目,并始终使用完全相同的 MariaDB 服务器(v10.4.8)和完全相同的数据库。

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 //是的,它对sql注入开放,我也会修复它

The definition of $this->db is the next: $this->db 的定义如下:

$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.据我所知,它是 PDO 的包装器。

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.我试图更改代码以在每个查询中插入多行,但查询仍然比在 php 7.1 中花费更多的时间。

This is my setup这是我的设置

->Original Project (in which the queries run fine) -> 原始项目(其中查询运行良好)

  • PHP 7.1.32 (memory limit 2048mb) PHP 7.1.32(内存限制 2048mb)
  • Fat Free 3.6.4无脂肪 3.6.4
  • MariaDB 10.4.8玛丽亚数据库 10.4.8

->New Project (in which the queries run slow) -> 新建项目(查询运行缓慢)

  • PHP 7.4.6 (memory limit 2048mb) PHP 7.4.6(内存限制 2048mb)
  • Fat Free 3.7.2无脂肪 3.7.2
  • MariaDB 10.4.8 (same server and db that in the previous one) MariaDB 10.4.8(与上一个相同的服务器和数据库)

Thanks for your time.谢谢你的时间。

EDIT: I Just noticed that the PDO Drivers for MySQL are different between versions编辑:我刚刚注意到 MySQL 的 PDO 驱动程序在版本之间是不同的

for PHP 7.1:对于 PHP 7.1:

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

for PHP 7.4:对于 PHP 7.4:

  • mysqlnd 7.4.6 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.编辑 2:查询在一个事务中,它使用相同的索引和相同的 dB 引擎,因为在同一台服务器上的同一数据库中的同一表上进行了相同的插入。 Nothing change in the code only the PHP versión.代码中没有任何变化,只有 PHP 版本。

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.默认情况下,Fat-Free 将记录所有数据库查询。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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