132

What is cardinality in MySQL? Please explain in simple, non-technical language.

If a index detail of any table displays the cardinality of a field say group_id as 11, then what does that mean?

8 Answers 8

147

Max cardinality: All values are unique

Min cardinality: All values are the same

Some columns are called high-cardinality columns because they have constraints in place (like unique) prohibiting you from putting the same value in every row.

Cardinality is a property which affects the ability to cluster, sort and search data. It is therefore an important measurement for the query planners in DBs, it is a heuristic which they can use to choose the best plans.

9
  • 16
    What's with this fetish for big words. "Distinctness" would work fine isn't it?
    – Pacerier
    Commented Feb 1, 2015 at 23:14
  • 15
    @Pacerier: Yes, although I think that people working on databases are already quite familiar with relational theory, set theory and mathematics. And they borrowed the term from set theory: en.m.wikipedia.org/wiki/Cardinality Commented Jul 17, 2015 at 12:33
  • 41
    @Pacerier, Distinctness is a bigger word (by 1)
    – Drew
    Commented Sep 14, 2015 at 22:17
  • 4
    @Drew, ;) not if comparing lexemes.
    – Pacerier
    Commented Sep 17, 2015 at 7:28
  • 8
    @Pacerier Lexeme? "Distinctness" would work there too, wouldn't it? ;) If we work hard enough, we can reduce the whole language down to just 1 word and express ourselves through varying repetition and pauses.
    – Jason
    Commented May 19, 2016 at 19:49
38

Wikipedia summarizes cardinality in SQL as follows:

In SQL (Structured Query Language), the term cardinality refers to the uniqueness of data values contained in a particular column (attribute) of a database table. The lower the cardinality, the more duplicated elements in a column. Thus, a column with the lowest possible cardinality would have the same value for every row. SQL databases use cardinality to help determine the optimal query plan for a given query.

0
36

It is an estimate of the number of unique values in the index.

For a table with a single primary key column, the cardinality should normally be equal to the number of rows in the table.

More information.

2
  • 4
    That should be "will always be equal to..." and not "should normally be equal to..."!
    – Vérace
    Commented Mar 24, 2021 at 19:56
  • 2
    In my testing, value of cardinality of primary key is not the same as count(*) for large table.
    – John Wong
    Commented Dec 23, 2022 at 6:01
16

It's basically associated with the degree of uniqueness of a column's values as per the Wikipedia article linked to by Kami.

Why it is important to consider is that it affects indexing strategy. There will be little point indexing a low cardinality column with only 2 possible values as the index will not be selective enough to be used.

1
  • 6
    The second paragraph is important if you try to understand when it makes sense to index a column.
    – VMC
    Commented Jun 12, 2017 at 14:53
16

The higher cardinality, the better is differentiation of rows. Differentiation helps navigating less branches to get data.

COLUMN     DISTINCT VALUES     CARDINALITY
A          8,9                 2
B          22, 13, 24, 98      4

Therefore higher cordinality values mean:

  • better performance of read-queries;
  • bigger database size;
  • worse performance of write-queries, because hidden index data is being updated.
2
  • Don't get why there should be "worse performance of write-queries, because hidden index data is being updated."?
    – Vérace
    Commented Mar 24, 2021 at 19:58
  • Yupp. More job needs to be done to synchronize things.
    – Zon
    Commented Mar 25, 2021 at 5:39
12

In mathematical terms, cardinality is the count of values in a set of values. A set can only contains unique values. An example would be the set "A".

Let the set "A" be: A={1,2,3} - the cardinality of that set is |3|.

If set "A" contains 5 values A={10,21,33,42,57}, then the cardinality is |5|.

SET     VALUES           Cardinality
A       1,2,3                 3
B       10,21,33,42,57        5

What that means in the context of MySQL is that the cardinality of a table column is the count of that column's unique values. If you are looking at the cardinality of your primary key column (e.g. table.id), then the cardinality of that column will tell you how many rows that table contains, as there is one unique ID for each row in the table. You don't have to perform a "COUNT(*)" on that table to find out how many rows it has, simply look at the cardinality.

6

From the manual:

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.

And an analysis from Percona:

CREATE TABLE `antest` (
  `i` int(10) unsigned NOT NULL,
  `c` char(80) default NULL,
  KEY `i` (`i`),
  KEY `c` (`c`,`i`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

mysql> select count(distinct c) from antest;
+-------------------+
| count(distinct c) |
+-------------------+
|               101 |
+-------------------+
1 row in set (0.36 sec)


mysql> select count(distinct i) from antest;
+-------------------+
| count(distinct i) |
+-------------------+
|               101 |
+-------------------+
1 row in set (0.20 sec)

mysql> select count(distinct i,c) from antest;
+---------------------+
| count(distinct i,c) |
+---------------------+
|               10201 |
+---------------------+
1 row in set (0.43 sec)

mysql> show index from antest;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| antest |          1 | i        |            1 | i           | A         |        NULL |     NULL | NULL   |      | BTREE      |         |
| antest |          1 | c        |            1 | c           | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
| antest |          1 | c        |            2 | i           | A         |        NULL |     NULL | NULL   |      | BTREE      |         |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.00 sec)

mysql> analyze table sys_users;
+--------------------------------+---------+----------+----------+
| Table                          | Op      | Msg_type | Msg_text |
+--------------------------------+---------+----------+----------+
| antest                         | analyze | status   | OK       |
+--------------------------------+---------+----------+----------+
1 row in set (0.01 sec)


mysql> show index from antest;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| antest |          1 | i        |            1 | i           | A         |         101 |     NULL | NULL   |      | BTREE      |         |
| antest |          1 | c        |            1 | c           | A         |         101 |     NULL | NULL   | YES  | BTREE      |         |
| antest |          1 | c        |            2 | i           | A         |       10240 |     NULL | NULL   |      | BTREE      |         |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.01 sec)
2
  • 1
    This answer contain zero content that you have written yourself, it's just a collage from the MySQL manual and the blog post you linked to. And horribly formatted on top of that.
    – Siguza
    Commented Apr 9, 2017 at 2:38
  • 1
    Now it clearly says the sources and it's better formatted.
    – carla
    Commented Aug 4, 2017 at 20:08
5

In a simple way, cardinality is the number of rows or tuples within the table. No. of columns is called "degree"

1
  • Not true - you could have millions of rows and a cardinality of 1 (all values the same) or millions (up to the no. of rows in the table if each row value was unique)!
    – Vérace
    Commented Mar 24, 2021 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.