Background:
We wish to be able to match on these strings quickly in a query without the performance hit of doing lots of joins.
So I am thinking of storing a hash code of all these strings in the main table and including it in our index, so the joins are only processed by the database when the hash code matches.
So how do I get a good hashcode? I could:
So what do people think?
In the end I just concatenate the strings and compute the hashcode for the concatenation, as it is simple and worked well enough.
(If you care we are using .NET and SqlServer)
Bug!, Bug!
Quoting from Guidelines and rules for GetHashCode by Eric Lippert
The documentation for System.String.GetHashCode notes specifically that two identical strings can have different hash codes in different versions of the CLR, and in fact they do. Don't store string hashes in databases and expect them to be the same forever, because they won't be.
So String.GetHashcode() should not be used for this.
Standard java practise, is to simply write
final int prime = 31;
int result = 1;
for( String s : strings )
{
result = result * prime + s.hashCode();
}
// result is the hashcode.