Skip to content

Instantly share code, notes, and snippets.

@johncip
Last active December 28, 2024 15:12
Show Gist options
  • Select an option

  • Save johncip/17c496f04dcd032d594a785e2ad09824 to your computer and use it in GitHub Desktop.

Select an option

Save johncip/17c496f04dcd032d594a785e2ad09824 to your computer and use it in GitHub Desktop.

Revisions

  1. johncip revised this gist Jun 17, 2019. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion rails_eager_load.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    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`
    @@ -13,4 +16,13 @@
    `joins`
    * inner join
    * precise; allows you to *avoid* loading extra data
    * can use aliases to add new methods to left table's records
    * can use aliases to add new methods to left table's records

    Other AR optimizations
    * queries can be built without being fired (calling e.g. `all`, `first`, `count`, or `each` does that)
    * results of recent queries are cached
    * these don't help with the N+1 problem

    Tips
    * index on foreign keys
    * benchmark with a remote db if that's what prod uses
  2. johncip revised this gist May 28, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions rails_eager_load.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,14 @@
    `eager_load`
    * can reference the other table's columns in `where`
    * single query (left outer join)
    * can reference the other table's columns in `where`

    `preload`
    * a few queries (one per table)
    * typically faster
    * one query per table

    `includes`
    * acts like `preload` by default
    * acts like `eager_load` if you reference columns from associated tables
    * falls back on `eager_load` if you reference the associated tables

    `joins`
    * inner join
  3. johncip revised this gist May 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_eager_load.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    `includes`
    * acts like `preload` by default
    * acts like `eager_load` if you use `references`
    * acts like `eager_load` if you reference columns from associated tables

    `joins`
    * inner join
  4. johncip created this gist May 28, 2016.
    16 changes: 16 additions & 0 deletions rails_eager_load.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    `eager_load`
    * can reference the other table's columns in `where`
    * single query (left outer join)

    `preload`
    * typically faster
    * one query per table

    `includes`
    * acts like `preload` by default
    * acts like `eager_load` if you use `references`

    `joins`
    * inner join
    * precise; allows you to *avoid* loading extra data
    * can use aliases to add new methods to left table's records