Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate slow query in mongodb?

Tags:

mongodb

Does MongoDb have anything like MySql's 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

like image 998
Oleg Mikheev Avatar asked Oct 16 '12 06:10

Oleg Mikheev


People also ask

Does MongoDB log slow queries?

or slow queries and operations. By default, mongod records slow queries to its log, as defined by slowOpThresholdMs. Enabling database profiler puts negative impact on MongoDB's performance.

What is slow query in MongoDB?

We also call it “slow queries”. The slow queries can happen when you do not have proper DB indexes. Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement.

Can we run query efficiently in MongoDB?

Performance. Because the index contains all fields required by the query, MongoDB can both match the query conditions and return the results using only the index. Querying only the index can be much faster than querying documents outside of the index.

Which query is taking long time in MongoDB?

One can identify slow queries in MongoDB by enabling the profiler and configuring it to its some specifications or executing db. currentOp() on a running mongod instance. By looking at the time parameters on the returned result, we can identify which queries are lagging.


2 Answers

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

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.

like image 59
code_monkey_steve Avatar answered Sep 22 '22 09:09

code_monkey_steve


You can use the $where operator as code_monkey_steve says, 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 sleep command is javascript, so it would apparently block other javascript queries you could be making.

like image 29
RubenCaro Avatar answered Sep 22 '22 09:09

RubenCaro