我使用 JDBC 将 100 万行插入到带有时间刻度的测试表中,性能似乎约为普通 postgresql 的一半。时间刻度调整是通过采用时间刻度调整实用程序建议的所有值来完成的。我做错了什么?
private static void writeTable(String sql, int count, int commitCount,
Connection conn) throws Exception
{
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
long t = start;
PreparedStatement stmt = conn.prepareStatement(sql);
for(int i = 0; i < count; i++)
{
stmt.setTimestamp(1, new Timestamp(t));
stmt.setDouble(2, 10.9);
stmt.addBatch();
t ++;
if(commitCount != -1 && ((i + 1) % commitCount) == 0)
{
stmt.executeBatch();
conn.commit();
}
}
stmt.executeBatch();
stmt.close();
conn.commit();
conn.close();
long diff = System.currentTimeMillis() - start;
System.out.println("Count : " + count);
System.out.println("Total Time : " + diff);
System.out.println("Writes/Sec : " + ((count * 1000) / diff));
}
Query: INSERT INTO kt_device_info (di_device_id, di_time, di_value) VALUES (1,?,?)
Table:
CREATE TABLE kt_device (
id BIGINT PRIMARY KEY,
d_name TEXT
);
insert into kt_device(id, d_name) values (1, 'dev-1');
CREATE TABLE kt_device_info (
di_device_id BIGINT REFERENCES kt_device NOT NULL,
di_time TIMESTAMPTZ NOT NULL,
di_value DOUBLE PRECISION NULL
);
SELECT create_hypertable('kt_device_info', 'di_time');
时间刻度: 数量:1000000 总时间:42026 每秒写入次数:23794
Postgres 10: 数量:1000000 总时间:22573 写入/秒:44300
x86_64-pc-linux-gnu 上的 PostgreSQL 10.10 (Ubuntu 10.10-1.pgdg16.04+1),由 gcc 编译 (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609,64 位
timescaledb | 1.4.2 |公共(public)|支持时间序列数据的可扩展插入和复杂查询
硬件: Intel(R) Core(TM) i7-4702MQ CPU @ 2.20GHz,16GB 内存
Chunks:
SELECT show_chunks('kt_device_info');
show_chunks
----------------------------------------
_timescaledb_internal._hyper_7_7_chunk
(1 row)
最佳答案
查看您的代码,您正在创建间隔毫秒的时间戳。这可以解释为什么你只有一大块。默认 block 大小为 7 天。在这种情况下,您可能希望将分区设置得更小,例如几秒钟。您可以使用以下命令更改 block 大小: SELECT set_chunk_time_interval('kt_device_info', 4000);
关于java - TimescaleDB 的插入速度比普通 postgresql 10 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214652/