MySQL: low cardinality/selectivity columns = how to index?

前端 未结 7 1271
既然无缘
既然无缘 2020-12-04 15:31

I need to add indexes to my table (columns) and stumbled across this post:

How many database indexes is too many?

Quote: “Having said that, you can clearly

7条回答
  •  不思量自难忘°
    2020-12-04 16:10

    @a'r answer is correct, however it needs to be pointed out that the usefulness of an index is given not only by its cardinality but also by the distribution of data and the queries run on the database.

    In OP's case, with 150M records having status='enabled' and 150M having status='disabled', the index is unnecessary and a waste of resource.

    In case of 299M records having status='enabled' and 1M having status='disabled', the index is useful (and will be used) in queries of type SELECT ... where status='disabled'.
    Queries of type SELECT ... where status='enabled' will still run with a full table scan.

提交回复
热议问题