Is there much of a speed difference for index lookups by using string
for the primary key versus the actual uuid
type, specifically if the string has a prefix like user-94a942de-05d3-481c-9e0c-da319eb69206
(making the lookup have to traverse 5-6 characters before getting to something unique)?
-
1I think the difference in speed between two string indexes with slightly different lengths is insignificant. If you really care, then add auto-increments/serial columns to your tables and use integers for the indexes.– Gordon LinoffMay 21, 2017 at 20:39
-
2Possible duplicate of PostgreSQL UUID type performance– SchwernMay 21, 2017 at 20:41
-
@GordonLinoff UUIDs are only strings on MySQL. They're stored as numbers in PostgreSQL.– SchwernMay 21, 2017 at 20:47
-
@Schwern A different, but related, question is being asked.– SteveMay 21, 2017 at 21:17
-
@Steve Ah. Umm... AFAIK MySQL doesn't have a UUID type, and I'm not sure why you wouldn't use the PostgreSQL UUID type. Could you clarify why you're sking?– SchwernMay 21, 2017 at 21:50
3 Answers
This is a micro-optimization and is unlikely to cause a real performance problem until you get to enormous scales. Use the key that best fits your design. That said, here's the details...
UUID is a built in PostgreSQL type. It's basically a 128 bit integer. It should perform as an index just as well as any other large integer. Postgres has no built in UUID generating function. You can install various modules to do it on the database, or you can do it on the client. Generating the UUID on the client distributes the extra work (not much extra work) away from the server.
MySQL does not have a built in UUID type. Instead there's a UUID function which can generate a UUID as a string of hex numbers. Because it's a string, UUID keys may have a performance and storage hit. It may also interfere with replication.
The string UUID will be longer; hex characters only encode 4 bits of data per byte so a hex string UUID needs 256 bits to store 128 bits of information. This means more storage and memory per column which can impact performance.
Normally this would mean comparisons are twice as long, since the key being compared is twice as long. However, UUIDs are normally unique in the first few bytes, so the whole UUID does not need to be compared to know they're different. Long story short: comparing string vs binary UUIDs shouldn't cause a noticeable performance difference in a real application... though the fact that MySQL UUIDs are UTF8 encoded might add cost.
Using UUIDs on PostgreSQL is fine, it's a built-in type. MySQL's implementation of UUID keys is pretty incomplete, I'd steer away from it. Steer away from MySQL while you're at it.
-
1The string version of uuid normally has 4 dashes, hence
CHAR(36)
. May 28, 2018 at 19:18 -
5PostgreSQL 13 introduced gen_random_uuid() to avoid the need to install uuid-ossp just to get a random (v4) UUID - though that module remains available if you need other versions or functions. Nov 6, 2020 at 7:57
The real problem with UUIDs comes when the table (or at least the index) is too big to be cached in RAM. When this happens, the 'next' uuid needs to be stored into (or fetch from) some random block that is unlikely to be cached. This leads to more and more I/O as the table grows.
AUTO_INCREMENT
ids usually don't suffer that I/O growth because INSERTs
always go at the 'end' of the table and SELECTs
usually cluster near the end. This leads to effective use of the cache, thereby avoiding the death-by-IO.
My UUID blog discusses how to make "Type-1" UUIDs less costly to performance, at least for MySQL.
Use the built-in UUID type that maps to a 128-bit int. Not just for performance, but to prevent strings like "password1" from showing up in that column.