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

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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘