Sunday, 15 June 2014

ruby on rails - How do I optimize a query when includes won't work? -



ruby on rails - How do I optimize a query when includes won't work? -

i have these each loops:

<% relation_types.each |relation| %> <% if !current_user.memberships.where(relation: relation).empty? %> <% current_user.memberships.where(relation: relation).each |membership| %> <% end %>

both of generate log on each pageload:

user load (32.1ms) select "users".* "users" "users"."id" = 1 order "users"."id" asc limit 1 familytree load (6.2ms) select "family_trees".* "family_trees" "family_trees"."user_id" = $1 limit 1 [["user_id", 1]] (3.0ms) select "memberships"."relation" "memberships" "memberships"."family_tree_id" = $1 [["family_tree_id", 1]] (3.7ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'great_grandmother' [["user_id", 1]] (17.9ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'great_grandfather' [["user_id", 1]] (3.3ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'grandmother' [["user_id", 1]] (1.8ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'grandfather' [["user_id", 1]] (3.5ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'mom' [["user_id", 1]] (14.9ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'dad' [["user_id", 1]] (2.8ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'aunt' [["user_id", 1]] (3.9ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'uncle' [["user_id", 1]] membership load (3.4ms) select "memberships".* "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'uncle' [["user_id", 1]] (2.7ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'sister' [["user_id", 1]] (14.2ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'brother' [["user_id", 1]] membership load (3.2ms) select "memberships".* "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'brother' [["user_id", 1]] (3.7ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'cousin' [["user_id", 1]] (4.3ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'daughter' [["user_id", 1]] (3.8ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'son' [["user_id", 1]] (5.7ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'niece' [["user_id", 1]] (3.0ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'nephew' [["user_id", 1]] membership load (3.9ms) select "memberships".* "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'nephew' [["user_id", 1]] (2.4ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'granddaughter' [["user_id", 1]] (10.7ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'grandson' [["user_id", 1]] (4.7ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'great_granddaughter' [["user_id", 1]] (5.4ms) select count(*) "memberships" "memberships"."user_id" = $1 , "memberships"."relation" = 'great_grandson' [["user_id", 1]]

this schema & associations membership:

# == schema info # # table name: memberships # # id :integer not null, primary key # family_tree_id :integer # user_id :integer # created_at :datetime # updated_at :datetime # relation :string(255) class membership < activerecord::base attr_accessible :user_id, :relation belongs_to :family_tree belongs_to :user end

how optimize this?

how grouping relation?

in controller:

@memberships_grouped_by_relations = current_user.memberships.group_by(&:relation)

in view:

<% @memberships_grouped_by_relations.each |relation, memberships| %> <% if memberships.any? %> <% memberships.each |membership| %> <% end %> <% end %> <% end %>

ruby-on-rails ruby-on-rails-4 optimization query-optimization

No comments:

Post a Comment