mongodb 在mongodb中模拟慢查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12908425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:57:57  来源:igfitidea点击:

Simulate slow query in mongodb?

mongodb

提问by Oleg Mikheev

Does MongoDb have anything like MySql's SELECT SLEEP(5);?

MongoDb 有没有类似 MySql 的东西SELECT SLEEP(5);

I can see some internal sleep function that would pause the whole server, but I need to pause just the current query.

我可以看到一些内部睡眠功能会暂停整个服务器,但我只需要暂停当前查询。

Disclaimer: just for testing purposes

免责声明:仅用于测试目的

采纳答案by code_monkey_steve

You can use the $where operatorto call sleep(). This should work in any language or ORM/ODM. For example, in Mongoid you could do:

您可以使用$where 运算符来调用 sleep()。这应该适用于任何语言或 ORM/ODM。例如,在 Mongoid 你可以这样做:

Model.where( :$where => "sleep(100) || true" ).count

Tune the sleep value for the number of documents in the collection (it will delay on each one). This will do fairly horrible things to the DB server, so only use it for testing, and never (ever!) on a production server.

调整集合中文档数量的 sleep 值(它会延迟每个文档)。这将对数据库服务器造成相当可怕的影响,因此仅将其用于测试,而永远不要(永远!)在生产服务器上使用它。

回答by RubenCaro

You can use the $whereoperator as code_monkey_stevesays, but be sure you do with mongo version >= 2.4. Before that version no javascript can be run on parallel on the same server. The sleepcommand is javascript, so it would apparently block other javascript queries you could be making.

您可以使用code_monkey_steve所说的$where运算符,但请确保使用mongo version >= 2.4。在该版本之前,没有 javascript 可以在同一台服务器上并行运行。该命令是 javascript,因此它显然会阻止您可能进行的其他 javascript 查询。sleep

回答by Stennie

From the mongoshell you can do sleep( ms ), eg sleep for 5 seconds before running a query:

mongoshell 中,您可以执行 sleep( ms ),例如在运行查询之前休眠 5 秒:

> sleep(5000); db.collection.find(..);

This doesn't pause the current query, but does pause execution on that connection for the specific number of milliseconds before continuing with the next statement (which is equivalent to a select sleep(5)in MySQL).

这不会暂停当前查询,但会在继续执行下一条语句(相当于select sleep(5)MySQL 中的 a)之前暂停该连接上的执行特定毫秒数。