I just had a showerthought that the default Identity Seed is 1. I have some tables that I know will grow to the billions at a certain point. Wouldn't it make more sense to start on int.Min
( -2,147,483,648) for these tables?
This could just make the difference of migrating your key to bigint
in 4 years or in 8 years. Can be relevant enough.
Is this common? It feels weird. Is there anything I am missing?
Best Answer
There is nothing at all wrong with starting at -2,147,483,648 as far as SQL Server is concerned. Starting at 2,147,483,647 and counting backwards with
IDENTITY(2147483647,-1)
is perfectly valid too.Things that would make me be wary of doing so:
WHERE (x.a<>y.a OR x.a IS NULL AND y.a IS NOT NULL OR x.a IS NOT NULL AND y.a IS NULL)
toWHERE ISNULL(x.a,-1)<>ISNULL(x.a,-2)
is something I've seen numerous times. Not likely for your PK as that'll never beNULL
but might happen in comparing FK values in other tables. I've even seen someone usesomevalue>-1
in place ofsomevalue IS NOT NULL
(apparently there is a circumstance where that is more efficient, though he never explained to my satisfaction what the circumstance might be!). There might be that and other odd shenanigans in code outside the DB too.If I expect hitting ~400,000,000 is ever going to be likely in the lifetime of the data, far before the difference between 2 or 4 000,000,000 is a consideration, I'm already going to go for a larger key so doubling up by using negative values isn't going to make much difference.
You don't want to be making a change to something core like a PK in four years time, but you even less want to do it in eight years time. In both cases if the design lasts that long you've long forgotten details and many other bits & bobs have started to depend on the key so the changes needing to be made grow massively, and even that single table is going to be massive work to change (unless most of the data gets deleted after a time) as it contains that many rows, then you have all those that refer to it with FKs to work on also.