N+1 query problem
- ORMs make it easy to a query per loop iteration, which we want to avoid
eager_load
- single query (left outer join)
- can reference the other table's columns in
where
preload
- a few queries (one per table)
- typically faster
| class ArticleImageUploader < ImageUploader | |
| process :fix_exif_rotation | |
| process :strip | |
| process :convert => 'jpg' | |
| process :quality => 85 # Percentage from 0 - 100 | |
| version :gallery_thumb do | |
| process :resize_to_fill => Settings.images.article_images.processing.gallery_thumb #44x44 | |
| end |
N+1 query problem
eager_load
wherepreload
| ###################### | |
| # | |
| # Monkey patch to ActiveRecord to prevent 'implicit' checkouts. Currently tested with Rails 4.0.8, prob | |
| # should work fine in Rails 4.1 too. | |
| # | |
| # If you create a thread yourself, if it uses ActiveRecord objects without | |
| # explicitly checking out a connection, one will still be checked out implicitly. | |
| # If it is never checked back in with `ActiveRecord::Base.clear_active_connections!`, | |
| # then it will be leaked. | |
| # |
| http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query | |
| http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails | |
| #payload: [{"kind"=>"person"}] | |
| Segment.where("payload @> ?", [{kind: "person"}].to_json) | |
| #data: {"interest"=>["music", "movies", "programming"]} | |
| Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json) | |
| Segment.where("data #>> '{interest, 1}' = 'movies' ") | |
| Segment.where("jsonb_array_length(data->'interest') > 1") |