I have table containing a series of unique strings which I need to provide quick lookups to (besides for memory-caching).
@Entity
public class UniqueString {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Basic
@NaturalId
private String stringId;
}
Initially I had the stringId variable as @Id, however it turned out that some databases (e.g., oracle) went into full table scan when retrieving the object by string id; hence I switched to a long instead.
How can I quickly access the UniqueString object from a string stringId. I see two solutions so far:
- Again, annotate
stringIdwith@idand try to find a fix to whyfull table scansoccur in some databases - Hash the string into a
long(while loosing precision) and use a lookup table to return all objects matching the hash, then compare theirstringIdproperty for equality to find the match we're looking for, e.g, :
.
LookupTable UniqueString
+----+------+ +----+----+----------+
|hid | hash | | id |hid | stringId |
+----+------+ +----+----+----------+
| 1 | 123 | -------------> | .. | 1 | .... |
| 2 | 321 | `-----> | .. | 1 | .... |
+----+------+ +----+----+----------+
Opinions, suggestions?
[EDIT] Ok I realize that my above table illustration could simply be normalized to
UniqueString
+----+-----+----------+
| id |hash | stringId |
+----+-----+----------+
| .. | 123 | .... |
| .. | 123 | .... |
| .. | 321 | .... |
+----+-----+----------+
This makes all the difference as I suspect both of the following queries would perform roughly the same:
from UniqueString where hash='123'from UniqueString where stringId='abc'
make sure there is an index on the stringId colum in the database
just query the database using hql or criteria API
configure your query cache to cache this kind query.