JDBC setMaxRows使查询慢得多。为什么?

时间:2015-10-13 09:18:02

标签: jdbc db2

我试图了解我对查询的情况。

查询产生54条记录。如果我设置setMaxRows(50),则查询需要23秒。如果我设置setMaxRows(5000),则查询需要0.4秒。删除setMaxRows调用也可让查询在0.4秒内完成。

我的代码:

Connection conn = getConnection();

Statement s = conn.createStatement();
s.setMaxRows(max);
long t = System.currentTimeMillis();
ResultSet rs = s.executeQuery(QUERYPOSSTR));
System.out.println(System.currentTimeMillis() - t);
rs.close();
s.close();
conn.close();

static String QUERYPOSSTR = 
    "SELECT T.MMM FROM TABLE A, TABLE T " +
    "WHERE A.SSS = ' ' AND T.MMM = A.MMM AND T.SPRACHE = 'D' " +
        "AND T.TTT IN ( 'AB','AE','AV','A0','A1') AND A.LAND IN ( 'CN') " +
        "AND (A.JJJ >= 2010) AND POSSTR(T.AAA, '234') <> 0 " +
    "ORDER BY A.MMM, T.TTT, T.LFDNR ASC";

setMaxRows实际上在这做什么?似乎选择“错误”值会导致查询花费50-60倍的时间!我使用不正确吗?

更新 再次使用不同的最大行值进行测试:

Statement with POSSTR. Max=54
    Complete in 22,62 sec
    Row count = 54
Statement with POSSTR. Max=55
    Complete in 22,61 sec
    Row count = 54
Statement with POSSTR. Max=60
    Complete in 23,36 sec
    Row count = 54
Statement with POSSTR. Max=100
    Complete in 0,90 sec
    Row count = 54

0 个答案:

没有答案