簡體   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