热爱编程,热爱生活 2019-08-27 11:52 采纳率: 0%
浏览 798
已采纳

多线程读数据库操作,为什么我的代码执行起来比原来还慢呢

/**
* 查询 应用系统 各表的记录总数
*
* @param url 数据源地址
* @param userName 数据源登陆用户名
* @param password 数据源密码
* @param tableNames 表名 List
* @return Map < 表名,表下数据行数>
*/

static class CountThread implements Runnable{
    CountThread(String tableName){
        this.tableName=tableName;
    }

    static void setConnection(Connection connection) {
        CountThread.connection = connection;
    }
    static void setReMap(ConcurrentHashMap map){
        CountThread.reMap=map;
    }
    static void setThreadCount(CountDownLatch countDownLatch) {
        CountThread.countDownLatch = countDownLatch;
    }
    private static CountDownLatch countDownLatch;
    private static ConcurrentHashMap<String,String> reMap;
    private static Connection connection;
    private String tableName;
    @Override
    public void run() {
        String sql = "SELECT COUNT(1) num FROM " + tableName;
        System.out.println(sql);
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            ResultSet tablesRest = stmt.executeQuery();
            while (tablesRest.next()) {
                String count = tablesRest.getString("num");
                reMap.put(tableName, count);
            }
            tablesRest.close();
            tablesRest = null;
            countDownLatch.countDown();
        } catch (SQLException e) {
            log.error(e.getMessage());
            reMap.put(tableName, "0");
        }
    }
}

public static Map<String, String> conutRows(String url, String userName, String password, List<String> tableNames)  {
    int size=tableNames.size();
    Date start=new Date();
    ConcurrentHashMap<String, String> reMap = new ConcurrentHashMap<>(size);
    CountDownLatch countDownLatch=new CountDownLatch(size);
    try (Connection connection = DriverManager.getConnection(url, userName, password)) {
        CountThread.setConnection(connection);
        CountThread.setReMap(reMap);
        CountThread.setThreadCount(countDownLatch);
        for(String tableName:tableNames){
            CountThread thread = new CountThread(tableName);
            thread.run();
        }
        countDownLatch.await();

// sql.append(" SELECT ");
// for (String tableName : tableNames) {
// sql.append(" " + tableName + "b.num " + tableName + "a ,"); //字符串拼接 表名+a 拼接成表别名
// }
//
// //删除多余的一个 ,
// sql.deleteCharAt(sql.length() - 1);
// sql.append(" FROM ");
// //子查询 ,分别统计表字段数
// for (String tableName : tableNames) {
// sql.append(" ( SELECT COUNT(*) num FROM ");
// sql.append(tableName);
// sql.append(" ) " + tableName + "b ,");
// }
// //删除多余的一个 ,
// sql.deleteCharAt(sql.length() - 1);
// System.out.println(sql);
// //使用sql语句进行查询获取结果集
// PreparedStatement stmt = connection.prepareStatement(sql.toString());
// ResultSet tablesRest = stmt.executeQuery();
// //对结果进行遍历,此处结果集实际应只有一行数据
// while (tablesRest.next()) {
// for (String tableName : tableNames) {
// String count = tablesRest.getString(tableName + "a");
// reMap.put(tableName, count);
// }
// }
// tablesRest.close();
// tablesRest = null;
} catch (Exception e) {
log.error(e.getMessage());
throw new RuntimeException("数据源连接失败");
}
Date end=new Date();
long costTime = (end.getTime()-start.getTime());
System.out.println("******************************************"+costTime+"**************************");
return reMap;

}
  • 写回答

2条回答 默认 最新

  • threenewbee 2019-08-27 11:55
    关注

    像数据库操作这种重io的,并且数据库本身已经优化的,再多线程就没有办法加速了,甚至更慢。

    好比你同时复制两个文件,肯定比一个一个复制更慢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决