Upgrade to Pro — share decks privately, control downloads, hide ads and more …

20000 Leagues Under ActiveRecord

20000 Leagues Under ActiveRecord

Pat Shaughnessy

September 12, 2014
Tweet

More Decks by Pat Shaughnessy

Other Decks in Technology

Transcript

  1. irb(main):001:0> User.where(name: 'Captain Nemo').first ! User Load (0.1ms) SELECT "users".*

    FROM "users" WHERE "users"."name" = 'Captain Nemo' ORDER BY "users"."id" ASC LIMIT 1 ! => #<User id: 1, name: "Captain Nemo">
  2. ActiveRecord::Relation def first end ActiveRecord::Relation where: name="Captain Nemo" order: id,

    ASC limit: 1 offset: 0 where: name="Captain Nemo" User.all.where(name: "Captain Nemo") .first
  3. def second find_nth(1, offset_index) end def third find_nth(2, offset_index) end

    def fourth find_nth(3, offset_index) end def fifth find_nth(4, offset_index) end
  4. ActiveRecord::Relation def first end ActiveRecord::Relation where: name="Captain Nemo" order: id,

    ASC limit: 1 offset: 0 where: name="Captain Nemo" User.all.where(name: "Captain Nemo") .find_nth(0, 0)
  5. ActiveRecord::Relation def first end ActiveRecord::Relation where: name="Captain Nemo" order: id,

    ASC limit: 1 offset: 0 where: name="Captain Nemo" User.all.where(name: "Captain Nemo") .find_nth_with_limit(0, 1)
  6. ActiveRecord::Relation def first end ActiveRecord::Relation where: name="Captain Nemo" order: id,

    ASC limit: 1 offset: 0 where: name="Captain Nemo" User.all.where(name: "Captain Nemo") .order(arel_table[primary_key].asc) .limit(1)
  7. ActiveRecord Base ActiveRecord Relation ActiveRecord Relation ActiveRecord Relation ActiveRecord Relation

    all where order limit User.all.where(name: "Captain Nemo") .order(arel_table[primary_key].asc) .limit(1)
  8. # Converts relation objects to Array. def to_a load @records

    end User.all.where(name: "Captain Nemo") .order(arel_table[primary_key].asc) .limit(1).to_a PostgreSQLAdapter.select_all
  9. SELECT "users".* FROM "users" SelectStatement Attribute JoinSource Table Top And

    Equality Attribute BindParam Ascending Attribute Limit Quoted SelectCore
  10. SELECT "users".* FROM "users"! WHERE "users"."name" = $1 SelectStatement Attribute

    JoinSource Table Top And Equality Attribute BindParam Ascending Attribute Limit Quoted SelectCore
  11. SELECT "users".* FROM "users"! WHERE "users"."name" = $1! ORDER BY

    "users"."id" ASC SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam Ascending Attribute Limit Quoted SelectCore
  12. SELECT "users".* FROM "users"! WHERE "users"."name" = $1! ORDER BY

    "users"."id" ASC LIMIT 1 SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam Ascending Attribute Limit Quoted SelectCore
  13. el ruby Tokens Grammar Rule Stack gusta le el ruby

    Tokens Grammar Rule Stack SheLikes shift reduce gusta el ruby Tokens Grammar Rule Stack le
  14. SELECT RESTARGET A_STAR AEXPR SORTBY A_CONST COLUMNREF users from clause

    fields RANGEVAR users target list where clause sort clause limit count name: "=" COLUMNREF users.id COLUMNREF users.name A_CONST val: "Captain Nemo" val: 1 lexpr rexpr node
  15. QUERY RTE ALIAS jointree FROMEXPR users rtable target list sort

    clause limit count RANGETBLREF rtindex 1 OPEXPR users fromlist quals RELABELTYPE CONST "Captain Nemo" VAR args arg id name created_at updated_at SORTGROUPCLAUSE FUNCEXPR users CONST 1
  16. PLANNEDSTMT LIMIT startup cost 21.56 total_cost: 21.57 SORT startup cost

    21.56 total_cost: 21.57 SEQSCAN startup cost 0.00 total_cost: 21.55 OPEXPR users RELABELTYPE CONST "Captain Nemo" VAR args
  17. == 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn

    Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ...
  18. == 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn

    Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ...
  19. == 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn

    Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ...
  20. == 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn

    Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ...
  21. Laurianne Goodwin Candace Kris Miss Jaclyn Hansen Abby Powlowski Captain

    Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar Abby Powlowski Candace Kris Captain Nemo Jalyn Schroeder Laurianne Goodwin Ludwig O'Connell Miss Jaclyn Hansen Rubye Morar
  22. PLANNEDSTMT LIMIT startup cost 8.30 total_cost: 8.31 SORT startup cost

    8.30 total_cost: 8.31 INDEXSCAN startup cost 0.28 total_cost: 8.29 OPEXPR users RELABELTYPE CONST "Captain Nemo" VAR args
  23. < 'Captain Nemo' ? Amina Doyle Arlie Walsh Boyd Pagac

    Candace Kris Captain Nemo Catherine Wisoky PhD Cheyenne Jenkins Conor Collier Conseil Della Christiansen Desmond Wintheiser Era Beier Fanny Reinger Giovanni Rau Godfrey Fadel Ines Halvorson V Jaleel O'Hara Jalyn Schroeder Jermaine Schuppe Keara Schumm Kendall Ledner MD Keon Fisher Laurianne Goodwin Ludwig O'Connell Merlin Davis Miss Deja Kris Miss Jaclyn Hansen Mr. Hadley Bode Mr. Kenneth Wiza Ms. Kaylie Morar Ned Land Professor Aronnax Rae Dare Rubye Morar Samson Auer MD Santino Sipes Sibyl Ebert V Torrance Schmitt Tyrese Walsh Zora Emmerich 50%
  24. < 'Captain Nemo' ? Amina Doyle Arlie Walsh Boyd Pagac

    Candace Kris Captain Nemo Catherine Wisoky PhD Cheyenne Jenkins Conor Collier Conseil Della Christiansen Desmond Wintheiser Era Beier Fanny Reinger Giovanni Rau Godfrey Fadel Ines Halvorson V Jaleel O'Hara Jalyn Schroeder Jermaine Schuppe Keara Schumm Kendall Ledner MD Keon Fisher Laurianne Goodwin Ludwig O'Connell Merlin Davis Miss Deja Kris Miss Jaclyn Hansen Mr. Hadley Bode Mr. Kenneth Wiza Ms. Kaylie Morar Ned Land Professor Aronnax Rae Dare Rubye Morar Samson Auer MD Santino Sipes Sibyl Ebert V Torrance Schmitt Tyrese Walsh Zora Emmerich 25%
  25. < 'Captain Nemo' ? Amina Doyle Arlie Walsh Boyd Pagac

    Candace Kris Captain Nemo Catherine Wisoky PhD Cheyenne Jenkins Conor Collier Conseil Della Christiansen Desmond Wintheiser Era Beier Fanny Reinger Giovanni Rau Godfrey Fadel Ines Halvorson V Jaleel O'Hara Jalyn Schroeder Jermaine Schuppe Keara Schumm Kendall Ledner MD Keon Fisher Laurianne Goodwin Ludwig O'Connell Merlin Davis Miss Deja Kris Miss Jaclyn Hansen Mr. Hadley Bode Mr. Kenneth Wiza Ms. Kaylie Morar Ned Land Professor Aronnax Rae Dare Rubye Morar Samson Auer MD Santino Sipes Sibyl Ebert V Torrance Schmitt Tyrese Walsh Zora Emmerich 12.5%
  26. Insert: “Captain Nemo” ? Amina Doyle Arlie Walsh Boyd Pagac

    200 more names ... Torrance Schmitt Tyrese Walsh Zora Emmerich
  27. available space… NULL Monte Nicolas Amina Doyle Arlie Walsh Boyd

    Pagac Monte Nicolas Ned Land Professor Aronnax available space… Captain Nemo
  28. available space… Amina Doyle Arlie Walsh Boyd Pagac Monte Nicolas

    Ned Land Professor Aronnax available space… Captain Nemo available space… Amina Doyle Arlie Walsh Boyd Pagac Captain Nemo NULL Julius Powlowski Monte Nicolas
  29. available space… Amina Doyle Arlie Walsh Boyd Pagac Monte Nicolas

    Ned Land Professor Aronnax available space… Captain Nemo available space… Amina Doyle Arlie Walsh Boyd Pagac Captain Nemo NULL Julius Powlowski Monte Nicolas