简体   繁体   English

对于重复查询,JDBC for mysql的性能非常慢

[英]Performance of JDBC for mysql is very slow for repeatedly query

I have a program need large amount of queries written in Java, JDBC is chosen to manipulate mysql db. 我有一个程序需要用Java编写大量查询,选择JDBC来操作mysql db。 The frame of my code is as follows: 我的代码框架如下:

PreparedStatement stmt = conn.preparedStatement(
            "SELECT name FROM users WHERE id = ?" )
Iterator<String, Double> it = map.entrySet().iterator(); 
//map is relativelly large, holding about 100,000 records, 
//each need to query mysql, and do some computing
//I record the time consuming of query & computing
while ( it.hasNext() ) {
    String id = it.next().getKey();
    stmt.setString(1, id);     //set the missing param with id
    long queryStart = System.currentTimeMillis();
    ResultSet rs = st.executeQuery();
    long queryEnd = System.currentTimeMillis();
    while ( rs.next() ) {
        //computing
    }
    rs.close();
    st.clearParameters();
    long computeEnd = System.currentTimeMillis();
    System.out.println(index+" has done...");
    System.out.println("  query time: "+ (queryEnd-queryStart));
    System.out.println("  compute time: "+ (computeEnd-queryEnd));
}

The performance is well at beginning for about 100-200 loops. 大约100-200次循环的性能开始良好。 But it suddenly goes down after. 但它突然下降了。

The results print in console window is : 在控制台窗口中打印的结果是:

1 has done...
  query time:   0
  compute time: 0
2 has done...
  query time:   0
  compute time: 0
3 has done...
  ...
  ...
191 has done...
  query time:   1
  compute time: 0
192 has done...
  query time:   0
  compute time: 1
193 has done...
  query time: 1018
  compute time: 0
194 has done...
  query time: 1142
  compute time: 0
195 has done...
  query time: 1122
  compute time: 0

My database is at localhost. 我的数据库在localhost。 Why does this happen, what can impact the performance such dramatically? 为什么会发生这种情况,哪些会对性能产生如此大的影响?

How can I improve the performance? 如何提高性能?

BTW: the Object Statement, Connection ... is defined in java.sql, I'm not using the com.mysql.jdbc version, I don't know what's the deffernce. 顺便说一句:对象语句,连接...在java.sql中定义,我没有使用com.mysql.jdbc版本,我不知道什么是deffernce。

Few tips which can help improve the performance: 一些有助于提高性能的提示:

  1. Can you try enabling the query caching in MYSQL Server configuration? 您可以尝试在MYSQL Server配置中启用查询缓存吗? For the same please refer to : Query Caching in mysql 同样请参考: mysql中的查询缓存

  2. Are indexes set for this table? 是否为此表设置了索引?

when you issue a query , everytime it goes over network and got executed by db and come back to you. 当你发出一个查询时,每次它通过网络并由db执行并返回给你。 In order to reduce this roundtrip, you can execute the select query with bunch of ids. 为了减少此往返,您可以使用一堆id执行select查询。

how about writing a query like below 如何编写如下的查询

select name from users where id in ( id0 , id1, .... idn) 从id为(id0,id1,.... idn)的用户中选择名称

write a method to construct "id" clause and set it in the statement and execute it. 编写一个方法来构造“id”子句并在语句中设置它并执行它。

if number of ids are high , execute the same query with set of ids batch by batch. 如果id的数量很高,请逐批执行具有一组ID的相同查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM