Can cardinality differ for duplicate indexes in mysql?

China☆狼群 提交于 2019-12-02 11:28:55

问题


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:


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:

  • InnoDB and MyISAM Index Statistics Collection
  • Controlling Optimizer Statistics Estimation

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?



来源:https://stackoverflow.com/questions/34851674/can-cardinality-differ-for-duplicate-indexes-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!