phpMyAdmin 执行查询的速度比普通 PHP 脚本快

phpMyAdmin executes queries faster than normal PHP script

我有一个SQL查询:

SELECT country_code FROM GeoIP WHERE 3111478102>=ip_from AND 3111478102<=ip_to;

我试图在 phpMyAdmin 和普通的 php 脚本中执行相同的查询,结果如下:

phpMyAdmin

PHP 脚本

如您所见,在 phpMyAdmin 中完全执行查询仅需 0.8 秒。而在正常 PHP 脚本中需要 4.5 秒!

php脚本代码:

<?php
    // ob_start();
    // error_reporting(0);
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');

    header_remove("X-Powered-By");
    ini_set('session.gc_maxlifetime', 13824000);
    session_set_cookie_params(13824000);
    session_start();
    include_once './config.php';
    // include_once './functions/mainClass.php';

    $beforeMicro = microtime(true);
    $res = $conn->query("SELECT country_code FROM GeoIP WHERE 3111478102>=ip_from AND 3111478102<=ip_to");
    if ($res->rowCount() > 0){
        $sen = $res->fetch(PDO::FETCH_OBJ);
        $country_code = $sen->country_code;
        print_r([$country_code]);
    }
    $afterMicro = microtime(true);
    echo 'time:'.round(($afterMicro-$beforeMicro)*1000);
?>

与此同时,我正在设置一个新的网络服务器并使用以下内容:

CentOS 8
Apache 2.4.46
PHP 7.2.24

请注意:我在 (GeoIP) 中搜索的 table 包含超过 39 个 minion 记录。但我认为这不是问题,因为相同的查询在同一台服务器中 运行 更快。我还尝试将相同的数据库和 PHP 脚本上传到另一个共享主机帐户(不是我自己的服务器),并且它有效并在 1.6 秒内从 php 脚本执行查询。

这可能对您的问题有帮助:

Front-end tools like phpMyAdmin often staple on a LIMIT clause in order to paginate results and not crash your browser or app on large tables. A query that might return millions of records, and in so doing take a lot of time, will run faster if more constrained.

It's not really fair to compare a limited query versus a complete one, the retrieval time is going to be significantly different. Check that both tools are fetching all records.