Can cardinality differ for duplicate indexes in my

2019-02-28 06:56发布

I have a table which has duplicate indexes(same column is indexed twice (BTREE)) but surprisingly cardinality is different. Why this is happening.

'ACCENTURE_PASSIVE_CANDIDATES', '1', 'LD_INDEX', '1', 'LOCATION_DISTANCE', 'A', '37876', NULL, NULL, 'YES', 'BTREE', '', ''
'ACCENTURE_PASSIVE_CANDIDATES', '1', 'RS_INDEX', '1', 'RELEVANCY_SCORE', 'A', '21996', NULL, NULL, 'YES', 'BTREE', '', ''
'ACCENTURE_PASSIVE_CANDIDATES', '1', 'score_index', '1', 'RELEVANCY_SCORE', 'A', '146566', NULL, NULL, 'YES', 'BTREE', '', ''
'ACCENTURE_PASSIVE_CANDIDATES', '1', 'location_index', '1', 'LOCATION_DISTANCE', 'A', '172873', NULL, NULL, 'YES', 'BTREE', '', ''

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-28 07:19

Cardinality in mysql is an estimate, and mysql bases its estimate on table usage statistics:

Cardinality

An estimate of the number of unique values in the index. This is updated by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on statistics stored as integers, so the value is not necessarily exact even for small tables. The higher the cardinality, the greater the chance that MySQL uses the index when doing joins.

You can read more abot the statistics collected for myisam and innodb table engines in mysql's and innodb's documentation and how to configure these:

All statistics are stored in the STATISTICS table within information_schema.

The indexes, those estimated cardinality is closer to their exact cardinality (number of distinct values within the field) were created longer time ago, therefore mysql collected more statistics for them and its estimate is more accurate. If you run analyse table on this particular table, probably the cardinalities of the duplicate indexes will be a lot closer to each other, than now.

The huge question is, why do you have duplicate indexes at all?

查看更多
登录 后发表回答