For example, when trying to retrieve multiple (N) data using .each method etc., SQL is issued N times, which leads to performance degradation.
Suppose you have such a one-to-many Use and Message model.
app/model/user.rb
class Message < ApplicationRecord
has_many :messages
end
app/model/message.rb
class User < ApplicationRecord
belongs_to :user
end
Get the value to pass to the view in the controller
app/controllers/message_controller.rb
def index
@messages = Message.all
end
Display content and the name associated with it
app/views/index.html.erb
<% @messages.each do |m| %>
<span><%= m.content %> </span>
<span><%= m.user.name %> </span>
<% end %>
As for the SQL at that time, SQL will be issued for each record like this.
Message Load (3.7ms) SELECT `messages`.* FROM `messages` # +Part 1
↳ app/views/messages/index.html.erb:5
User Load (9.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
↳ app/views/messages/index.html.erb:8
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
↳ app/views/messages/index.html.erb:8
CACHE User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1 [["id", 3], ["LIMIT", 1]]
↳ app/views/messages/index.html.erb:8
CACHE User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1 [["id", 3], ["LIMIT", 1]]
↳ app/views/messages/index.html.erb:8
・
・
・
(Continued for N pieces)
Introduce gem bullet. Then, SQL like this will be issued and performance will improve.
Message Load (1.1ms) SELECT `messages`.* FROM `messages`
↳ app/views/messages/index.html.erb:5
User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IN (1, 3, 4)
Recommended Posts