查询主表然后在里面进行排序,然后在作为结果进行子查询进行group by 以及 order by
EXPLAIN SELECT * FROM (
SELECT t1.*
FROM leave_record_pre t1
ORDER BY t1.create_time DESC
) t
GROUP BY t.mark_id
, t.`pre_audit_status`
ORDER BY t.pre_audit_status ASC
, t.create_time DESC
;
运行用时3秒多
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 265471 Using temporary; Using filesort
2 DERIVED t1 ALL (NULL) (NULL) (NULL) (NULL) 265471 Using filesort
1秒内查询结果
我的建议 是 子查询不放 ORDER BY t1.create_time DESC 排序 这个没必要 因为 t1 整理查询的数据 都要GROUP BY 结果集进行分组,所以 会浪费查询时间!另外据我了解 索引的使用场景 应该在 where 上 你这也没用上 所以索引 没应用上!