Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference between UUID, CHAR, and VARCHAR in PostgreSql table?

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!

like image 256
Pensierinmusica Avatar asked Aug 24 '15 18:08

Pensierinmusica


People also ask

Is VARCHAR faster than TEXT Postgres?

Difference Between PostgreSQL TEXT and VARCHAR Data Types Both TEXT and VARCHAR have the upper limit at 1 Gb, and there is no performance difference among them (according to the PostgreSQL documentation).

Is UUID good for primary key Postgres?

You can use UUID as primary key in your table as it will be unique. However do keep in mind that UUID will occupy a bit more space as compared to SEQUENCE. And also they are not very fast. But yes they are for sure unique and hence you are guaranteed to get a consistent data.

What is the difference between VARCHAR and char in PostgreSQL?

The CHAR is fixed-length character type while the VARCHAR and TEXT are varying length character types. Use VARCHAR(n) if you want to validate the length of the string ( n ) before inserting into or updating to a column. VARCHAR (without the length specifier) and TEXT are equivalent.

Does PostgreSQL support UUID?

PostgreSQL allows you store and compare UUID values but it does not include functions for generating the UUID values in its core. Instead, it relies on the third-party modules that provide specific algorithms to generate UUIDs.


3 Answers

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.

like image 122
Craig Ringer Avatar answered Oct 12 '22 01:10

Craig Ringer


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.

like image 27
Abdullah Nehir Avatar answered Oct 11 '22 23:10

Abdullah Nehir


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.

like image 33
johnlemon Avatar answered Oct 12 '22 01:10

johnlemon