如何自动杀死缓慢的 MongoDB 查询? [英] How to automatically kill slow MongoDB queries?

查看:25
本文介绍了如何自动杀死缓慢的 MongoDB 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以保护我的应用免受 MongoDB 中慢速查询的影响?我的应用程序有很多过滤器的可能性,我正在监视所有这些查询,但同时我不想因为缺少索引定义而影响性能.

Is there a way that I can protect my app against slow queries in MongoDB? My application has tons of possibilities of filters and I'm monitoring all these queries but at the same time I don't want to compromise performance because of a missing index definition.

推荐答案

现在使用版本 2.6 这是可能的.在他们的 按发布可以看到如下:

Right now with version 2.6 this is possible. In their press release you can see the following:

使用 MaxTimeMS 运算符和开发人员可以指定自动取消查询,提供更好的资源利用率控制;

with MaxTimeMS operators and developers can specify auto-cancellation of queries, providing better control of resource utilization;

因此,使用 MaxTimeMS 您可以指定允许的时间您要执行的查询.例如,我不希望特定查询运行超过 200 毫秒.

Therefore with MaxTimeMS you can specify how much time you allow your query to be executed. For example I do not want a specific query to run more than 200 ms.

db.collection.find({
  // my query
}).maxTimeMS(200)

这很酷,您可以为不同的操作指定不同的超时时间.

What is cool about this, is that you can specify different timeouts for different operations.

在评论中回答 OP 的问题.没有针对此的全局设置.一个原因是不同的查询可以有不同的最大容忍时间.例如,您可以通过 ID 查找 userInfo 的查询.这是非常常见的操作,应该运行得非常快(否则我们做错了什么).所以我们不能容忍它运行超过 200 毫秒.

To answer OP's question in the comment. There is not global setting for this. One reason is that different queries can have different maximum tolerating time. For example you can have query that finds userInfo by it's ID. This is very common operation and should run super fast (otherwise we are doing something wrong). So we can not tolerate it to run longer than 200 ms.

但我们也有一些聚合查询,我们每天运行一次.对于此操作,运行 4 秒即可.但我们不能容忍它超过 10 秒.所以我们可以把 10000 作为 maxTimeMS.

But we also have some aggregation query, which we run once a day. For this operation it is ok to run for 4 seconds. But we can not tolerate it longer than 10 seconds. So we can put 10000 as maxTimeMS.

这篇关于如何自动杀死缓慢的 MongoDB 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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