15

Is there a max size of a UNIQUE index in MySQL? I have six dimensions in my index, which leads to a massive index. Is there a cardinality limit to UNIQUE index?

I am getting weird unexplained errors and wonder if there is a limit from the UNIQUE. Could it come from a uniqueness limit? Here is what it looks like (I also don't understand why the cardinality of the last three columns does not increase).

+-------+------------+----------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table |          0 | PRIMARY  |            1 | column_1      | A         |          15 |     NULL | NULL   |      | BTREE      |         |               |
| table |          0 | PRIMARY  |            2 | column_2      | A         |       91948 |     NULL | NULL   |      | BTREE      |         |               |
| table |          0 | PRIMARY  |            3 | column_3      | A         |      924889 |     NULL | NULL   |      | BTREE      |         |               |
| table |          0 | PRIMARY  |            4 | column_4      | A         |    15723114 |     NULL | NULL   |      | BTREE      |         |               |
| table |          0 | PRIMARY  |            5 | column_5      | A         |    15723114 |     NULL | NULL   |      | BTREE      |         |               |
| table |          0 | PRIMARY  |            6 | column_6      | A         |    15723114 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5
  • 2
    You have six unique constraints on a single table?! That could be an indication there's something wrong about the design ;-)
    – Ja͢ck
    Commented May 15, 2013 at 14:46
  • How many rows does your table currently have? Is it MyISAM or InnoDB?
    – Jocelyn
    Commented May 15, 2013 at 14:49
  • Yes, agreed. I will fix that by enforcing uniqueness elsewhere. However, it raised a point that I am curious about about index max size.
    – Mad Echet
    Commented May 15, 2013 at 14:50
  • This isn't 6 unique constraints. It is one constraint that spans 6 columns. I'm not sure enforcing uniqueness elsewhere is the answer. The database is the appropriate place to do it. Maybe if you post your table definition we could get a better idea of what you are trying to do. Commented May 15, 2013 at 14:58
  • Thanks, I am trying to aggregate logs with six dimensions. Since the whole process is quite long with a bunch of cron scripts (syslogs -> hadoop -> hive (to aggregate by dimension) -> sqoop -> mysql), I want to make sure I don't add the same line twice, hence the 6-column uniqueness. All values are INT, TINYINT or FLOAT so I am pretty far from the limits explained by @svidgen.
    – Mad Echet
    Commented May 15, 2013 at 15:08

1 Answer 1

21

For InnoDB tables, the limit is 3072 bytes across all indexed columns, presumably taking only the first 767 bytes of each column.

An index key for a single-column index can be up to 767 bytes. The same length limit applies to any index key prefix. See Section 13.1.13, “CREATE INDEX Syntax”.

The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to 3072 bytes. This limit applies to the length of the combined index key in a multi-column index.

http://dev.mysql.com/doc/refman/5.1/en/innodb-restrictions.html

So, based strictly on the documentation, I'd say you could have up to 1000 columns in your index (the max number of columns on an InnoDB table), provided that the average size of each is 3 bytes or less.

For MyISAM tables, it's the lesser of 16 columns or 1000 bytes.

The maximum number of columns per index is 16.

The maximum key length is 1000 bytes. This can also be changed by changing the source and recompiling. For the case of a key longer than 250 bytes, a larger key block size than the default of 1024 bytes is used.

http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html

3
  • It actually says "Arbitrary length UNIQUE constraints"
    – Tom
    Commented Nov 29, 2013 at 22:53
  • 2
    @Tom Interesting ... I'd be interested for a fuller explanation of what that means. In most contexts, arbitrary doesn't generally mean "with disregard for the limits" but rather "any value of whim within the limits."
    – svidgen
    Commented Nov 30, 2013 at 2:23
  • Nowadays InnoDB also limits to 16 columns: dev.mysql.com/doc/refman/8.0/en/innodb-limits.html.
    – Tomty
    Commented Oct 6, 2020 at 11:00

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.