MySQL排序优化
2021-07-09 本文已影响0人
养一只tom猫
最近在项目中有一个需求,将表中所有客户做出的数据查询出来并且统计每一个客户做的数量,数据量大概50W。
SQL大致如下
select
a.relation_id,
a.relation_name,
count(a.id) as total
from
table a
where
a.group_id = 'xx'
and a.org_id in('xx','xx')
group by a.relation_id
order by total
limit 0,40
在查询的时候,发现是排序太费时了
由于排序字段是count出来的,没办法使用索引进行优化