背景:
一个 id 为特定值的慢SQL
全表 group by 耗时3s
SELECT col_id,sum(money) as money FROM t where logtime >= ‘2022-08-23’ group by col_id;
18 rows in set (3.20 sec)
id为127慢:
SELECT col_id, sum(money) as money FROM t WHERE col_id = 127 AND logtime >= ‘2022-08-23’ group by col_id;
1 row in set (59.00 sec)
改写后更慢
select * from (SELECT col_id, sum(money) as money FROM t where logtime >= ‘2022-08-23’ group by col_id) as tmp_a where tmp_a.col_id = 127;
1 row in set (1 min 11.29 sec)
id为100快:
SELECT col_id, sum(money) as money FROM t WHERE col_id = 100 AND logtime >= ‘2022-08-23’ group by col_id;
1 row in set (3.40 sec)
表 t 为分区表,同时id为100的条目数比id为127的条目数要多,在 col_id 上有索引在查询时也利用上了该索引,这个群集后面也没加 TiFlash 之类的就 pd,tidb,tikv三个组件
所以怀疑是否是硬件性能问题,想看下 col_id = 127 的 region 分布在哪个 store 上。