目录
测试环境
表结构
CASE1
结果
CASE 2
结果
CASE 3
结果
TPS修正
CASE 4
结果
结论
Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
32G memory
SSD harddisk.
postgresql.conf
max_connections = 256 shared_buffers = 3GB # min 128kB work_mem = 16MB # min 64kB maintenance_work_mem = 256MB # min 1MB wal_buffers = 4MB checkpoint_timeout = 10min # range 30s-1d max_wal_size = 2GB checkpoint_completion_target = 0.9batchsize=100, 8 threads
test_insert.sql
[root@server4 zjcktest]# cat test_insert.sql insert into t1 select generate_series(1,100),1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1copy csv
copy (select generate_series(1,100),1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1) to '/data/zjcktest/csv';copy.sql
[root@server4 zjcktest]# cat copy.sql copy t1 from '/data/zjcktest/csv' ;pgbench命令执行insert和copy
INSERT: pgbench -M prepared -n -r -P 1 -f /data/zjcktest/test_insert.sql -h 127.0.0.1 -p 5432 -U postgres zj_testdb -c 8 -j 8 -T 120 COPY: pgbench -M prepared -n -r -P 1 -f /data/zjcktest/copy.sql -h 127.0.0.1 -p 5432 -U postgres zj_testdb -c 8 -j 8 -T 120两者速率相差不大,copy略快5%
8 threads batchsize=100行数事务数TPS batch 100 insert select120221001202211000.835batch 100 copy127711001277111057.615 batch 100 insert select129455001294551077.917batch 100 copy135522001355221120.751 batch 100 insert select131649001316491092.143batch 100 copy132996001329961107.717
1 thread, batchsize=100
修改pgbench执行命令
INSERT: pgbench -M prepared -n -r -P 1 -f /data/zjcktest/test_insert.sql -h 127.0.0.1 -p 5432 -U postgres zj_testdb -c 1 -j 1 -T 120 COPY: pgbench -M prepared -n -r -P 1 -f /data/zjcktest/copy.sql -h 127.0.0.1 -p 5432 -U postgres zj_testdb -c 1 -j 1 -T 120比8线程慢25%。 copy和insert很接近,insert比copy略好3%。
1 thread, batchsize=100行数事务数TPS batch 100 insert select900310090031750.254batch 100 copy890370089037741.454 batch 100 insert select897130089713747.577batch 100 copy879870087987733.224 batch 100 insert select883990088399736.622batch 100 copy877630087763731.356 batch 100 insert select904290090429753.572batch 100 copy882740088274735.607
1 thread, batchsize=1000
修改test_insert.sql和生成新的copy文件
copy (select generate_series(1,1000),1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1) to '/data/zjcktest/csv'; [root@server4 zjcktest]# cat test_insert.sql insert into t1 select generate_series(1,1000),1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1仍旧insert稍快 4%。
1 thread, batchsize=1000行数事务数TPS batch 100 insert select111790001117993batch 100 copy107940001079489.948
考虑到copy需要读取文件,按照金士顿 SSD 读取500MB/s来计算
总读取字节读取速率cost time修正后TPS21792330425242880004.16 s93.18去掉读文件的时间,总的TPS和insert几乎一致。
1 thread, batchsize = 1
修改test_insert.sql 和 重新生成copy文件csv
cat test_insert.sql insert into t1 values (5,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1) zj_testdb=# copy (select 5,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1) to '/data/zjcktest/csv';insert要比copy快 15%.
由于单行频繁打开关闭文件句柄导致copy性能下降。
1 thread, batchsize=1行数事务数TPS insert 8356328356326963.591copy6969416969415807.831
在test_insert.sql中,使用generate_series 而不是显示一行一行insert语句的情况下,insert 整体性能和copy基本一致,单线程情况下insert性能更好。
对比test_insert.sql里有100行insert语句的测试,这里少了绝大部分的SQL语句的解析,性能与copy一样甚至超过。