#372 Bullet
Aug 09, 2012 | 6 minutes | Performance, Plugins
Bullet will notify you of database queries that can potentially be improved through eager loading or counter cache column. A variety of notification alerts are supported.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Why would you try to avoid
ActiveRecord
in migrations when your migration is anActiveRecord::Migration
class?+1, I'm curious to hear more about why Ryan prefers writing SQL within migrations.
I think raw SQL within migration is the only option to update counter cache column.
Sorry, we can use .reset_counters:
Category.all.map(&:id).each { |id| Category.reset_counters(id, :products) }
When you step through migration steps, your model class might not correspond to the fields in the database for previous migrations. Let's say at a later point you change the model, and this older migration needs to run, it might fail because you've added validation logic or subsequently altered/removed a column that it's trying to migrate.
Makes sense, thanks Andrew.
Exactly right. This why, if I "must" use AR in a migration, I prefer to redefine (much simplified) classes within the migration file... ensuring that the AR class definitions match the db schema.
Also, for large datasets, executing SQL rather than instantiating AR records can mean your migration runs in seconds rather than minutes/hours.
Very nice! It would be even nicer if it did something like the miniprofiler showed in episode #368 and display a small expandable box in the corner of your web page with the info about slow queries.
Oh yes...Even I was thinking on the same lines!!!
Does this gem do
query_trace
style logging? What's the current fancy option for that?I enjoyed this episode, though the second migration to populate counts for previous records didn't seem to go over so well when I ran it on Heroku. I ended up rolling back the second migration.
How can you fix n + 1 queries on a has :through association?
Thanks ! Just used it today and the gem found a good optimization !
Can I enable/disable the alerts only for some paths/controllers ?
I got a lot of alerts on activeadmin and I cannot change that code.
For example I get one for the
column :user_type
line:If it's not too late: The GitHub page has a section "Whitelist" where it is explained how you can add third-party gems you want to exclude.
Great post.
Thanks for this
Somebody already provided this thing.