读取方式对读取数据库查询结果速度的影响

读取方式对读取数据库查询结果影响

基本结论:流式读取,查询速度快,读取比较耗时;非流式读取,查询速度慢,读取时间较短。设置每次读取条数后,流式读取失效(内存使用情况没有考察)
1、流式读取
query spend time:443
写入到第 1 个文件中!
写入到第 2 个文件中!
写入到第 3 个文件中!
写入到第 4 个文件中!
取回数据量为 2921844 行! spend time:2947
total spend time:3391

2、设定流式读取,每次100个id
query spend time:3441
写入到第 1 个文件中!
写入到第 2 个文件中!
写入到第 3 个文件中!
写入到第 4 个文件中!
取回数据量为 2871058 行! spend time:139
total spend time:3580

3、设定流式读取,每次1000个id
query spend time:3512
写入到第 1 个文件中!
写入到第 2 个文件中!
写入到第 3 个文件中!
写入到第 4 个文件中!
取回数据量为 2881259 行! spend time:85
total spend time:3597

3、设定流式读取,每次10000个id
query spend time:3391
写入到第 1 个文件中!
写入到第 2 个文件中!
写入到第 3 个文件中!
写入到第 4 个文件中!
取回数据量为 2838858 行! spend time:150
total spend time:3541

4、一次读取
query spend time:3406
写入到第 1 个文件中!
写入到第 2 个文件中!
写入到第 3 个文件中!
写入到第 4 个文件中!
取回数据量为 2813167 行! spend time:103
total spend time:3509

 

测试代码

 1 import java.sql.Connection;  
 2 import java.sql.DriverManager;  
 3 import java.sql.PreparedStatement;  
 4 import java.sql.ResultSet;  
 5 import java.sql.SQLException;  
 6 import java.sql.Statement;  
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 public class JdbcHandleMySQLBigResultSet {  
11   
12     public static long importData(String sql){  
13         String url = "jdbc:mysql://test.db.com:3306/logdb?user=ingage&password=ingage";
14         try {  
15             Class.forName("com.mysql.jdbc.Driver");  
16         } catch (ClassNotFoundException e1) {  
17             e1.printStackTrace();  
18         }  
19         long allStart = System.currentTimeMillis();  
20         long count =0;  
21   
22         Connection con = null;  
23         PreparedStatement ps = null;  
24         Statement st = null;  
25         ResultSet rs = null;  
26         try {  
27             con = DriverManager.getConnection(url);  
28               
29             ps = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,
30                       ResultSet.CONCUR_READ_ONLY);
31 
32             ps.setFetchSize(Integer.MIN_VALUE);
33 //            ps.setFetchSize(1000);
34 
35 //            ps.setFetchDirection(ResultSet.FETCH_REVERSE);
36   
37             rs = ps.executeQuery();
38 
39             List<Long> idList = new ArrayList<Long>();
40 
41             System.out.println("query spend time:"+(System.currentTimeMillis()-allStart));
42             long end = System.currentTimeMillis();
43             while (rs.next()) {  
44                   
45                 //此处处理业务逻辑
46 //                idList.add(rs.getLong(1));
47                 count++;  
48                 if(count%600000==0){
49 
50                     System.out.println(" 写入到第  "+(count/600000)+" 个文件中!");
51                     idList = new ArrayList<Long>();
52                 }  
53                   
54             }  
55             System.out.println("取回数据量为  "+count+" 行! spend time:"+(System.currentTimeMillis()-end));
56             System.out.println("total spend time:"+(System.currentTimeMillis()-allStart));
57         } catch (SQLException e) {  
58             e.printStackTrace();  
59         } finally {  
60             try {  
61                 if(rs!=null){  
62                     rs.close();  
63                 }  
64             } catch (SQLException e) {  
65                 e.printStackTrace();  
66             }  
67             try {  
68                 if(ps!=null){  
69                     ps.close();  
70                 }  
71             } catch (SQLException e) {  
72                 e.printStackTrace();  
73             }  
74             try {  
75                 if(con!=null){  
76                     con.close();  
77                 }  
78             } catch (SQLException e) {  
79                 e.printStackTrace();  
80             }  
81         }  
82         return count;  
83   
84     }  
85   
86     public static void main(String[] args) throws InterruptedException {  
87   
88         String sql = "select id from b_reg_statistics";
89         importData(sql);  
90   
91     }  
92   
93 }  

 

posted on 2017-04-14 14:13  学而知之者  阅读(297)  评论(0编辑  收藏  举报