over 1 year ago

counter_cache
https://rocodev.gitbooks.io/rails-102/content/chapter1-mvc/m/counter-cache.html

[Rails] Counter Cache用法
http://anxgang.logdown.com/posts/732495-rails-counter-cache-usage

計數快取 Counter Cache
https://ihower.tw/rails/performance.html

範例一:一個醫生有幾篇POST
Post還沒有裝Counter Cache

Post裝上Counter Cache
(Post belongs_to :doctor, counter_cache: :post_count)

這兩行不見了:
(0.2ms) SELECT COUNT() FROM posts WHERE posts.doctor_id = 2
(0.2ms) SELECT COUNT(
) FROM posts WHERE posts.doctor_id = 4

範例二:一篇POST有幾則回覆
Comment還沒有裝Counter Cache

Comment裝上Counter Cache
(Comment belongs_to :post, counter_cache: :comment_count)

這五行不見了:
(0.3ms) SELECT COUNT() FROM posts WHERE posts.doctor_id = 2
(0.4ms) SELECT COUNT(
) FROM comments WHERE comments.post_id = 21
(0.4ms) SELECT COUNT() FROM comments WHERE comments.post_id = 20
(0.3ms) SELECT COUNT(
) FROM comments WHERE comments.post_id = 18
(0.3ms) SELECT COUNT() FROM comments WHERE comments.post_id = 15
(0.3ms) SELECT COUNT(
) FROM comments WHERE comments.post_id = 14

← 用 mechanize gem 去爬 yahoo search page (first page) impressionist →