snippetsqlMinor
Does `SERIAL PRIMARY KEY` create two indexes on the table?
Viewed 0 times
theprimarycreateindexesserialtwodoestablekey
Problem
Since
There's definitely the wtf factor of seeing both
Should one avoid using
SERIAL is shorthand for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE, it looks like SERIAL PRIMARY KEY doubles up on the UNIQUE and PRIMARY KEY:> CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY);
> SHOW CREATE TABLE foo \G
*************************** 1. row ***************************
Table: foo
Create Table: CREATE TABLE `foo` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)There's definitely the wtf factor of seeing both
PRIMARY KEY and UNIQUE KEY id in SHOW CREATE TABLE, but is that really two physical indexes I'm seeing?Should one avoid using
PRIMARY KEY with SERIAL and instead opt for writing bigint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, or using SERIAL with no primary keys on the table?Solution
I'm also seeing this behavior in 5.7.18 MySQL Community Server.
Based on the output from
Edit: This also happens for normal tables:
Based on the output from
SHOW INDEX FROM foo there is a duplicate index being created and maintained:mysql> CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY);
Query OK, 0 rows affected (0.00 sec)
mysql> show index from foo;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| foo | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| foo | 0 | id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)Edit: This also happens for normal tables:
mysql> CREATE TABLE foo (id SERIAL PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)
mysql> show index from foo;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| foo | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| foo | 0 | id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where TABLE_NAME = 'foo';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| def | my_db | PRIMARY | my_db | foo | PRIMARY KEY |
| def | my_db | id | my_db | foo | UNIQUE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
2 rows in set (0.00 sec)Code Snippets
mysql> CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY);
Query OK, 0 rows affected (0.00 sec)
mysql> show index from foo;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| foo | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| foo | 0 | id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)mysql> CREATE TABLE foo (id SERIAL PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)
mysql> show index from foo;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| foo | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| foo | 0 | id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where TABLE_NAME = 'foo';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| def | my_db | PRIMARY | my_db | foo | PRIMARY KEY |
| def | my_db | id | my_db | foo | UNIQUE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
2 rows in set (0.00 sec)Context
StackExchange Database Administrators Q#183719, answer score: 2
Revisions (0)
No revisions yet.