Saturday, June 18, 2011

Logging MongoDB Queries

Why do you need to log queries?
  • In case you want to know how long it takes to execute a specific query.
  • To see which is the slowest query.
  • If you want to see which queries are actually being run (in case you're using a layer of abstraction like Morphia this can be a pretty interesting question)
  • ...
Using MongoDB's built in profiling tool it's very simple to achieve this:
  1. You can enable and disable profiling on a per database level (being disabled by default).
  2. Connect to the database on the shell / CMD (assuming you're using the port 8082): mongo localhost:8082/erpel_test
  3. Activate profiling for all queries, not just slow ones: db.setProfilingLevel(2);
  4. Exit the shell / CMD and run some queries.
  5. Your database should now have a new collection called system.profile, showing the raw queries and how long it took to execute them.
For additional profiling options see http://www.mongodb.org/display/DOCS/Database+Profiler.

1 comment:

  1. Also check this out: http://www.lennartkoopmann.net/post/3603788835/mongo-analyzer

    ReplyDelete