I'm building an app aimed to store lots of records (millions) and trying to find a best way to store records identifiers that is publicly accessible from the web (record URL).
I'd like to get:
- Random, probably unique, unpredictable IDs that is reasonably short and possible to use in an URL, meaning UUID will not suit as it is too long.
- Best performance from database and reduce storage space.
- Avoid creating auto increment id with the only purpose to use it as a PRIMARY key.
- ...Avoid future replication issues?
My first thought was to generate a random string like 4r5psPxuRw
and use it as a primary key (CHAR(10)). But in this case index will be 30 bytes per record and I assume that with the large amount of records INSERT performance will suffer.
Now I came to a random numeric string, for example 16 digits from 1 to 9, and store it as BIGINT(16).
Is this a good way to achieve security, speed and reduce number of surrogate keys? Am I missing something?
unpredictable
? Is it (1) hard to guess next record for given user OR (2) hard to index all keys in your system by brute force enumeration?CHAR(10)
can be just ASCII, no need for utf, so 10 bytes actually.. Whats your reason for not using auto-increment or other surrogate key? Using it internally and having uniquehandle
with the random identifier will give you good primary key with appending and good unique access for single row lookups (or do you expect to fetch hundreds of rows per user request by the public handle?)SELECT * FROM t WHERE comment_id IN (1,2,3)
useSELECT * FROM t WHERE comment_uid IN (1425889898813533, 9892597754837953, 3721461664138416)
. Why do I need auto incrementingcomment_id
then?