繁体   English   中英

为什么mysql没有使用正确的索引?

[英]why is mysql not using the right index?

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

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

任何帮助,不胜感激。 问候。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM