论坛首页 Java企业应用论坛

数据库性能优化,关于oracle字符串比较优化的改进方案,大家拍砖

浏览 7208 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-12-10  

  因为公司的单个业务数据达到千W级别,并且有源源不断的新数据进来。新数据进来都需要进行查重,重复数据不能进来,查重条件有很多字符串的对比,最大的字符串不超过1000个字符,但是字符串的比较,对于数据库来说,非常的耗性能,如果能将String转成数字来进行比较对于性能的提高将非常有用。

    后来想到String有一个hashcode,看看能否使用:

 

    <SPAN style="FONT-SIZE: small">/**
     * Returns a hash code for this string. The hash code for a
     * <code>String</code> object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
     * <i>i</i>th character of the string, <code>n</code> is the length of
     * the string, and <code>^</code> indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
	int h = hash;
        int len = count;
	if (h == 0 && len > 0) {
	    int off = offset;
	    char val[] = value;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }</SPAN>

 

     但是遗憾的是int的范围非常窄(-2147483648——2147483647),出现重复的概率虽然说比较低,但是不可避免会出现重复的可能性。

      于是想到,是否可以自己重新写过hashcode方法,将int的范围扩大到long类型(-9223372036854774808~9223372036854774807),这样在目前长度的字符生成的hashcode出现重复的可能性应该几乎为零。

      大家对于这个设想是否有什么更好的意见?

   发表时间:2011-12-10  
MD5/SHA1
0 请登录后投票
   发表时间:2011-12-10  
kimmking 写道
MD5/SHA1

MD5,并不是数字类型,还是字符串呀
0 请登录后投票
   发表时间:2011-12-10  
hash(string+string.length)
0 请登录后投票
   发表时间:2011-12-14  
hashcode相等并不代表字串相等,只能保证字串相等hashcode相等
0 请登录后投票
   发表时间:2011-12-14  
搞个MD5匹配下就好了
0 请登录后投票
   发表时间:2011-12-14  
1. 可以加一列 用于保存md5的值
2. 在上面的列 加上索引
3  新来的字符串 md5 后的值 走索引去比较
4. 再来确定是否接受新来的字符串


0 请登录后投票
   发表时间:2011-12-15  
参考Map实现机制:先用hashcode,相等则比较完整字符串。
0 请登录后投票
   发表时间:2011-12-15  
richard_2010 写道
hashcode相等并不代表字串相等,只能保证字串相等hashcode相等


hashcode相等只能保证两个对象在一个HASH表里的同一条HASH链上,不能保证两个对象一定相等
0 请登录后投票
   发表时间:2011-12-15   最后修改:2011-12-15


楼上这个即可:
参考Map实现机制:先用hashcode,相等则比较完整字符串。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics