I'm storing UUID v4 values in a PostgreSQL v9.4 table, under column "id".
When I create the table, is there any difference in following write or read performance whether I define the "id" column as VARCHAR(36), CHAR(36), or UUID data type?
Thanks!
I'm storing UUID v4 values in a PostgreSQL v9.4 table, under column "id".
When I create the table, is there any difference in following write or read performance whether I define the "id" column as VARCHAR(36), CHAR(36), or UUID data type?
Thanks!
Use uuid
. PostgreSQL has the native type for a reason.
It stores the uuid internally as a 128-bit binary field. Your other proposed options store it as hexadecimal, which is very inefficient in comparison.
Not only that, but:
uuid
does a simple bytewise sort for ordering. text
, char
and varchar
consider collations and locales, which is nonsensical for a uuid.
There is only one canonical respresentation of a uuid
. The same is not true for text etc; you have to consider upper vs lower case hex, presence or absence of {...-...}
s etc.
There's just no question. Use uuid
.
The only other type that makes any sense is bytea
, which at least can be used to store the 16 bytes of the uuid directly. This is what I'd do if I was using systems that couldn't cope with data types outside the basic set, like a really dumb ORM of some kind.
UUID would be the fastest because its 128 bits -> 16 bytes and comparisons are done numerically.
Char(36) and varchar(36) seems to be the same and slow: http://www.depesz.com/2010/03/02/charx-vs-varcharx-vs-varchar-vs-text/.
The server should check EOF to determine the job of reading the value has finished or not for each character.
Also text comparison is slower than numerical comparison. And because UUID consists of 16 bytes, comparing UUID is much faster than comparing two texts of 36 characters.
Use native UUID for performance.
char
and varchar
here, at least as stored by PostgreSQL. Both are stored as VARLENA
internally. Also, a UUID is 16 bytes (128 bits); I don't know where you get "5 hexadecimal values". –
Parnassus The index size is maybe the most notable difference: almost 86% more for VARCHAR.
From a performance perspective I didn't notice significant differences in PostgreSQL 9.5.
© 2022 - 2024 — McMap. All rights reserved.
char(36)
orvarchar(36)
would use 36 bytes. I can't seem to find where that's documented anymore, however. Beyond that, you need to be aware of the ramifications of how PostgreSQL does table/index clustering if your UUID is going to be your key and you plan on using the CLUSTER option. – Davedaveda