正しいインデックスが使用されていないという問題があります。
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秒かかります。
どんな助けでも大歓迎です。よろしく。