Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2916951
  • 博文数量: 199
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 4126
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-06 19:06
个人简介

半个PostgreSQL DBA,热衷于数据库相关的技术。我的ppt分享https://pan.baidu.com/s/1eRQsdAa https://github.com/chenhuajun https://chenhuajun.github.io

文章分类

全部博文(199)

文章存档

2020年(5)

2019年(1)

2018年(12)

2017年(23)

2016年(43)

2015年(51)

2014年(27)

2013年(21)

2011年(1)

2010年(4)

2009年(5)

2008年(6)

分类: Mysql/postgreSQL

2014-12-29 18:13:38

前段时间有同事咨询PostgreSQL相关的问题,发现他们用了一个自动生成的32字节的字符串作为唯一键,而这张表的数据量相当大,建议他们改用序列,可减少存储空间。但用序列有一点不一样,就是序列必须顺序产生,那么高并发访问时会不会成为性能瓶颈呢?下面做个测试验证一下。

1.测试环境

个人PC上的VMware虚拟机
PC
 CPU:Intel Core i5-3470 3.2G(4核)
 MEM:6GB
 SSD:OCZ-VERTEX4 128GB(VMware虚拟机所在磁盘,非系统盘)
 OS:Win7


VMware虚拟机
 CPU:4核
 MEM:1GB
 OS:CentOS 6.5
 PG:PostgreSQL 9.3.4(shared_buffers = 128MB,其他是默认值)

2.测试方法

创建表和序列

  1. postgres=# create sequence seq1;
  2. CREATE SEQUENCE
  3. postgres=# create sequence cached_seq cache 100;
  4. CREATE SEQUENCE
  5. postgres=# create table tb1(c1 bigint);
  6. CREATE TABLE


分别将下面的6个测试SQL写到到不同文件中

  1. select 1
  2. select nextval('seq1')
  3. select nextval('cached_seq')
  4. insert into tb1 values(1)
  5. insert into tb1 values(nextval('seq1'))
  6. insert into tb1 values(nextval('cached_seq'))

以“select 1”为例,分别测试1,10和100并发时的tps
1个并发

  1. -bash-4.1$ pgbench -n -c 1 -j 1 -T 2 -f s1.sql
  2. transaction type: Custom query
  3. scaling factor: 1
  4. query mode: simple
  5. number of clients: 1
  6. number of threads: 1
  7. duration: 2 s
  8. number of transactions actually processed: 53902
  9. tps = 26938.365906 (including connections establishing)
  10. tps = 26991.946783 (excluding connections establishing)

10个并发

  1. -bash-4.1$ pgbench -n -c 10 -j 10 -T 2 -f s1.sql
  2. transaction type: Custom query
  3. scaling factor: 1
  4. query mode: simple
  5. number of clients: 10
  6. number of threads: 10
  7. duration: 2 s
  8. number of transactions actually processed: 194019
  9. tps = 96983.799293 (including connections establishing)
  10. tps = 97749.039911 (excluding connections establishing)

100个并发

  1. -bash-4.1$ pgbench -n -c 100 -j 100 -T 2 -f s1.sql
  2. transaction type: Custom query
  3. scaling factor: 1
  4. query mode: simple
  5. number of clients: 100
  6. number of threads: 100
  7. duration: 2 s
  8. number of transactions actually processed: 178286
  9. tps = 88122.453862 (including connections establishing)
  10. tps = 97019.521088 (excluding connections establishing)

3.测试结果


                        \   发数
                   SQL   \   
1 10 100
select 1 26991 97749 97019
select nextval('seq1') 17336 61615 69211
select nextval('cached_seq') 19379 69693 76410
insert into tb1 values(1) 4042 19792 30982
insert into tb1 values(nextval('seq1')) 4083 18822 27365
insert into tb1 values(nextval('cached_seq')) 3953 18145 28701


4. 结论

1,序列创建很快
   单纯的nextval()在普通PC上都可以达到7万的tps,相比其他操作,创建序列本身要快的多,所以不大可能成为系统性能的瓶颈
2,序列的cache优化效果不大
  因为序列创建不是性能瓶颈所以也看不出cache的优化效果。序列cache后可能会导致序列的不连续,所以除非真的需要,否则不必cache。

5. 补充

序列既然提供了cache,想必对性能还是有用处的。参考网友对Oracle的测试,使用cache 50和不使用cache,处理时间居然差了100多倍。
http://blog.itpub.net/751051/viewspace-731760/

但在PostgreSQL上进行类似的带序列的批量插入的测试,cache的性能提高仍然不明显。是不是PG对序列的优化做的太好了,都不需要那种牺牲序列连续性的序列cache上场了?

  1. postgres=# insert into tb1 select 1 from (select generate_series(1,100000))tbx;
  2. INSERT 0 100000
  3. Time: 145.642 ms
  4. postgres=# insert into tb1 select 1 from (select generate_series(1,100000))tbx;
  5. INSERT 0 100000
  6. Time: 156.057 ms
  7. postgres=# insert into tb1 select 1 from (select generate_series(1,100000))tbx;
  8. INSERT 0 100000
  9. Time: 172.874 ms
  10. postgres=# insert into tb1 select nextval('seq1') from (select generate_series(1,100000))tbx;
  11. INSERT 0 100000
  12. Time: 184.046 ms
  13. postgres=# insert into tb1 select nextval('seq1') from (select generate_series(1,100000))tbx;
  14. INSERT 0 100000
  15. Time: 183.670 ms
  16. postgres=# insert into tb1 select nextval('seq1') from (select generate_series(1,100000))tbx;
  17. INSERT 0 100000
  18. Time: 183.410 ms
  19. postgres=# insert into tb1 select nextval('cached_seq') from (select generate_series(1,100000))tbx;
  20. INSERT 0 100000
  21. Time: 181.197 ms
  22. postgres=# insert into tb1 select nextval('cached_seq') from (select generate_series(1,100000))tbx;
  23. INSERT 0 100000
  24. Time: 144.633 ms
  25. postgres=# insert into tb1 select nextval('cached_seq') from (select generate_series(1,100000))tbx;
  26. INSERT 0 100000
  27. Time: 198.545 ms



阅读(5836) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~