我想弄清楚為什么我的一個查詢在使用限制時很慢,以及我怎樣才能解決它。
表的大小:2.8M
索引的順序是:索引的順序是:
- user_id => transactions_user_id_foreign 。
- service => service_idx 。
- type => type_idx 。
- 貨幣 => currency_idx
- status => status_idx created_at => created_at_idx
- currency_id => currency_idx
- user_action_id =>currency_idx
- user_action_id => user_action_idx 。
- provider => provider_idx 。
查詢:
select *
from transactions
where type = 0
and status != '2'/span>
and status ! = '0'
and service IN ('credit_card', 'paypal')
and created_at >= '2021-09-15 14:04:40'
order by id DESC
極限200
解釋:
---- ------------- -------------- ------------ ------- -------------------------------- ------ --------- ------ -------- ---------- ---------------------------------------------------------
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---- ------------- -------------- ------------ ------- -------------------------------- ------ --------- ------ -------- ---------- ---------------------------------------------------------
| 1 | SIMPLE | transactions | NULL | range | service。 型別,狀態。 created_at | type | 1 | NULL | 506082 | 0。 05 | 使用指數 條件。使用 where; 向后的索引掃描 |
---- ------------- -------------- ------------ ------- -------------------------------- ------ -------
現在,它需要大約4秒鐘。如果我取消限制,則需要0.03秒的時間。
我嘗試了更多的索引組合,但沒有成功。
uj5u.com熱心網友回復:
我建議兩個索引。 (優化器會選擇它喜歡的那一個。)
INDEX(type, service, created_at) --按這個順序。
INDEX(type, created_at) --按這個順序。
然后洗掉現在多余的
。INDEX(type)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326832.html
標籤:
上一篇:如何用Pandas有效地在前一個和后一個行索引之間取平均值?
下一篇:NumPy陣列的計算時間問題
