Yuval Adam
Yuval Adam

Reputation: 165242

Should key values in a database table be hashed?

Suppose a database table has a column "Name" which is defined as key for the table. Usual name values will be "Bill", "Elizabeth", "Bob", "Alice". Lookups on the table will be done by the name key as well.

Does hashing the values optimize the operations in any way? i.e. entering each name as some hashed value of the name (suppose MD5 - 32 bits).

If so - shouldn't this be a feature of the database and not something the client handles?

Upvotes: 2

Views: 329

Answers (3)

Zebra North
Zebra North

Reputation: 11492

No, don't hash them. Your database will build an index based on the data, and hashing won't help. The only time it might help is if your key values were much longer than the hash.

Upvotes: 1

S.Lott
S.Lott

Reputation: 391862

"Does hashing the values optimize the operations in any way? " Not really.

Hashes are one-way. You can't do a table scan and reconstruct the original name.

If you want to keep both name and hash-of-name, you've broken a fundamental rule by including derived data. Now a name update requires a hash update.

The "spread my values around evenly" that would happen with a hash, is the job of an index.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500873

Assuming your database generates an index for the primary key (and I can't imagine it wouldn't) it's doing it for you. So yes, it should absolutely be something that the database handles.

Upvotes: 3

Related Questions