不久前我开始优化mysql db的查询。我创建了一些索引并决定使用 show index from 语法来查看它们的参数。并且看到基数不等于某些列的明显计数。
所以,我有桌子
CREATE TABLE `item_owners` (
`uid` varchar(255) NOT NULL,
`version` bigint(20) NOT NULL,
`type_id` varchar(255) NOT NULL,
`owner_name` varchar(255) NOT NULL,
`item_id` varchar(255) NOT NULL,
`status_id` varchar(255) NOT NULL,
PRIMARY KEY (`uid`),
KEY `FK181EADBC7346EE24` (`status_id`),
KEY `FK181EADBC90094D43` (`type_id`),
KEY `FK181EADBC499E38CA` (`item_id`),
CONSTRAINT `FK181EADBC499E38CA` FOREIGN KEY (`item_id`) REFERENCES `items` (`uid`),
CONSTRAINT `FK181EADBC7346EE24` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`uid`),
CONSTRAINT `FK181EADBC90094D43` FOREIGN KEY (`type_id`) REFERENCES `types` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
在那张表中我有2M行(实际上是2M uniq PK),500k uniq item_id,5 uniq status_id和20 uniq type_id
但是当我执行
时show index from item_owners
我有
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| item_owners | 0 | PRIMARY | 1 | uid | A | 1893427 | NULL | NULL | | BTREE | | |
| item_owners | 1 | FK181EADBC7346EE24 | 1 | status_id | A | 17 | NULL | NULL | | BTREE | | |
| item_owners | 1 | FK181EADBC90094D43 | 1 | type_id | A | 17 | NULL | NULL | | BTREE | | |
| item_owners | 1 | FK181EADBC499E38CA | 1 | item_id | A | 148000 | NULL | NULL | | BTREE | | |
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
然后我决定执行
analyze table item_owners
此输出后:
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| item_owners | 0 | PRIMARY | 1 | uid | A | 2005419 | NULL | NULL | | BTREE | | |
| item_owners | 1 | FK181EADBC7346EE24 | 1 | status_id | A | 17 | NULL | NULL | | BTREE | | |
| item_owners | 1 | FK181EADBC90094D43 | 1 | type_id | A | 17 | NULL | NULL | | BTREE | | |
| item_owners | 1 | FK181EADBC499E38CA | 1 | item_id | A | 2005419 | NULL | NULL | | BTREE | | |
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
任何人都知道怎么可能?,因为我真的误解了为什么基数不等于uniq行的数量,即使对于PK索引也是如此。
答案 0 :(得分:1)
引用the manual:
<强>基数强>
索引中唯一值数量的估计值。这是 通过运行
ANALYZE TABLE
或myisamchk -a
进行更新。基数是 根据存储为整数的统计信息计算,因此值不是 即使对于小桌子也必须完全。