How to analyze database statements in a Rails app

Posted . Visible to the public.

Marginalia Show archive.org snapshot is a gem that will help you to find out where in your code database queries are triggered.

After adding it to your Gemfile, add a config/initializers/marginalia.rb with the following contents:

Marginalia::Comment.components = [:line]
Marginalia::Comment.prepend_comment = true

And in your log you will see the file and line number before each database statement.
If you are only interested in writing statements, you can grep through your log like this:

tail -f log/test.log | grep -E "( Create | Update )"

This can be really helpful to find out which code triggers a lot of updates.

Judith Roth
Last edit
Judith Roth
Posted by Judith Roth to Judith's Dev Notes (2023-01-25 15:14)