咨询关于 order_by 和 group_by 的一些索引问题
表结构
表名 label_file_bridges
+————+————+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+————+————+——+—–+———+—————-+
| id | int(32) | NO | PRI | NULL | auto_increment |
| label_id | int(32) | NO | MUL | NULL | |
| file_id | int(32) | NO | | NULL | |
| is_active | tinyint(1) | NO | | NULL | |
+————+————+——+—–+———+—————-+
索引
PRIMARY id
idx_label_file_bridge_label_id_file_id_is_active (label_id, file_id, is_active)
sql
select file_id, group_concat(label_id) from label_file_bridges where label_id in (1,2,3,4,5,6,7,8,9,10) and is_active=1 group by file_id;
explain
id: 1
select_type: SIMPLE
table: label_file_bridges
partitions: NULL
type: index
possible_keys: idx_label_file_bridge_label_id_file_id_is_active
key: idx_label_file_bridge_label_id_file_id_is_active
key_len: 9
ref: NULL
rows: 48182
filtered: 10.00
Extra: Using where; Using index; Using filesort
疑问
表数据量百万级,以上是测试数据, 使用此 sql 速度还行,但是存在外层排序 filesort, 调试发现用 in + order_by 或者 in + group_by 都会有这个情况 请教一下这个 sql 或者表结构 ( 索引 ) 有可优化的地方吗,有可能避免 filesort 吗