php - 如何在 PHP 中显示 MySQL 查询的执行时间?

标签 php mysql pdo query-optimization

我正在开发一个 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>

如有任何帮助,我们将不胜感激。

最佳答案

这就像一个魅力!

    $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)

关于php - 如何在 PHP 中显示 MySQL 查询的执行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23376613/

相关文章:

php - 如何检查类中未使用的变量/函数

javascript - Ajax post 带有参数数组,然后在 PHP 中引用

c# - 更新MySql数据库表中的数据

php - PDO fatal error : Call to a member function prepare() on a non-object

MySQL 错误 - SQLSTATE [42000] : Syntax error or access violation: 1064

php - 当两者都被触发时,正则表达式多重前瞻不起作用

php - 在 PHP 中解码和访问 json 字典数组

php - MySQL不会从配置文件中读取密码

php - 从 MySql 表中删除重复行

php - 在 PHP 中使用 PDO 绑定(bind) decimal/double/float 值的最佳方法是什么?