以下查询
Install.eager_load(customer: :invoices).includes(calendar_appointment: :technician).where("state = 'scheduled' AND start_time < ?", Date.tomorrow)
真的很慢,我希望只返回没有关联发票的安装,因为这会使它运行得更快。我该怎么做呢?此外,任何提高此查询速度的想法都会有所帮助,相应的表将被编入索引。
答案 0 :(得分:0)
不确定关联的定义是如何确定的,但这是我对它的抨击:
Install
.joins("inner join invoices on installs.id = invoices.install_id")
.includes(calendar_appointment: :technician)
.where("state = 'scheduled' AND start_time < ? AND invoices.install_id IS NULL", Date.tomorrow)