Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the biggest ID number that autoincrement can produce in mysql

I have an database that is rapidly filled with data we talk about 10-20k rows per day.

What is a limit of an ID with and autoincrement option? If ID is created as INTEGER then I can do max value of 2,147,483,647 for unsigned values?

But what when autoincrement goes above this? Does it all collapses? What would be solution then?

I am sure that a lot of people have big databases, and I would like to hear them.

Thank you.

like image 343
user123_456 Avatar asked May 22 '12 17:05

user123_456


People also ask

What does AutoIncrement do in MySQL?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

What happens if MySQL auto increment reaches limit?

What will happen if the MySQL AUTO_INCREMENT column reaches the upper limit of the data type? When the AUTO_INCREMENT column reaches the upper limit of data type then the subsequent effort to generate the sequence number fails.

How do I find my AutoIncrement ID?

The starting value for AUTO_INCREMENT is 1, which is the default. It will get increment by 1 for each new record. To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with “id” as auto-increment.

Can we have 2 auto increment in MySQL?

You can't have two auto-increment columns.


2 Answers

If you are worried about it growing out of bounds too quickly, I would set the PK as an UNSIGNED BIGINT. That gives you a max value of 18446744073709551615, which should be sufficient.

like image 170
Dan Armstrong Avatar answered Oct 08 '22 10:10

Dan Armstrong


                    | Min. (inclusive)           | Max. (inclusive) ----------------------------------------------------------------------------- INT Signed (+|-)    |             -2,147,483,648 |              2,147,483,647 ----------------------------------------------------------------------------- INT Unsigned (+)    |                          0 |              4,294,967,295 ----------------------------------------------------------------------------- BIGINT Signed (+|-) | -9,223,372,036,854,775,807 |  9,223,372,036,854,775,806 ----------------------------------------------------------------------------- BIGINT Unsigned (+) |                          0 | 18,446,744,073,709,551,615 

MySQL reference.

If you have MySQL table with column ID (INT unsigned) with auto_increment, and the table has 4,294,967,295 records, then you try to insert 1 more record, the ID of the new record will be automatically changed and set to the max which is "4,294,967,295", so you get a MySQL error message Duplicate entry '4294967295' for key 'PRIMARY', you will have duplicated IDs if the column is set as Primary Key.

2 Possible Solutions:

  1. Easy Approach: Extend the limits, by setting the ID to BIGINT unsigned, just like what Dan Armstrong said. Although this doesn't mean it's unbreakable! and performance might be affected when the table gets really large.
  2. Harder Approach: Use partitioning, which is a little more complicated approach, but gives better performance, and truly no database limit (Your only limit is the size of your physical harddisk.). Twitter (and similar huge websites) use this approach for their millions of tweets (records) per day!
like image 35
evilReiko Avatar answered Oct 08 '22 08:10

evilReiko