İçeriğe geç

ORM N+1 Sorgusu (ARCH6079)

Anahtar ruby.performance.orm-n-plus-one · Önem error · Kategori performance

Bir sorgu sonucu üzerinde döngü kurup her öğenin ilişkisine erişmek, kayıt başına ayrı bir sorgu tetikler (N+1). İlişkiyi includes ile önceden yükleyin.

def author_names(posts)
Post.where(published: true).map do |post|
post.author.name
end
end
def author_names(posts)
Post.where(published: true).includes(:author).map do |post|
post.author.name
end
end

İlişkili kayıtlara döngüde erişeceğinizde includes/preload ile eager loading uygulayın.