【问题标题】:How do I display execution time of a MySQL query in PHP?如何在 PHP 中显示 MySQL 查询的执行时间?
【发布时间】:2014-06-16 02:18:54
【问题描述】:

我正在开发一个在文本框中接受查询并返回分页结果的 PHP 应用程序。作为应用程序的一部分,我想报告查询的运行时间。

这是我到目前为止所做的。

我首先通过直接在文本框中输入并运行脚本来启用分析:

set global profiling = 1

使用提供的文本框输入以下查询:

select @@profiling

得到:

1

最后,我这样运行查询:

select * from log

但是,当我运行命令来分析查询时:

show profiles

我没有收到任何结果,页面上也没有显示任何内容。

由于我在“显示配置文件”命令后没有看到任何表格,这是否意味着没有足够的权限或者我错过了另一个步骤?

我遵循以下程序:

Measuring actual MySQL query time

请指教。

我的PHP代码如下:

<?php
    if($_POST)
    {
        $db = new PDO('mysql:host=localhost;dbname=<dbname>;charset=utf8', 'user', 'pass');
        $stmt = $db->prepare($_POST['query']);
        $stmt->execute();

        $records = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $errmsg = $stmt->errorInfo()[2]; //Output the error message - Index 2 of the array

        echo $errmsg;
    }  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Log</title>
</head>
<body>
    <form method="post" id="queryform">
        <div class="label">
            <span class="label">Enter SQL Query</span>
        </div>
        <div class="input">
            <input type="text" name="query" size="150" value="<?=$_POST['query']?>" />
        </div>
    </form>
    <? if (isset($records)): ?>
    <table border="1">
        <? foreach($records as $record): ?>
            <tr>
                <? foreach($record as $colname => $value): ?>
                    <td>
                       <?=$value;?>
                    </td>
                <? endforeach; ?>    
            </tr>
        <? endforeach; ?>
    </table>

    <? endif; ?>
</body>
</html>

任何帮助将不胜感激。

【问题讨论】:

    标签: php mysql pdo query-optimization


    【解决方案1】:

    这就像一个魅力!

        $db->query('set profiling=1'); //optional if profiling is already enabled
        $db->query($_POST['query']);
        $stmt = $db->query('show profiles');
        $db->query('set profiling=0'); //optional as well
    
        $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
        $errmsg = $stmt->errorInfo()[2]; //Output the error message 
    

    更新(以下现在适用于我目前设置的 innodb)

    $db->query('set profiling=1'); //optional if profiling is already enabled
    $db->query($_POST['query']);
    $res = $db->query('show profiles');
    $records = $res->fetchAll(PDO::FETCH_ASSOC);
    $duration = $records[0]['Duration'];  // get the first record [0] and the Duration column ['Duration'] from the first record
    

    来自 phpmyadmin 的(显示配置文件)结果。

    Query_ID    Duration    Query   
    1           0.00010575  SELECT DATABASE()
    

    Getting the actual (absolute) execution time of the last query in PHP (excluding network latency etc)

    【讨论】:

      【解决方案2】:

      这个怎么样:

      $start = microtime(true);
      $stmt->execute();
      $end = microtime(true);
      echo "This query took " . ($end - $start) . " seconds.";
      

      【讨论】:

      • 感谢您的回复。这是唯一可行的方法吗?我采用这种方法并没有想到它可能是唯一可行的解​​决方案。感谢您的帮助。
      • 正确的消息应该是"This query took " . "($end - $start) . " seconds."
      • @MarcioMazzucato 你是对的,microtime(true) 在几秒钟内返回一个浮点数。我相应地编辑了答案。
      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2011-10-08
      • 2011-05-28
      • 2014-09-21
      相关资源
      最近更新 更多