[英]mysql primary key returning less results than compound composite index
我繼承了一個數據庫架構,其中存在一些設計問題
請注意,桌上還有另外9個鍵,我下面沒有列出,這些鍵看起來像
+-------+------------+----------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| users | 0 | PRIMARY | 1 | userid | A | 604 | NULL | NULL | | BTREE | | |
| users | 1 | userid_2 | 1 | userid | A | 604 | NULL | NULL | | BTREE | | |
| users | 1 | userid_2 | 2 | age | A | 604 | NULL | NULL | YES | BTREE | | |
| users | 1 | userid_2 | 3 | image | A | 604 | 255 | NULL | YES | BTREE | | |
| users | 1 | userid_2 | 4 | gender | A | 604 | NULL | NULL | YES | BTREE | | |
| users | 1 | userid_2 | 5 | last_login | A | 604 | NULL | NULL | YES | BTREE | | |
| users | 1 | userid_2 | 6 | latitude | A | 604 | NULL | NULL | YES | BTREE | | |
| users | 1 | userid_2 | 7 | longitude | A | 604 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
在具有以下字段的表中。
+--------------------------------+---------------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------------------+---------------------+------+-----+-------------------+----------------+ | userid | int(11) | NO | PRI | NULL | auto_increment | | age | int(11) | YES | | NULL | | | image | varchar(500) | YES | | | | | gender | varchar(10) | YES | | NULL | | | last_login | timestamp | YES | MUL | NULL | | | latitude | varchar(20) | YES | MUL | NULL | | | longitude | varchar(20) | YES | | NULL | | +--------------------------------+---------------------+------+-----+-------------------+----------------+
運行一條說明語句並強制其使用userid_2 ,它使用522行
mysql> describe SELECT userid, age FROM users WHERE userid
>=100 and age >27 limit 10 ;
+----+-------------+-------+-------+--------------------------------------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+--------------------------------------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | users | range | PRIMARY,users_user_ids_key,userid,userid_2 | PRIMARY | 4 | NULL | 261 | Using where |
+----+-------------+-------+-------+--------------------------------------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)
如果我不強迫它使用索引,那只是使用主鍵 ,它僅由userid組成,並且僅使用261行
mysql> describe SELECT userid, age FROM users WHERE userid
>=100 and age >27 limit 10 ; +----+-------------+-------+-------+--------------------------------------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+--------------------------------------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | users | range | PRIMARY,users_user_ids_key,userid,userid_2 | PRIMARY | 4 | NULL | 261 | Using where | +----+-------------+-------+-------+--------------------------------------------+---------+---------+------+------+-------------+ 1 row in set (0.00 sec)
該行計數僅是基於索引值分布的估計。
您有兩種選擇:
ANALYZE TABLE mytable
以重新計算分布,然后重試describe
rows
僅僅是一個估計
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.