当 MySQL 查询耗时超过 X 秒时,PHP PDO 执行以中止? [英] PHP PDO execute to abort when MySQL query taking more than X seconds?

查看:61
本文介绍了当 MySQL 查询耗时超过 X 秒时,PHP PDO 执行以中止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP PDO 或 MySQL 中是否有一个功能可以在执行超过 2 秒时自动中止并返回空结果集?

Is there a feature in PHP PDO or in MySQL that automatically aborts and returns with an empty result set when it's executing more than, say, 2 seconds?

进行了一些搜索,没有找到任何有用的信息.我认为这对用户体验来说可能会很棒,尤其是对于不那么重要的微不足道的统计数据.

Did some searches and nothing helpful was found. I think this would probably be great for user experience, especially with trivial stats data that are not so important.

尝试在 PHP 中想出一种方法来做到这一点,但不确定如何在 PDO:execute() 尚未返回时监控它的执行时间.

Tried to come up with a way in PHP to do this but not sure how to monitor the execution time of PDO:execute() when it's not returned yet.

有什么想法吗?

推荐答案

你可以这样做我的设置 PDO::ATTR_TIMEOUT,像这样:

You can do this my setting PDO::ATTR_TIMEOUT, like this:

$db = new PDO(
    "mysql:host=$host;dbname=$dbname", 
    $username, 
    $password,
    array(
      PDO::ATTR_TIMEOUT => 10
    )
  );

这会导致 10 秒后超时.

This is cause a timeout after 10 seconds.

这篇关于当 MySQL 查询耗时超过 X 秒时,PHP PDO 执行以中止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆