Currently I’m trying to develop a PostgreSQL database schema, it has only 2 tables, one of which contains usernames. The difficulty is that for certain reasons I cannot store the username directly, so I have to store the hash (SHA256).
Postgres has a bytea data type, which is an array of bytes, which is technically SHA256.
My question is whether there is a better type to store the hash in terms of speed of searching whether the username exists in the database or not.
Perhaps I should look towards NoSQL solutions, where such a search is faster?
—–Add—–
The answer and comments suggest that it is optimal to use bytea
.
CREATE TABLE users (
username bytea UNIQUE NOT NULL,
somedata text NOT NULL
);
CREATE INDEX idx_username ON users USING HASH (username);
Added a hash index because I’m only interested in equality checks. Is the scheme ok?
2
Answers
It is probably a micro-optimization, but
bytea
would use significantly less space and compare faster.If you end up using
text
(avoidcharacter
), make sure that you are using theC
collation:Other collations will result in much more expensive comparisons.
Try it. As you suspected and as already confirmed in comments and the answer from @Laurenz Albe,
bytea
wins by being the most compact and fastest to look up.Here’s how storing 400k hashes compares between these:
And here’s how much time it takes to look one up:
You can’t be
using hash
for unique indexes, so you’d need to maintain both that and the separate unique index that gets created to handle theUNIQUE
constraint on yourusername
column. I’d just stick with the default unique you already have.