문제

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 INDEXES shows 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.

다른 팁

Your understanding is wrong. Multi column key is created from multiple values, not for multiple columns. MySQL reports 4 unique entries because you entered 4 unique pairs of entries.

You have described separate indexes for these columns, so instead

KEY `with_what` (`with`,`what`)

it should be

KEY `with` (`with`),
KEY `what` (`what`)

And it should work as you wanted it to.

you should think of a "multi column" index as a "concatenated index", meaning the individual columns are concatenated in the index. That is, there is one index entry for each row, and that entry contains all indexed columns.

Have a look at my eBook to get the full picture how indexes work: http://Use-The-Index-Luke.com/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top