Currently I want to create a SQL database effectively and "logically". Lately I read a lot about the issues that it is (mostly) not a good idea to define primary keys that can be seen from "outside" as autoincrement. And for my applications it would be a problem too/risk. As a solution I used UUIDs for this. However, I was told at the time that data retrieval (SELECT, ...) would take longer using this method. (Because there are no simple integer values to search for). <-- Please correct me, if this is wrong.
Then I had the idea to set an autoincrement primary key and a unique uuid
value. Inside the database the primary keys were used and everything that was output in the API was passed from the id value to the uuid value or vice versa.
Example:
user_id = SELECT id FROM xxx WHERE xxx = user_input (user_uuid).
SELECT name FROM xxx WHERE user_id = Value1
Instead of:
SELECT name FROM xxx WHERE user_id = user_input (user_id)
*Theoretically some things can be compressed, simplified and secured. It is only for better illustration. No explicit programming language is used.
Since I haven't done anything with databases for a while, I wonder if the longer times are true at all, and if so, if my method makes sense at all - I don't think so. So I wanted to ask what is the most sensible and easiest solution to keep the query time minimal, but not to have incrementing keys.
varbinary(16)
type). This could limit performance, if you really have that kind of scale. I wouldn't think twice about using UUIDs in more feature-rich DBs like Postgres though, modulo the typical UUID considerations like different data access patterns. – amon Feb 24 '23 at 22:27