首页 > 解决方案 > 如何在 Java 中加快数据插入 MySQL 的速度

问题描述

我根据Java 文件中的行数从 ASCII 文件中读取行

在我阅读了从数据中创建的 ArryLists 的行之后。我通过以下方式将这些 ArrayLists 插入 MySQL:

public static void post(ArrayList<String> date1, 
            ArrayList<Integer> cloud_layer_1,
            ArrayList<Integer> cloud_layer_2,
            ArrayList<Integer> cloud_layer_3,
            ArrayList<Integer> cloud_layer_4,
            ArrayList<Integer> cloud_layer_5,
            ArrayList<Integer> LayerC_1, 
            ArrayList<Integer> LayerC_2,
            ArrayList<Integer> LayerC_3,
            ArrayList<Integer> LayerC_4,
            ArrayList<Integer> LayerC_5, 
            ArrayList<Integer> ncb,
            ArrayList<Character> sc,
            ArrayList<Integer> hlbc,
            ArrayList<Integer> hvv,
            ArrayList<Integer> hslbc,
            ArrayList<Integer> hhbc,
            ArrayList<String> iaws,
            ArrayList<Integer> scL,
            ArrayList<Integer> reL,
            ArrayList<Integer> profL,
            ArrayList<Integer> leL,
            ArrayList<String> tempL,
            ArrayList<Integer> wtL,
            ArrayList<Integer> tiltL,
            ArrayList<Integer> bclL,
            ArrayList<String> parL,
            ArrayList<Integer> sbacL,
            //List<Double> hx) throws Exception{
            ArrayList<String> twabss) throws Exception{
        try{
            Connection con = getConnection();
            int ii = date1.size();
            String sql = "INSERT INTO ceilo6(time, "
                    + "cloud_1, "
                    + "cloud_2,"
                    + "cloud_3,"
                    + "cloud_4,"
                    + "cloud_5,"
                    + "layer_1,"
                    + "layer_2,"
                    + "layer_3,"
                    + "layer_4,"
                    + "layer_5,"
                    + "ncb,"
                    + "sc,"
                    + "hlbc,"
                    + "hvv,"
                    + "hslbc,"
                    + "hhbc,"
                    + "iaws,"
                    + "scL,"
                    + "reL,"
                    + "profL,"
                    + "leL,"
                    + "tempL,"
                    + "wtL,"
                    + "tiltL,"
                    + "bclL,"
                    + "parL,"
                    + "sbacL,"
                    + "twabss)"

                   + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            try(PreparedStatement posted = con.prepareStatement(sql)){
            for(int k =0; k < ii; k++){
            posted.setString(1, date1.get(k));
            posted.setInt(2, cloud_layer_1.get(k));
            posted.setInt(3, cloud_layer_2.get(k));
            posted.setInt(4, cloud_layer_3.get(k));
            posted.setInt(5, cloud_layer_4.get(k));
            posted.setInt(6, cloud_layer_5.get(k));
            posted.setInt(7, LayerC_1.get(k));
            posted.setInt(8, LayerC_2.get(k));
            posted.setInt(9, LayerC_3.get(k));
            posted.setInt(10, LayerC_4.get(k));
            posted.setInt(11, LayerC_5.get(k));
            posted.setInt(12, ncb.get(k));
            posted.setString(13,String.valueOf(sc.get(k)));
            posted.setInt(14, hlbc.get(k));
            posted.setInt(15, hvv.get(k));
            posted.setInt(16, hslbc.get(k));
            posted.setInt(17, hhbc.get(k));
            posted.setString(18, iaws.get(k));
            posted.setInt(19, scL.get(k));
            posted.setInt(20, reL.get(k));
            posted.setInt(21, profL.get(k));
            posted.setInt(22, leL.get(k));
            posted.setString(23, tempL.get(k));
            posted.setInt(24, wtL.get(k));
            posted.setInt(25, tiltL.get(k));
            posted.setInt(26, bclL.get(k));
            posted.setString(27, parL.get(k));
            posted.setInt(28, sbacL.get(k));
            posted.setString(29, twabss.get(k));
            posted.addBatch();       
            posted.executeBatch();

            }
            }catch(Exception e){System.out.println(e);} 
        }catch(Exception e){System.out.println(e);}
        finally {System.out.println("Insert Completed.");}


    }

即使我使用了Branch. 我读到这LOAD DATA IFILE会加快进程,但我不能使用它,因为原始 ASCII 文件包含标题,我无法修改原始文件。有人可以建议我加快插入过程的方法吗?

标签: javamysql

解决方案


推荐阅读