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

查看:170
本文介绍了如何自动杀死缓慢的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天全站免登陆