2

我有一个问题,没有使用正确的索引。

我在 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                                                                |
+----+-------------+---------+-------------+--------------------------------------------------------------+------------------------------+---------+---------------------------+-------+----------+----------------------------------------------------------------------------+

我的问题是为什么不使用索引“状态”的查询

当我强制它使用索引“状态”时 - 需要 0.2 秒,如果我不使用则需要 2.5 秒

非常感谢任何帮助。问候。

4

2 回答 2

0

你有几个与他们索引的字段重叠的索引。例如 pictures_cntstatus
优化器“认为”您的查询更接近于使用字段pictures_cnt而不是status索引。如果您在where属于status索引的字段中有更多字段,它可能会决定使用该索引而不是picture_cnt

请注意,它不能同时使用两者。

如果您认为优化器是错误的(在不常见的情况下就是这种情况),您可以强制它使用您希望它使用的索引。做基准测试。

于 2013-01-13T14:15:41.690 回答
0

经过一番搜索,我发现了一些关于不使用索引中允许 null 的列的建议。我将状态索引中的列都更改为非空,现在正在使用该索引。

通常情况下,最简单和最干净的解决方案是最好的。

于 2013-01-13T22:41:03.960 回答