N+1 Query (ARCH6050)
Esta página aún no está disponible en tu idioma.
Key ruby.orm.n-plus-one-query · Severity warning · Category orm
Fetching a related record with a separate query for each element while iterating over a collection causes the N+1 query problem. This is the classic ORM performance killer.
Incorrect
Section titled “Incorrect”posts = Post.allposts.each do |post| puts post.author.name # a separate SELECT per iterationendCorrect
Section titled “Correct”posts = Post.includes(:author)posts.each do |post| puts post.author.name # author loaded in one goendEager-load related data with includes before the loop; do not issue a new query per iteration.