Consider following MySQL table:
CREATE TABLE `log`
(
`what` enum('add', 'edit', 'remove') CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`with` int(10) unsigned NOT NULL,
KEY `with_what` (`with`,`what`)
) ENGINE=InnoDB;
INSERT INTO `log` (`what`, `with`) VALUES
('add', 1),
('edit', 1),
('add', 2),
('remove', 2);
As I understand, with_what index must have 2 unique entries on its first with level and 3 (EDIT: 4) unique entries in what “subindex”. But MySQL reports 4 unique entries for each level. In other words, number of unique elements for each level is always equal to number of rows in log table.
EDIT: It is okay for the “second level” to have number of unique entries equal to total number of records, but not okay for top level.
EDIT2: Have noticed if number of a bits occupied by with column changed, for instance to int(11) and back to int(10), then cardinality start working as expected. Even EXPLAIN SELECT COUNT(DISTINCT 'with') FROM log display adequate value for rows.
Is that a bug, a feature or my misunderstanding?
SHOW INDEXESshows statistics which are approximate.These statistics are gathered automatically as the queries to the table are performed, and additionally, you can force gathering them manually by issuing
ANALYZE TABLE log.The value in the cardinality column is not exact and it can change between calls to
ANALYZE, even if the underlying table is not changed.