请勿滥用unlogged table & hash index
2015-10-14 21:44
288 查看
Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开。本次大会嘉宾阵容强大,国内顶级PostgreSQL数据库专家将悉数到场,并特邀欧洲、俄罗斯、日本、美国等国家和地区的数据库方面专家助阵:
Postgres-XC项目的发起人铃木市一(SUZUKI Koichi)
Postgres-XL的项目发起人Mason Sharp
pgpool的作者石井达夫(Tatsuo Ishii)
PG-Strom的作者海外浩平(Kaigai Kohei)
Greenplum研发总监姚延栋
周正中(德哥), PostgreSQL中国用户会创始人之一
汪洋,平安科技数据库技术部经理
……
2015年度PG大象会报名地址:http://postgres2015.eventdove.com/PostgreSQL中国社区: http://postgres.cn/PostgreSQL专业1群: 3336901(已满)PostgreSQL专业2群: 100910388PostgreSQL专业3群: 150657323 |
unlogged table 的风险以及修复手段可以见 :
http://blog.163.com/digoal@126/blog/static/163877040201582621345351/
hash index则风险略小,但是也必须重建,但是个人还是建议大家不要使用hash index,改用btree,因为性能确实相差无几。
批量将数据库集群的hash index修改为btree index的方法:
例子:
先并行创建一个btree索引,然后并行删除对应的hash 索引。
$ vi test.sh#!/bin/bash
for db in `psql -n -q -t -h 127.0.0.1 -p 1921 -U postgres postgres -c "copy (select datname from pg_database where datname <>'template0') to stdout;"`do
psql -n -q -t -h 127.0.0.1 -p 1921 -U postgres $db -c "with t1(sql,nsp,idx) as (select regexp_replace(indexdef,'USING hash','USING btree'),schemaname,indexname from pg_indexes where indexdef ~ 'USING hash'), t2(sql_create,sql_drop) as (select regexp_replace(sql,'CREATE INDEX','CREATE INDEX CONCURRENTLY'), 'DROP INDEX CONCURRENTLY '||quote_ident(nsp)||'.'||quote_ident(idx) from t1) select regexp_replace(sql_create,'CONCURRENTLY (.*) ON','CONCURRENTLY \1_0926 ON') ||'; '|| sql_drop ||'; ' from t2;"|psql -a -e -h 127.0.0.1 -p 1921 -U postgres $db -f -
done
$ . ./test.sh
这个with查询的结果如下举例:postgres=# with t1(sql,nsp,idx) as (select regexp_replace(indexdef,'USING hash','USING btree'),schemaname,indexname from pg_indexes where indexdef ~ 'USING hash'),t2(sql_create,sql_drop) as (select regexp_replace(sql,'CREATE INDEX','CREATE INDEX CONCURRENTLY'), 'DROP INDEX CONCURRENTLY '||quote_ident(nsp)||'.'||quote_ident(idx) from t1)select regexp_replace(sql_create,'CONCURRENTLY (.*) ON','CONCURRENTLY \1_0926 ON') ||'; '|| sql_drop ||'; ' from t2; ?column? ------------------------------------------------------------------------------------------------- CREATE INDEX CONCURRENTLY hi1_0926 ON t USING btree (id); DROP INDEX CONCURRENTLY public.hi1; CREATE INDEX CONCURRENTLY hi2_0926 ON s1.tbl USING btree (id); DROP INDEX CONCURRENTLY s1.hi2; (2 rows)
或者在每个数据库调用这个inline code:
do language plpgsql $$declare v_sql text; v_schema name; v_idx name; sql1 text;beginfor v_sql,v_schema,v_idx in select regexp_replace(indexdef,'USING hash','USING btree'),schemaname,indexname from pg_indexes where indexdef ~ 'USING hash'loop sql1='DROP INDEX '||quote_ident(v_schema)||'.'||quote_ident(v_idx); execute sql1; execute v_sql; end loop; end; $$;
postgres=# \d t Table "public.t" Column | Type | Modifiers --------+---------+----------- id | integer | not nullIndexes: "t_pkey" PRIMARY KEY, btree (id) "i1" btree (id) "i2" btree (id)
postgres=# \d s1.tbl Table "s1.tbl" Column | Type | Modifiers --------+---------+----------- id | integer | Indexes: "i1" btree (id)
关于hash和btree性能:
查询性能:
postgres=# create table tbl(id int, info text);CREATE TABLEpostgres=# insert into tbl select generate_series(1,1000000);INSERT 0 1000000postgres=# create index idx_tbl1 on tbl using hash (id);
$ vi test.sql\setrandom id 1 1000000select * from tbl where id=:id;
postgres@digoal-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 30progress: 1.0 s, 24219.3 tps, lat 0.258 ms stddev 0.436progress: 2.0 s, 29387.1 tps, lat 0.270 ms stddev 0.401progress: 3.0 s, 29281.0 tps, lat 0.271 ms stddev 0.442progress: 4.0 s, 29231.7 tps, lat 0.272 ms stddev 0.844......transaction type: Custom queryscaling factor: 1query mode: preparednumber of clients: 8number of threads: 8duration: 30 snumber of transactions actually processed: 876806latency average: 0.270 mslatency stddev: 0.592 mstps = 29202.503991 (including connections establishing)tps = 29379.956438 (excluding connections establishing)statement latencies in milliseconds: 0.003062 \setrandom id 1 1000000 0.266481 select * from tbl where id=:id;
postgres=# drop index idx_tbl1 ;DROP INDEXpostgres=# create index idx_tbl1 on tbl using btree (id);CREATE INDEX
postgres@digoal-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 30progress: 1.0 s, 28414.2 tps, lat 0.240 ms stddev 0.306progress: 2.0 s, 31192.2 tps, lat 0.255 ms stddev 0.605progress: 3.0 s, 31022.8 tps, lat 0.256 ms stddev 0.451progress: 4.0 s, 29587.1 tps, lat 0.268 ms stddev 0.671......transaction type: Custom queryscaling factor: 1query mode: preparednumber of clients: 8number of threads: 8duration: 30 snumber of transactions actually processed: 903467latency average: 0.263 mslatency stddev: 0.678 mstps = 30088.054150 (including connections establishing)tps = 30229.295069 (excluding connections establishing)statement latencies in milliseconds: 0.002900 \setrandom id 1 1000000 0.259402 select * from tbl where id=:id;
更新性能
$ vi test.sql\setrandom id 1 1000000update tbl set id=1+:id where id=:id;
postgres@digoal-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 30progress: 1.0 s, 12500.2 tps, lat 0.570 ms stddev 0.864progress: 2.0 s, 17456.9 tps, lat 0.456 ms stddev 0.641progress: 3.0 s, 18242.3 tps, lat 0.435 ms stddev 0.234progress: 4.0 s, 17693.0 tps, lat 0.450 ms stddev 0.909progress: 5.0 s, 17753.3 tps, lat 0.448 ms stddev 0.758......transaction type: Custom queryscaling factor: 1query mode: preparednumber of clients: 8number of threads: 8duration: 30 snumber of transactions actually processed: 521331latency average: 0.456 mslatency stddev: 0.702 mstps = 17372.386945 (including connections establishing)tps = 17430.542432 (excluding connections establishing)statement latencies in milliseconds: 0.003072 \setrandom id 1 1000000 0.452489 update tbl set id=1+:id where id=:id;
postgres=# drop index idx_tbl1 ;DROP INDEXpostgres=# create index idx_tbl1 on tbl using hash (id);CREATE INDEX
postgres@digoal-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 30progress: 1.0 s, 16321.8 tps, lat 0.411 ms stddev 0.521progress: 2.0 s, 17372.0 tps, lat 0.458 ms stddev 0.409progress: 3.0 s, 16731.5 tps, lat 0.475 ms stddev 1.094progress: 4.0 s, 16972.9 tps, lat 0.469 ms stddev 0.880progress: 5.0 s, 17392.7 tps, lat 0.457 ms stddev 0.607......transaction type: Custom queryscaling factor: 1query mode: preparednumber of clients: 8number of threads: 8duration: 30 snumber of transactions actually processed: 527778latency average: 0.450 mslatency stddev: 0.678 mstps = 17587.300360 (including connections establishing)tps = 17671.181609 (excluding connections establishing)statement latencies in milliseconds: 0.002955 \setrandom id 1 1000000 0.446435 update tbl set id=1+:id where id=:id;
SIZE
postgres=# create index idx_tbl1 on tbl using hash (id);CREATE INDEXpostgres=# create index idx_tbl2 on tbl using btree (id);CREATE INDEXpostgres=# \di+ idx_tbl* List of relations Schema | Name | Type | Owner | Table | Size | Description --------+----------+-------+----------+-------+-------+------------- public | idx_tbl1 | index | postgres | tbl | 32 MB | public | idx_tbl2 | index | postgres | tbl | 21 MB | (2 rows)
小结:
查询和更新性能相差无几。
大小,btree是hash的2/3。
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- c语言实现hashmap(转载)
- 数据库链接字符串查询网站
- DB2实例管理
- DB2实例管理
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- CentOS下DB2数据库安装过程详解
- PostgreSQL新手入门教程
- EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)第1/2页
- PostgreSQL教程(十):性能提升技巧
- PostgreSQL教程(二):模式Schema详解
- PostgreSQL教程(十三):数据库管理详解
- PostgreSQL教程(八):索引详解