tags:

views:

88

answers:

2

I have a django app that uses MySQL as the database backend. It's been running for a few days now, and I'm up to ID 5000 in some tables already.

I'm concerned about what will happen when I overflow the datatype.

Is there anyway to tell the auto increment to start over at some point? My data is very volatile, so when I do overflow the ID, there is no possible way that ID 0, or anywhere near that will still be in use.

+3  A: 

Depending on whether you're using an unsigned integer or not and which version of MySQL you're running, you run the rink of getting nasty negative values for the primary key or (worse) the row simply won't be inserted and will throw an error.

That said, you can easily change the size/type of the integer in MySQL using an ALTER command to preemptively stop this from happening. The "standard" size for an INT being used as a primary key is an INT(11), but the vast majority of DB applications don't need anything nearly that large. Try a MEDIUMINT.

MEDIUMINT - The signed range is –8388608 to 8388607. The unsigned range is 0 to 16777215

As compared to....

INT or INTEGER - The signed range is –2147483648 to 2147483647. The unsigned range is 0 to 4294967295

There's also the BIGINT, but to be honest you've probably got much larger scalability issues than your data types to worry about if you have a table with > 2 billion rows :)

AvatarKava
Is the row not being inserted with an erro really "worse"? I'd rather have the system fail fast than roll over and potentially (one day) overwrite ID values.
Calum
A fair point - it depends on how crucial that data is .. if it's truly volatile as the OP claims, the rollover probably is the lesser of two evils. Either way, the issue is pretty easily avoided.
AvatarKava
A: 

Well, the default 32bit INT goes up to about 2 billion. At 5000 IDs per day, that's about 1000 years till overflow. I don't think you have to worry yet...

Michael Borgwardt
This does not directly answer the question.
slypete