I have a table which has duplicate indexes(same column is indexed twice (BTREE)) but surprisingly cardinality is different. Why this is happening.
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?