views:

153

answers:

3

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?

+3  A: 

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.

Jon Skeet
+2  A: 

"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.

S.Lott
+1  A: 

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.

MrZebra