Re: Hashes from composite keys?

From: Clifford Heath <no_at_spam.please.net>
Date: Mon, 26 Jul 2010 20:27:56 +1000
Message-ID: <4c4cd64f$0$3031$afc38c87_at_news.optusnet.com.au>


Karsten Wutzke wrote:
>> what are the best practices for generating hash codes from composite
>> keys? I need to mimic something like what a composite index does. In
>> fact, it's for mapping between relational keys and object IDs.
> OK one last try. Maybe someone has a good tip where hashing algorithms > are discussed?

Karsten,

You're missing the point here. You cannot "identify" your objects using any kind of general hash code. All general hash codes will (in fact, *must*) sometimes map two different key values into the same hash code. This breaks identity; the same hash code tries to "identify" more than one object. It cannot be avoided because the hash code has fewer bits (so fewer combinations) than your unknown keys have - there aren't enough has codes to go around.

The only way a hash code can be useful is where it provides a 1:1 mapping. If your entire key population is known and static, you should google for "perfect hash function". If it's not static and known in advance, you might consider building a table of all keys, and assigning a sequential identifier to the entries in the table. That is, construct a 1:1 mapping from the data. Most DBMS will do that for you. Don't forget to consider what happens when you delete, and perhaps subsequently re-use, a key value.

Clifford Heath. Received on Mon Jul 26 2010 - 12:27:56 CEST

Original text of this message