0

为了优化执行批量插入子句的性能。我使用了TDengine的绑定参数功能。其中预处理sql。下面的代码是我的演示,我在其中前向插入 4 行数据到表 [t1,t2,t3,t4] 中,这些表是使用稳定天气自动创建的。

public void testBind() {
    String[] tbnm = {"t1", "t2", "t3", "t4"};
    String[] tags = {"New York", "New Jersey", "Texas", "Massachusetts"};
    for (int i = 0; i < tbnm.length; i++) {
        pstmt_insert.setTableName(tbnm[i]);
        pstmt_insert.setTagString(0, tags[i]);
        try {
            ArrayList<Long> ts = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                ts.add(System.currentTimeMillis() );//+ j
            }
            pstmt_insert.setTimestamp(0, ts);

            ArrayList<Integer> f1 = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                f1.add(r.nextInt(100));
            }
            pstmt_insert.setInt(1, f1);

            ArrayList<Long> f2 = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                f2.add(r.nextLong());
            }
            pstmt_insert.setLong(2, f2);

            ArrayList<Float> f3 = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                f3.add(r.nextFloat());
            }
            pstmt_insert.setFloat(3, f3);

            ArrayList<Double> f4 = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                f4.add(r.nextDouble());
            }
            pstmt_insert.setDouble(4, f4);
            pstmt_insert.columnDataAddBatch();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
    try {
        pstmt_insert.columnDataExecuteBatch();
        pstmt_insert.columnDataClearBatch();
        pstmt_insert.columnDataCloseBatch();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }

}

运行上面的代码后,我发现已经创建了 4 个表 t1,t2,t3,t4,但是每个表只有一行。结果

4

0 回答 0