Share
Embed
20000 Leagues Under ActiveRecord
Pat Shaughnessy
September 12, 2014
Transcript
66 slides
-
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"> -
Agenda: ! - ActiveRecord::Relation - Generating SQL - Executing SQL
- The B-Tree Algorithm -
User ActiveRecord::Base def where end ActiveRecord::Relation def all end def
where end User.all.where(name: "Captain Nemo") -
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 -
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 -
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) -
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) -
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) -
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) -
# 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 -
SELECT * FROM users WHERE name = 'Captain Nemo' ORDER
BY id ASC LIMIT 1 -
SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam Ascending
Attribute Limit Quoted SelectCore -
SELECT SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam
Ascending Attribute Limit Quoted SelectCore -
SELECT SelectStatement Attribute JoinSource Table Top And Equality Attribute BindParam
Ascending Attribute Limit Quoted SelectCore -
SELECT "users".* SelectStatement Attribute JoinSource Table Top And Equality Attribute
BindParam Ascending Attribute Limit Quoted SelectCore -
SELECT "users".* FROM "users" SelectStatement Attribute JoinSource Table Top And
Equality Attribute BindParam Ascending Attribute Limit Quoted SelectCore -
SELECT "users".* FROM "users"! WHERE "users"."name" = $1 SelectStatement Attribute
JoinSource Table Top And Equality Attribute BindParam Ascending Attribute Limit Quoted SelectCore -
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 -
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 -
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 -
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 -
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 -
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 -
== 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn
Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ... -
== 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn
Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ... -
== 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn
Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ... -
SELECT * FROM users WHERE name = 'Captain Nemo' ORDER
BY id ASC LIMIT 1 -
== 'Captain Nemo' ? Laurianne Goodwin Candace Kris Miss Jaclyn
Hansen Abby Powlowski Captain Nemo Ludwig O'Connell Jalyn Schroeder Rubye Morar etc ... -
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 -
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 -
< '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% -
< '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% -
< '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% -
Insert: “Captain Nemo” ? Amina Doyle Arlie Walsh Boyd Pagac
200 more names ... Torrance Schmitt Tyrese Walsh Zora Emmerich -
available space… NULL Monte Nicolas Amina Doyle Arlie Walsh Boyd
Pagac Monte Nicolas Ned Land Professor Aronnax available space… Captain Nemo -
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 -
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