Use statement cache for `find_by(author: david)`
```ruby ActiveRecord::Schema.define do create_table :posts, force: true do |t| end create_table :comments, force: true do |t| t.belongs_to :post end end class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end post = Post.create! Benchmark.ips do |x| x.report("find_by post") { Comment.find_by(post: post) } end ``` Before: ``` Warming up -------------------------------------- find_by post 395.000 i/100ms Calculating ------------------------------------- find_by post 4.026k (± 3.1%) i/s - 20.145k in 5.008059s ``` After: ``` Warming up -------------------------------------- find_by post 1.183k i/100ms Calculating ------------------------------------- find_by post 11.859k (± 2.5%) i/s - 60.333k in 5.090685s ```
Showing
想要评论请 注册 或 登录