Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Which one is faster to have database key as String or as Long?</p> </blockquote> <p>It is very very likely that a long is faster than a String. For one thing, processors can deal with values of type long "naturally", while a String has to be manipulated via software. Also, a string will likely occupy more memory than a long value which means the caches are under more stress and more data has to be loaded from / saved to disk.</p> <blockquote> <p>If the difference is micro seconds, it does not matter to our application. The table has thousands records/rows.</p> </blockquote> <p>So it depends on the operations. If you</p> <blockquote> <p>If Long is faster, how to convert a String into Long, are there some Java API so that unique String could be converted to unique long?</p> </blockquote> <p>You can derive an int from a String using it's <a href="http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html#hashCode%28%29" rel="nofollow">hash code</a>. This the ints you will get from this method will be well distributed within the allowed range, but are not guaranteed to be unique. Generally it is not possibly to derive a unique int from a string, because there are strictly more strings than long.</p> <p>Just imagine you build a giant table holding the decimal representation of all long values. Now there is a trivial long &lt;-> string mapping between the two. But this table still does not contain the string "hello world", and there are no more long's left you could use to represent this string.</p> <p>Generally my advice is: If your source data is <em>naturally</em> of type string, use that and let the database do the optimizations. A table holding "thousands" of entries won't be a problem for any current database. Maybe you can help performance by creating clever indexes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload