【发布时间】:2020-12-04 00:10:21
【问题描述】:
我有疑问:SELECT DISTINCT a1, a2, a3 FROM t WHERE a4 = '' ORDER BY a1, a5
和索引:(a4, a1, a5, a2, a3)
查询说明:Using where; Using index; Using temporary
索引中哪些字段顺序是最佳的或者查询可以优化?
UPD
EXPLAIN FORMAT=JSON SELECT ...:
{
"query_block": {
"select_id": 1,
"temporary_table": {
"function": "buffer",
"table": {
"table_name": "t",
"access_type": "range",
"possible_keys": ["my_index"],
"key": "my_index",
"key_length": "92",
"used_key_parts": ["a4"],
"rows": 113479,
"filtered": 100,
"attached_condition": "(t.a4 = '')",
"using_index": true
}
}
}
【问题讨论】:
-
没有
DISTINCT,我认为你的索引是最好的。请提供EXPLAIN FORMAT=JSON SELECT ... -
@RickJames 我在问题中添加了信息
-
如果没有
DISTINCT,你会得到多少行? (SELECT COUNT(*) WHERE a4='';) -
@RickJames 大约 120 000
-
听起来好像不需要
DISTINCT?
标签: mysql sql select query-optimization