代码操作数据库性能优化总结

来源:互联网 发布:阿里云ftp端口号 编辑:程序博客网 时间:2024/06/05 10:56

1 查询

1.1 按时间字段分块查询(数据量非常大)

  • 按照时间进行分段查询,比如只查询当天未处理的数据

1.2 分页查询(定时任务访问数据库)

  • 每次任务查询规定好条数的一页,任务再次执行的时候再去查询下一页,这样可以达到优化

1.3 查询出来的数据放到内存当中(对于公共的配置库信息)

1.3.1 定时任务型的

  • 写一个公共Cache类,把从数据库读出来的信息放到公共的Cashe类里面,如:
public static PacAppRunInfo getPacAppRunInfo(String msg) {        //-----------------------Send-----------------------        if(msg.equals("cusPAC101")) {            if(pacAppRunInfoCusPAC101 == null) {                return pacAppRunInfoCusPAC101 = new PacAppRunInfo();            }            return pacAppRunInfoCusPAC101;        }        else if (msg.equals("cusPAC102")) {            if(pacAppRunInfoCusPAC102 == null) {                return pacAppRunInfoCusPAC102 = new PacAppRunInfo();            }            return pacAppRunInfoCusPAC102;        }        .....

1.3.2 并发型的

  • 写一个公共Cache类,把从数据库读出来的信息放到公共的Cashe类里面,这里为了防止并发,需要加同步锁,如:
private CachePool()  {    this.cacheItems = new HashMap();  }  public static synchronized CachePool getAppConfigInstance()  {    if (appConfigInstance == null)      appConfigInstance = new CachePool();    return appConfigInstance;  }  public static synchronized CachePool getTaskCertRefInstance()  {    if (taskCertRefInstance == null)      taskCertRefInstance = new CachePool();    return taskCertRefInstance;  }
原创粉丝点击