/**
* 获取statement对象,操作数据库,处理返回结果
* */
public static void process() {
System.out.println("------- " + System.currentTimeMillis());
Connection con = getConnection();
System.out.println("------- " + System.currentTimeMillis());
PreparedStatement ps = null;
ResultSet rs = null;
long current = System.currentTimeMillis();
System.out.println("------ " + current);
String sql = "select user_id from (select t.user_id, sum(case when type='fundmoney' then t.cny else 0 end) as fundcny, \n" +
"sum(case when type='withdrawmoney' then -t.cny else 0 end) as withdrawmoney from userdata t1 inner join `transaction` t \n" +
"on t1.user_id = t.user_id and t.type in ('fundmoney','withdrawmoney') where t.type in ('fundmoney','withdrawmoney') \n" +
"and substring(t1.id_number,7,4) >= 1993 and length(t1.id_number) = 18 and t1.id_number_status in (2,3,4,9,10) and t1.id_type = 1 \n" +
" group by t1.user_id) t where fundcny >= 100000 or withdrawmoney >= 100000";
try {
ps = con.prepareStatement(sql);
ps.setFetchSize(1000);
System.out.println("开始查询");
if (ps.execute()) {
rs = ps.getResultSet();
System.out.println("---查询时间--- " + (System.currentTimeMillis() - current) / 1000);
} else {
int i = ps.getUpdateCount();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rs, ps, con);
}
}
这个查出来需要 170秒
但是直接把sql弄到客户端上查只要7秒,已经确定没有查询缓存。
统一回复一下, 不是说SQL慢的问题,而是说用jdbc执行同一个SQL 执行时间却变慢了,30倍的差距, 所以找sql的原因我真不知道应该怎么回答,
试过简单的SQL,在客户端上只要300ms 但是用jdbc需要3s 也是10倍的差距, 已经确定不是因为连接时的原因,
就是在executeQuery的时候的问题。