【发布时间】:2021-09-18 17:07:20
【问题描述】:
我试图弄清楚为什么我的一个查询使用限制很慢以及如何解决它。
表大小:2.8M
按此顺序索引:
- user_id => transactions_user_id_foreign
- service => service_idx
- type => type_idx
- 货币 => currency_idx
- 状态 => status_idx
- created_at => created_at_idx
- currency_id => currency_id_idx
- user_action_id => user_action_id_idx
- provider => provider_idx
查询:
select *
from transactions
where type = 0
and status != '2'
and status != '0'
and service IN ('credit_card', 'paypal')
and created_at >= '2021-09-15 14:04:40'
order by id DESC
limit 200
解释:
+----+-------------+--------------+------------+-------+--------------------------------+------+---------+------+--------+----------+---------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+-------+--------------------------------+------+---------+------+--------+----------+---------------------------------------------------------+
| 1 | SIMPLE | transactions | NULL | range | service,type,status,created_at | type | 1 | NULL | 506082 | 0.05 | Using index condition; Using where; Backward index scan |
+----+-------------+--------------+------------+-------+--------------------------------+------+-------
现在大约需要 4 秒。如果我取消限制,则需要 0.03 秒。
我尝试了更多的索引组合,但没有成功。
【问题讨论】:
-
将
and status != '2' and status != '0更改为and status not in (2,0)有什么不同吗? -
请发布 A) SHOW INDEX FROM transactions 的 TEXT 结果; B) SHOW CREATE TABLE 交易;积极的副作用,索引将被刷新。
-
请向我们展示查询的两个完整版本 - 带和不带 LIMIT 以及每个版本的 EXPLAIN。
-
status中有多少个不同的值?
标签: mysql performance