20000 Leagues Under ActiveRecord

20000 Leagues Under ActiveRecord

Eb5382b137146b381dd13740d165d1f2?s=128

Pat Shaughnessy

September 12, 2014
Tweet

Transcript

  1. None
  2. User.where(name: 'Captain Nemo').first

  3. 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">
  4. None
  5. Agenda: ! - ActiveRecord::Relation - Generating SQL - Executing SQL

    - The B-Tree Algorithm
  6. class User < ActiveRecord::Base end User ActiveRecord::Base

  7. User.where(name: "Captain Nemo") User ActiveRecord::Querying def where end ActiveRecord::Relation def

    all end def where end
  8. User ActiveRecord::Base def where end ActiveRecord::Relation def all end def

    where end User.all.where(name: "Captain Nemo")
  9. ActiveRecord::Relation def where end ActiveRecord::Relation where: name="Captain Nemo" User.all.where(name: "Captain

    Nemo")
  10. 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
  11. def first(limit = nil) if limit find_nth_with_limit(offset_index, limit) else find_nth(0,

    offset_index) end end
  12. 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
  13. def forty_two find_nth(41, offset_index) end

  14. 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)
  15. 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)
  16. 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)
  17. 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)
  18. # 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
  19. None
  20. ActiveRecord::Relation where: name="Captain Nemo" limit: 1 offset: 0 order: id,

    ASC
  21. Arel::Nodes::Attribute ActiveRecord::Relation relation: User name: name Arel::Nodes::Equality where_values "$1"

  22. None
  23. None
  24. None
  25. None
  26. SELECT * FROM users WHERE name = 'Captain Nemo' ORDER

    BY id ASC LIMIT 1
  27. SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam Ascending

    Attribute Limit Quoted SelectCore
  28. User.where(name: 'Captain Nemo').first users = Arel::Table.new(:users)! users.project('*')! .where(users[:name].eq('Captain Nemo'))! .order(users[:id],

    'ASC')! .take(1)
  29. SELECT SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam

    Ascending Attribute Limit Quoted SelectCore
  30. SELECT SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam

    Ascending Attribute Limit Quoted SelectCore
  31. SELECT "users".* SelectStatement Attribute JoinSource Table Top And Equality Attribute

    BindParam Ascending Attribute Limit Quoted SelectCore
  32. SELECT "users".* FROM "users" SelectStatement Attribute JoinSource Table Top And

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

    JoinSource Table Top And Equality Attribute BindParam Ascending Attribute Limit Quoted SelectCore
  34. 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
  35. 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
  36. None
  37. None
  38. - Parse - Analyze and Rewrite - Plan - Execute

  39. None
  40. 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
  41. 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
  42. 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
  43. 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
  44. == 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn

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

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

    Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ...
  47. SELECT * FROM users WHERE name = 'Captain Nemo' ORDER

    BY id ASC LIMIT 1
  48. == 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn

    Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ...
  49. None
  50. class AddIndexOnUserName < ActiveRecord::Migration! def change! add_index :users, :name! end!

    end
  51. 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
  52. 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
  53. < '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%
  54. < '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%
  55. < '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%
  56. Insert: “Captain Nemo” ? Amina Doyle Arlie Walsh Boyd Pagac

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

    Pagac Monte Nicolas Ned Land Professor Aronnax available space… Captain Nemo
  58. 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
  59. None
  60. None
  61. None
  62. None
  63. None
  64. 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
  65. What have we learned?

  66. None