我遇到了没有使用正确索引的问题。
我在innodb表上有以下索引(大约500,000行):
+-------------+------------+------------------------+--------------+------------------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------------+------------+------------------------+--------------+------------------------+-----------+-------------+----------+--------+------+------------+---------+
| osdate_user | 0 | PRIMARY | 1 | id | A | 419700 | NULL | NULL | | BTREE | |
| osdate_user | 0 | email | 1 | email | A | 419700 | NULL | NULL | | BTREE | |
| osdate_user | 0 | username | 1 | username | A | 419700 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | lastvisit | 1 | lastvisit | A | 419700 | NULL | NULL | | BTREE | |
| osdate_user | 1 | active | 1 | active | A | 8 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | gender | 1 | gender | A | 88 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | regdate | 1 | regdate | A | 419700 | NULL | NULL | | BTREE | |
| osdate_user | 1 | lastupdate | 1 | lastupdate | A | 419700 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | password | 1 | password | A | 419700 | NULL | NULL | | BTREE | |
| osdate_user | 1 | age | 1 | age | A | 190 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | is_new | 1 | is_new | A | 8 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | private_photos | 1 | private_photos | A | 8 | NULL | NULL | | BTREE | |
| osdate_user | 1 | pictures_cnt | 1 | pictures_cnt | A | 10 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | pictures_cnt | 2 | private_photos | A | 10 | NULL | NULL | | BTREE | |
| osdate_user | 1 | status | 1 | status | A | 19 | NULL | NULL | | BTREE | |
| osdate_user | 1 | status | 2 | active | A | 19 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | status | 3 | gender | A | 19 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | status | 4 | age | A | 19 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | status | 5 | country | A | 7630 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | status | 6 | city | A | 46633 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | status | 7 | pictures_cnt | A | 83940 | NULL | NULL | YES | BTREE | |
| osdate_user | 1 | status | 8 | private_photos | A | 139900 | NULL | NULL | | BTREE | |
| osdate_user | 1 | status | 9 | lang | A | 209850 | NULL | NULL | | BTREE | |
| osdate_user | 1 | status | 10 | is_new | A | 209850 | NULL | NULL | YES | BTREE | |
+-------------+------------+------------------------+--------------+------------------------+-----------+-------------+----------+--------+------+------------+---------+
此查询:
EXPLAIN EXTENDED SELECT user.id, user.active
FROM osdate_user user
WHERE user.active =1
AND user.status = 'active'
AND user.gender = 'M'
AND user.age
BETWEEN 19
AND 35
AND user.pictures_cnt >0
AND user.private_photos = '0'
ORDER BY user.lastvisit DESC
LIMIT 0 , 24
显示以下计划:
+----+-------------+---------+-------------+--------------------------------------------------------------+------------------------------+---------+---------------------------+-------+----------+----------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+-------------+--------------------------------------------------------------+------------------------------+---------+---------------------------+-------+----------+----------------------------------------------------------------------------+
| 1 | SIMPLE | user | index_merge | PRIMARY,active,gender,age,private_photos,pictures_cnt,status | gender,private_photos,active | 4,1,2 | NULL | 28204 | 100.00 | Using intersect(gender,private_photos,active); Using where; Using filesort |
| 1 | SIMPLE | userext | eq_ref | userid | userid | 4 | db_name.user.id | 1 | 100.00 | Using index |
+----+-------------+---------+-------------+--------------------------------------------------------------+------------------------------+---------+---------------------------+-------+----------+----------------------------------------------------------------------------+
我的问题是为什么查询不是使用索引“status”
当我强制它使用索引“状态”时 - 它需要0.2秒,如果我不需要2.5秒
任何帮助非常感谢。问候。
答案 0 :(得分:0)
pictures_cnt
和status
pictures_cnt
而不是status
索引的字段。如果您在where
中有更多属于status
索引的字段,则可能决定使用该索引而不是picture_cnt
请注意,它不能同时使用。
如果你认为优化器是错误的(就像在不常见的情况下那样),你可以强迫它使用你希望它使用的索引。尽管做基准测试。
答案 1 :(得分:0)
在搜索了一下后,我发现了一些关于不使用索引中允许null的列的建议。我将状态索引中的列更改为非null,现在正在使用该索引。
通常情况下,最简单,最干净的解决方案是最好的。