简体   繁体   English

PHP 72的数据库查询速度太慢

[英]PHP 72 too slow on db queries

IMPORTANT: I reported this terrible result of PHP 7.2 at https://bugs.php.net/bug.php?id=76565 . 重要信息:我在https://bugs.php.net/bug.php?id=76565上报告了PHP 7.2的糟糕结果。 I tried this with PHP 7.0.30 and also with PHP 7.1.13 and the problem is exactly the same: much much much slower queries in comparson with PHP 5.4.16. 我在PHP 7.0.30和PHP 7.1.13中都尝试过,问题是完全一样的:与PHP 5.4.16相比,查询要慢得多。

PHP 7.2 is very fast, amazingly fast. PHP 7.2非常快,速度惊人。 Doing very simple benchmarks (like looping thousands of times and doing some basic arithmetics) demonstrates that PHP 7.2 is easily 3x faster than PHP 5.4.16 (which is the default PHP version that comes enabled to be installed on Centos 7 using the "yum"). 进行非常简单的基准测试(例如循环执行数千次并执行一些基本的算术运算)表明,PHP 7.2的速度很容易比PHP 5.4.16(这是默认的PHP版本,可以使用“ yum”安装在Centos 7上)快3倍。 )。

BUT for some strange reason doing DB queries is AT LEAST 2X slower - at least. 但是出于某些奇怪的原因,执行数据库查询至少要慢2倍。

I booted 2 CENTOS servers - identical. 我启动了2个CENTOS服务器-完全相同。 Then on one I installed PHP 5.4.16 and in the other I installed PHP 7.2 (both were running the latest version of MariaDB). 然后在一个上我安装了PHP 5.4.16,在另一个上我安装了PHP 7.2(两个都运行最新版本的MariaDB)。

Then I created a simple database and executed the benchmarking below: 然后,我创建了一个简单的数据库并执行以下基准测试:

<?php

include("connect.php");

$microtime = microtime(true);

for ($i=0;$i<100;$i++) {

    $query = "SELECT * FROM table WHERE id = 'xxx'";
    $result = mysqli_query($connection,$query);
    $data = mysqli_fetch_array($result);

}

echo microtime(true) - $microtime;

?>

I executed this code at least 5 times on each server, allowing some delay between tests. 我在每个服务器上至少执行了5次此代码,从而在测试之间存在一些延迟。 They were very clear: 他们很清楚:

PHP 7.2 took about 4.5 seconds to run PHP 5.4.16 took about 1.1 seconds to run PHP 7.2运行约需4.5秒PHP 5.4.16运行约需1.1秒

So what? 所以呢? Is there something with PHP 7.2 that I am not aware that I should be taking into account when querying db? 查询db时,PHP 7.2是否存在我不应该考虑的问题?

EDIT 编辑

Replying some comments: 回复一些评论:

1) to install PHP 5.4.16 I just did this: 1)安装PHP 5.4.16,我只是这样做的:

yum install php php-mysql mariadb

2) to install PHP 7.2.7 I first installed REMI, then I did this: 2)安装PHP 7.2.7我首先安装了REMI,然后我这样做了:

yum install yum-utils
yum-config-manager --enable remi-php72
yum install php php-mysql mariadb

The connection to the DB is made with this code: 通过以下代码与数据库建立连接:

$connection = mysqli_connect("localhost","root","----");
mysqli_select_db($connection ,"database");

I am aware of a bug regarding using "localhost", but I also tried "127.0.0.1" and the results were the same, so using "localhost" in this case does not change anything. 我知道有关使用“ localhost”的错误,但是我也尝试了“ 127.0.0.1”,结果是相同的,因此在这种情况下使用“ localhost”不会更改任何内容。

I just checked right now, both servers have same CPU (double checked using cat /proc/cpuinfo ) and same SSDs. 我现在刚刚检查了一下,两台服务器都具有相同的CPU(使用cat /proc/cpuinfo双重检查)和相同的SSD。 Ram is the same and RAM consumption is very close when using the command free . 使用free命令时,Ram相同,并且RAM消耗非常接近。

I cannot reproduce such difference. 我无法重现这种差异。

You seems you have some very big table / row, as I have to increase the loop to 10000 to have 1/2 second, on a small RHEL-7.5 VM 您似乎有一些很大的表/行,因为在小型RHEL-7.5 VM上,我必须将循环增加到10000,才能有1/2秒的时间

  • PHP Version 5.4.16 in 0.55" (using php-mysql and libmysqlclient) 0.55“中的PHP 5.4.16版(使用php-mysql和libmysqlclient)
  • PHP Version 5.4.16 in 0.65" (using php-mysqlnd) 0.65的PHP版本5.4.16(使用php-mysqlnd)
  • PHP Version 5.5.38 in 0.66" 0.66中的PHP版本5.5.38“
  • PHP Version 5.6.36 in 0.65" 0.65中的PHP版本5.6.36“
  • PHP Version 7.0.30 in 0.67" PHP版本7.0.30(0.67英寸)
  • PHP Version 7.1.19 in 0.67" PHP版本7.1.19(0.67英寸)
  • PHP Version 7.2.7 in 0.69" PHP版本7.2.7(0.69版)
  • PHP Version 7.3.0alpha2 en 0.55 PHP版本7.3.0alpha2 zh 0.55

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

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