Reputation: 197
I am working on a page in my Rails app that needs to display information through many associated tables. Currently, this page takes ~15 seconds to load about 700 records. and I'm trying to optimize the data query. Currently, my query looks like this:
@students = Student.includes(:school, orders: { cart: { packages: [:options, :order_packages] } })
.where('students.school_id = ?', params[:school_id])
.where('students.teacher like ?', '%day%')
.where('students.grade = ?', '12')
.order(:last_name)
There are deeply nested associations required for this query through the .includes method. Is there a more efficient way to run this query?
Upvotes: 0
Views: 23
Reputation: 239392
This doesn't seem like an eager-loading problem. You're just querying against columns in the students
table. Make sure those columns have proper indexes and that your query is using those indexes. Make sure the associated table's foreign keys have indexes and that those are also used to fulfill the eager-loading.
Upvotes: 3