缓存搜索结果实现

时间:2013-10-07 07:20:59

标签: java mysql caching

数据库:mysql - 表结构是。

------------------------------------------
phone_no (This is PK)    | month   | year
------------------------------------------
varchar(15)              | INT (2) | INT(4)

这有数百万条记录,现在当用户在一个月内第一次访问该网站时,我必须在数据库中创建一个条目,否则什么都不会发生,这是出于报告目的。我的Java代码是:

The VO ---->
         public class UesrAccessInfo {
            private String phone_no;
            private int year;
            private int month;
             .. getters and setters ...
        }

现在访问该表的java代码是:

/ *以下方法在java代码中实现,没有数据库访问* /

String phone_no = getPhoneNumber(); 
int currentMonth = getCurMonth(); 
int currentYear = getCurYear(; 

if (phone_no == null ) {
     request.setAttribute("firstAccessInCurrentMonth", false);
} 
else{
    UesrAccessInfo oUesrAccessInfo = new UesrAccessInfo();
    oUesrAccessInfo.setPhone_no(phone_no);
    UserAcessHistoryDBUtil.getUesrAccessInfo(oUesrAccessInfo);

    //////////// Code  for getUesrAccessInfo() in UserAcessHistoryDBUtil class /////////////
      String sql = "select month , year from user_access_history where phone_no= ?";
      String[][] rs = new MyDBAccessor().getPreparedTable(sql,new String[] { oUesrAccessInfo.getMsisdn() });
      if (rs != null && rs.length > 0) {
         for (String[] item : rs) {
            oUesrAccessInfo.setMonth(Integer.parseInt(item[0]));
            oUesrAccessInfo.setMonth(Integer.parseInt(item[1]));
         }
      }else{
        oUesrAccessInfo.setMonth(0);
      }
    }
    /////////////////////////////////////////////////////////////////
    /**
     * User is already present in database
     */
    if(oUesrAccessInfo.getMonth() != 0){

        /**
         * User has already accessed the site in current month
         */
        if(oUesrAccessInfo.getYear() == currentYear && oUesrAccessInfo.getMonth() == currentMonth){
            request.setAttribute("firstAccessInCurrentMonth", false);
        }
        else{
            UserAcessHistoryDBUtil.updateUserAccessHistory(phone_no, ""+ currentMonth, "" + currentYear);
            request.setAttribute("firstAccessInCurrentMonth", true);
        }
    }
    /**
     * User is not present in database
     */
    else{
        UserAcessHistoryDBUtil.createUserAccessHistory(phone_no,""+ currentMonth, "" + currentYear);
        request.setAttribute("firstAccessInCurrentMonth", true);
    }
}

所以,我在这里遇到性能问题。我不能使用缓存,因为服务器中可能存在内存不足错误。

任何建议,为了提高性能,我是mysql的新手。

1 个答案:

答案 0 :(得分:0)

我的建议

  • 由于这只是为了报告目的而不是 影响用户,你为什么不run it in a separate thread
  • 您可以为每次访问某些TableA执行插入操作,并创建一个批处理进程,该进程将批量插入到您当前插入的表中。至少这将避免运行选择查询。