开发者

Hibernate quick way to lookup unique string in table

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 stringId with @id and try to find a fix to why full table scans occur 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 their stringId property 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 |h开发者_如何学Goash | 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'


  1. make sure there is an index on the stringId colum in the database

  2. just query the database using hql or criteria API

  3. configure your query cache to cache this kind query.


This is more about how you keep the said column in the database. I reckon the right way is to define a hash index over the column and mark the field in the entity as index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜