It is a bad idea. In addition to what @blispr points out, scaling is a serious problem. This also shows up when using UUIDs and GUIDs.
When a key (PRIMARY KEY
or otherwise) is 'random' (as UUIDs, hashes, etc, are), the 'next' lookup (for INSERT
or SELECT
) will be at some 'random' place in the Index (and/or table). When the table is huge, this means that the necessary block is unlikely to be in cache. In the extreme (index size much larger than cache), the cache will be thrashed and you will need about one disk hit per read or write operation. On conventional drives that is something like 100 hits per second. This won't be enough for huge tables.
So, you read somewhere to "keep the PRIMARY KEY
small? Do it when convenient; don't go out of your way. Take, for example, "country codes". There are fewer than 256 countries, so you might be tempted to use TINYINT UNSIGNED
, which takes 1 byte. I argue for using the standard 2-letter codes and CHAR(2) CHARACTER SET ascii
, which takes 2 bytes. Simpler, more readable, and not enough bigger to matter.
Edit
An AUTO_INCREMENT
is often (but not always) better because it is "chronological". That is, 'old' entries have small ids and are at one end of the table/index; 'new' entries are at the other end. In many applications, most of the activity is with the 'new' entries, therefore they tend to be cached while the 'old' entries stay on disk, unmolested.
Regardless of whether the PRIMARY KEY
for my row is 'RickJames' or 12345 or '827ccb0eea8a706c4c34a16891f84e7b', I don't see much difference in "security". Don't confuse "obscurity" and "security".
On the other hand, if my id is 12345, a hacker can easily assume that 12346 and 12347 are probably valid ids, and could try to fetch their info. If that is your concern, then continue to use 12345, but also have some random value (not derivable from 12345) as a secondary value for validating that the id is not hacked. Store that value in the db for testing; you don't need to index it. (Hence, it won't hit my previous comments.) Furthermore, ids, security codes, etc, are probably best passed through cookies, not urls.