【转载】Fast Inserts to PostgreSQL with JDBC and COPY FROM
source: http://rostislav-matl.blogspot.com/2011/08/fast-inserts-to-postgresql-with-jdbc.html
Thanks !
Fast Inserts to PostgreSQL with JDBC and COPY FROM
For the purpose of the test I created following table:
CREATE TABLE measurement
(
measurement_id bigint NOT NULL,
valid_ts timestamp with time zone NOT NULL,
measurement_value numeric(19,4) NOT NULL,
CONSTRAINT pk_mv_raw PRIMARY KEY (measurement_id, valid_ts)
)
WITH (OIDS=FALSE)
I decided to test the insertion of 1000 records to the table. The data for the recors was generated before running of any of test methods. Four test methods were created to reflect ususal approaches:
- VSI (Very Stupid Inserts) - executing queries made of concatenated Strings one by one
- SPI (Stupid Prepared Inserts) - similar to VSI but using prepared statements
- BPI (Batched Prepared Inserts) - prepared inserts, executed in batches of various length
- CPI (Copy Inserts) - inserts based on COPY FROM, executed in batches of various length
Prior to each inserts the table is cleared, the same after all data are succesfully inserted. Commit is called only once in each test method, following all the insert calls. The following code exerpts illustrate the above listed approaches:
VSI
for (int i=0; i<testSize; i++)
{
insertSQL = "insert into measurement values ("
+ measurementIds[i] +",'"+ timestamps[i] +"',"+values[i]+")";
insert.execute(insertSQL);
}
SPI
PreparedStatement insert = conn.prepareStatement("insert into measurement values (?,?,?)");
for (int i=0; i<testSize; i++)
{
insert.setLong(1,measurementIds[i]);
insert.setTimestamp(2, timestamps[i]);
insert.setBigDecimal(3, values[i]);
insert.execute();
}
BPI
PreparedStatement insert = conn.prepareStatement("insert into measurement values (?,?,?)");
for (int i=0; i<testSize; i++)
{
insert.setLong(1,measurementIds[i]);
insert.setTimestamp(2, timestamps[i]);
insert.setBigDecimal(3, values[i]);
insert.addBatch();
if (i % batchSize == 0) { insert.executeBatch(); }
}
insert.executeBatch();
CPI
StringBuilder sb = new StringBuilder();
CopyManager cpManager = ((PGConnection)conn).getCopyAPI();
PushbackReader reader = new PushbackReader( new StringReader(""), 10000 );
for (int i=0; i<testSize; i++)
{
sb.append(measurementIds[i]).append(",'")
.append(timestamps[i]).append("',")
.append(values[i]).append("\n");
if (i % batchSize == 0)
{
reader.unread( sb.toString().toCharArray() );
cpManager.copyIn("COPY measurement FROM STDIN WITH CSV", reader );
sb.delete(0,sb.length());
}
}
reader.unread( sb.toString().toCharArray() );
cpManager.copyIn("COPY measurement FROM STDIN WITH CSV", reader );
I hoped to get some improvements for using COPY FROM instead of batched inserts but not expected no big gain. But the results were a pleasant surprise. For a batch of size 50 (as defined in the original aplication I wanted to improve) the COPY FROM gave 40% improvement. I expect some improvements when data come from a stream and skip the StringBuffer-with-PushbackReader exercise.
See the graphs yourself - the number following the method abbreviation is the size of the batch.
![]() |
Average time in milliseconds |
![]() |
All the 200 runs individually |
【转载】Fast Inserts to PostgreSQL with JDBC and COPY FROM的更多相关文章
- 数据库语言(三):MySQL、PostgreSQL、JDBC
MySQL MySQL资料很多,这里只给出一个在论坛博客中最常用的操作:分页 mysql> select pname from product limit 10,20; limit的第一个参数是 ...
- (转载)SQL Server 2008 连接JDBC详细图文教程
点评:SQL Server 2008是目前windows上使用最多的sql数据库,2008的安装机制是基于framework重写的,特点是非常耗时间SQL Server 2008是目前windows上 ...
- postgresql Java JDBC 一次性传入多个参数到 in ( ?) - multple/list parameters
经常不清楚需要传入多少个参数到 IN () 里面,下面是简单方法: 方法 1 - in ( SELECT * FROM unnest(?)) ) Integer[] ids={1,2,3}; ...
- 【转载】C#的DataTable类Clone及Copy方法的区别
在C#中的Datatable类中,Clone方法和Copy方法都可以用来复制当前的DataTable对象,但DataTable类中的Clone方法和Copy方法还是有区别的,Clone方法只复制结构信 ...
- PostgreSQL数据导出导入COPY
[postgres@DELL-R720 bin]$ ./psql -p 6432psql (9.4.5)Type "help" for help. postgres=# postg ...
- 编写postgresql函数执行循环copy命令导入大数据
CREATE OR REPLACE FUNCTION copyData() RETURNS boolean AS $BODY$ DECLARE i int; begin i :=1; FOR i IN ...
- JMeter学习(八)JDBC测试计划-连接Oracle(转载)
转载自 http://www.cnblogs.com/yangxia-test 一.测试环境准备 Oracle:10g JDBC驱动:classes12.jar oracle安装目录下(orac ...
- 数据库jdbc链接:mysql, oracle, postgresql
#db mysql#jdbc.driver=com.mysql.jdbc.Driver#jdbc.url=jdbc:mysql://localhost:3306/mysql?&useUnico ...
- PostgreSQL相关的软件,库,工具和资源集合
PostgreSQL相关的软件,库,工具和资源集合. 备份 wal-e - Simple Continuous Archiving for Postgres to S3, Azure, or Swif ...
随机推荐
- reverse-XNUCA-babyfuscator
上一次线上赛的一道题目 链接:http://pan.baidu.com/s/1qY9ztKC 密码:xlr2 这是一道代码混淆的题目,因为当时还不知道angr这样一个软件,所以我就用了自己的一种思路 ...
- What does "Rxlch" mean in ENCODE?
In ENCODE project, we could see some files are called "...rxlch...", which means "rev ...
- 国内较快的maven镜像
原文网址:http://www.cnblogs.com/dingyingsi/p/3856456.html 国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以 ...
- PostgreSQL数据库基本配置
一.安装 首先安装PostgreSQL客户端: sudo apt-get install postgresql-client 然后,安装PostgreSQL服务器: sudo apt-get inst ...
- Shell基础-Bash变量-用户自定义变量
变量设置规则: 变量名称可以由字母.下划线和数字组成,但是不能由数字开头. 在Bash中变量的默认类型是字符串类型,若需要进行数值运算,则需指定变量类型为数值型.变量用等号链接,且两边不能有空格.若需 ...
- Android使用echarts框架的K线图
百度echarts框架还是比较强大的,之前有尝试使用它,但毕竟主要使用于web网页端,效果不是很好,所以最终还是取消使用echarts 但之前在使用的过程中遇到些问题,虽然解决很简单,但也花了我不少时 ...
- Do It Wrong, Get It Right
Do It Wrong, Get It Right Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:65536KB Total ...
- c++对象成员的引用---12
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 在程序中经常需要访问对象中的成员.访问对象中的成员可以有3种方法: 通过对象名和成员运算符访问对象中的成 ...
- NUnit笔记
注意:单元测试中,Case 与 Case 之间不能有任何关系 测试方法不能有返回值,不能有参数,测试方法必须声明为 public [TestFixture] //声明测试类 [SetUp] //建立, ...
- Shell 显示带颜色字体
格式: echo "/033[字背景颜色;字体颜色m字符串/033[控制码"如果单纯显示字体颜色可以固定控制码位0m.格式: echo "/033[字背景颜色;字体颜色m ...