MySQL连接速度相对较低

时间:2018-08-05 07:58:32

标签: php mysql apache2

我有两个服务器。

1) (old) intel i7-3770 3.4Ghz,DDR3 32gb RAM, 3TB HDD, UBUNTU 16.04, PHP 7.0
2) (new) intel i7-7700 3.6Ghz,DDR4 64gb RAM, 512Gb SSD, UBUNTU 18.04 PHP 7.2

在“新”服务器上,我运行简单的php脚本:

<?PHP
$start_time = microtime(1);
$DB_HOST    = "localhost";
$DB_USER    = "wdr" ;
$DB_PASS    = "twnlccd087qXrB8e" ;
$DB_NAME    = "wdr_new" ;

for($i=0;$i<50000;$i++)
    $r=1;

echo 'befor db connect = ',microtime(1)-$start_time,"<br>\n";

$connection = mysqli_connect($DB_HOST,$DB_USER,$DB_PASS) or die("mysqli - cant connect");
echo 'after db connect = ',microtime(1)-$start_time,"<br>\n";

mysqli_select_db($connection,$DB_NAME);
echo 'after db select  = ',microtime(1)-$start_time,"<br>\n";

$query = 'SELECT m.id, m.title, m.last_change, t.name FROM main m, type t WHERE t.id=m.type_id and m.enabled=1 and m.id=23';
mysqli_query($connection, $query);
echo 'after some query= ',microtime(1)-$start_time,"<br>\n";
?>

如果我通过cli运行它

php connection_test.php

我有下一个结果

befor db connect = 0.0003819465637207<br>
after db connect = 0.00086498260498047<br>
after db select  = 0.00094509124755859<br>
after some query = 0.0012741088867188<br>

如果我通过

浏览器-> apache-> php运行此脚本
http://myhost/connection_test.php

我有不同的结果

befor db connect = 0.0014159679412842
after db connect = 0.0023338794708252
after db select  = 0.0025839805603027
after some query = 0.0032298564910889

这比从命令行运行要慢三倍。

在旧服务器上,运行位置没有区别,它始终运行很快,并且执行时间相同。但是旧服务器首先在ubuntu 14.04上启动,然后更新到15.04,然后是16.04。

我可以做什么来提高速度?

“浏览器-> apache-> php-> mysql”的瓶颈在哪里?

我怎么知道为什么这么慢?

0 个答案:

没有答案