晚上所有,
我刚刚有一个关于 SQL 平均查询的快速问题。
我有一个有很多评级的帖子模型。将特定帖子的所有评分相加并除以平均评分数。所以这个计算在 post 模型中声明并保存在数据库中。在我的索引视图中,我列出了所有帖子及其作者、姓名和评级,这些来自帖子评级数据库列。
我的问题是,当这些值已保存在数据库中时,为什么 rails 会执行所有这些 SQL 平均查询?
我的代码如下:
在控制台中
Post Load (1.0ms) SELECT `posts`.* FROM `posts` ORDER BY posts.created_at ASC LIMIT 1
=> #<Post id: 48, user_id: 5, song_name: "Enter Sandman", song: "ewwe wer ere rr erew rewr r erw erw rewedwde", created_at: "2012-08-16 13:35:32", updated_at: "2012-08-22 21:11:34", rating: 5.0, ratings_count: 2>
Post.rb
def rating
if self.ratings.any?
self.rating = self.ratings.average(:rating)
end
end
帖子/index.html.erb
<legend>Music library</legend>
<%= will_paginate @paginate_posts %>
<% if @paginate_posts.any? %>
<% @paginate_posts.each do |post| %>
<h4><%= link_to post.song_name, post %></h4>
Author: <%= link_to post.user.name, "#" %><br/>
<% if post.rating == nil %>
No one has rated this yet<br/>
<% else %>
Rating: <%= post.rating %>/10<br/>
<% end %>
<br/>
<% end %>
<% end %>
<%= will_paginate @paginate_posts %>
posts_controller.rb 索引操作(我尝试过急切加载,但平均查询仍然存在)
def index
@paginate_posts = Post.paginate(page: params[:page], per_page: 10).includes(:user).search(params[:search])
end
包含大量查询的 SQL 日志
Started GET "/" for 127.0.0.1 at 2012-08-22 22:51:38 +0100
Processing by PostsController#index as HTML
Post Load (0.3ms) SELECT `posts`.* FROM `posts` ORDER BY posts.created_at DESC LIMIT 10 OFFSET 0
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IN (5)
(0.3ms) SELECT COUNT(*) FROM `posts`
Rendered posts/_copy.html.erb (0.1ms)
(0.3ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 63
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 63
(0.3ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 62
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 62
(0.2ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 61
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 61
(0.2ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 60
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 60
(0.2ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 59
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 59
(0.2ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 58
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 58
(0.2ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 57
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 57
(0.2ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 56
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 56
(0.2ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 55
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 55
(0.3ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 54
CACHE (0.0ms) SELECT AVG(`ratings`.`rating`) AS avg_id FROM `ratings` WHERE `ratings`.`post_id` = 54
Rendered posts/index.html.erb within layouts/application (60.8ms)
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 2012ms (Views: 1871.7ms | ActiveRecord: 33.1ms)
像往常一样,如果您需要更多代码,请大喊。非常感谢,安迪