[英]Why MySQL isn't using my FULLTEXT indexes?
I have the following query: 我有以下查询:
EXPLAIN SELECT *
FROM glean2_saves
WHERE username = '1d85d5aed8b02b3d6b0c155a563293ef'
AND ses_id = 'e4fa3ae216f5033fbd16d6c66370954c'
AND save_status =1
ORDER BY id DESC
And the result is this: 结果如下:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE glean2_saves ref save_status,username,ses_id save_status 2 const 286315 Using where; Using filesort
Here are my indexes: 这是我的索引:
Action Keyname Type Unique Packed Column Cardinality Collation Null Comment
Edit Edit Drop Drop PRIMARY BTREE Yes No id 331837 A
Edit Edit Drop Drop save_status BTREE No No save_status 3 A YES
Edit Edit Drop Drop nickname FULLTEXT No No nickname 7374 YES
Edit Edit Drop Drop username FULLTEXT No No username 7717 YES
Edit Edit Drop Drop ses_id FULLTEXT No No ses_id 11442 YES
I do have indexes on the right columns, but why are they not used ? 我在右列上有索引, 但为什么不使用它们 ? How come they are in the "possible keys" (save_status, username, ses_id) but only one "key" is actually used: save_status 为什么他们在“可能的键”(save_status,username,ses_id)中,但实际上只使用了一个“键”:save_status
Without the indexes, the query takes way too long (sometimes over 15 seconds), it should take less than one second. 如果没有索引,查询会占用太长时间(有时超过15秒),它应该不到一秒钟。 The database is over 330k entries. 该数据库超过330k条目。
For this query, you want one index that covers the where
and order by
clauses. 对于此查询,您需要一个涵盖where
和order by
子句的索引。 That index would be: 该指数将是:
glean2_saves(username, ses_id, save_status, id)
Note that because you are doing equality comparisons, the first three columns can be in any order. 请注意,因为您正在进行相等比较,所以前三列可以按任何顺序排列。 But the last column needs to be id
for the order by
. 但是最后一列需要为order by
id
。
As for why MySQL is not using the individual indexes. 至于为什么 MySQL没有使用个别索引。 Let's just say that merging indexes for where
clauses is a lot of work, and might result in more work than scanning one index and doing the tests for the remaining columns. 我们只是说合并where
子句的索引是很多工作,并且可能导致比扫描一个索引和对其余列进行测试更多的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.