程序袁_哈哈 2017-08-07 08:06 采纳率: 0%
浏览 3454
已结题

jdbc查询mysql与直接在客户端上查询慢很多

 /**
     * 获取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的时候的问题。

  • 写回答

12条回答 默认 最新

  • GannnkKi 2017-08-07 08:10
    关注

    你确定要170秒?!

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题