MySQL - 我可以限制允许查询运行的最长时间吗? [英] MySQL - can I limit the maximum time allowed for a query to run?

查看:48
本文介绍了MySQL - 我可以限制允许查询运行的最长时间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来限制 mysql 服务器上查询的最大运行时间.我认为这可以通过 my.cnf 配置文件完成,但在文档中找不到任何相关内容.有谁知道这是否可以做到?谢谢.

I'm looking for a way to limit the max running time of a query on mysql server. I figured this could be done through the my.cnf configuration file, but couldn't find anything relevant in the docs. Anyone knows if this could be done? thanks.

推荐答案

更新

从 MySQL 5.7 开始,您可以包含 MAX_EXECUTION_TIME 优化器提示在 SELECT 查询中指示服务器在指定时间后终止它.

Update

As of MySQL 5.7, you can include a MAX_EXECUTION_TIME optimizer hint in your SELECT queries to instruct the server to terminate it after the specified time.

据我所知,如果您想强制执行服务器范围的超时,或者您关心除 SELECT 之外的查询,那么原始答案仍然是您唯一的选择.

As far as I know, if you want to enforce a server-wide timeout, or if you care about queries besides SELECTs, the original answer is still your only option.

在向服务器发送查询以运行时,无法指定最长运行时间.

There is no way to specify a maximum run time when sending a query to the server to run.

但是,在您的数据库服务器上每秒运行一次 cron 作业并进行连接并执行以下操作的情况并不少见:

However, it is not uncommon to have a cron job that runs every second on your database server, connecting and doing something like this:

  1. 显示处理程序
  2. 查找查询时间大于所需最大时间的所有连接
  3. 为每个进程运行 KILL [进程 ID]

这篇关于MySQL - 我可以限制允许查询运行的最长时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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