还有其他方法可以优化此查询吗?
它需要大约44毫秒才能运行但是希望它能低得多。任何想法??
@@accepted_list = History.select('DISTINCT ON (item_id) *')
.joins("LEFT JOIN case_statuses on histories.item_id = case_statuses.id LEFT JOIN loan_applications on loan_applications.id = case_statuses.loan_application_id")
.where("loan_applications.product_id = ?", 9)
.where(item_type: 'CaseStatus', field: 'loan_case_id', new_value: "6")
.where("histories.created_at >= ? and histories.created_at <= ?", '2017-02-10 16:00:00.000000', '2017-03-17 15:59:59.000000')
.order("item_id, histories.created_at asc")
@@accepted_cases_list.where("histories.created_at >= ? and histories.created_at <= ?", '2017-03-11 16:00:00.000000', '2017-03-17 15:59:59.000000')
.order("item_id, histories.created_at asc")
两个查询之间的唯一区别是日期,这是因为,我使用从第一个查询获得的记录,并按第二个查询中更具体的日期范围进行过滤。
但两者也在同一时间运行。
任何想法将不胜感激。