unlogged table和hash index同样都不会写XLOG,所以如果你用流复制来搞HA,一定概要搞清楚一个问题,切换到备库的话unlogged table数据会被清掉,而hash index也没有,走hash index会失败。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/bashfor 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
$$declarev_sql text; v_schema name; v_idx name; sql1 text;
begin
for v_sql,v_schema,v_idx in select regexp_replace(indexdef,'USING hash','USING btree'),schemaname,indexname from pg_indexes where indexdef ~ 'USING hash'
loopsql1='DROP INDEX '||quote_ident(v_schema)||'.'||quote_ident(v_idx); execute sql1; execute v_sql;
end loop;
end; $$
; postgres=# \d tTable "public.t"Column |  Type   | Modifiers
--------+---------+-----------id     | integer | not null
Indexes:"t_pkey" PRIMARY KEY, btree (id)"i1" btree (id)"i2" btree (id)postgres=# \d s1.tblTable "s1.tbl"Column |  Type   | Modifiers
--------+---------+-----------id     | integer |
Indexes:"i1" btree (id)关于hash和btree性能:
查询性能:
postgres=# create table tbl(id int, info text);
CREATE TABLE
postgres=# insert into tbl select generate_series(1,1000000);
INSERT 0 1000000
postgres=# create index idx_tbl1 on tbl using hash (id);$ vi test.sql
\setrandom id 1 1000000
select * from tbl where id=:id;postgres@digoal-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 30
progress: 1.0 s, 24219.3 tps, lat 0.258 ms stddev 0.436
progress: 2.0 s, 29387.1 tps, lat 0.270 ms stddev 0.401
progress: 3.0 s, 29281.0 tps, lat 0.271 ms stddev 0.442
progress: 4.0 s, 29231.7 tps, lat 0.272 ms stddev 0.844
......
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 8
number of threads: 8
duration: 30 s
number of transactions actually processed: 876806
latency average: 0.270 ms
latency stddev: 0.592 ms
tps = 29202.503991 (including connections establishing)
tps = 29379.956438 (excluding connections establishing)
statement latencies in milliseconds:0.003062        \setrandom id 1 10000000.266481        select * from tbl where id=:id;postgres=# drop index idx_tbl1 ;
DROP INDEX
postgres=# create index idx_tbl1 on tbl using btree (id);
CREATE INDEXpostgres@digoal-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 30
progress: 1.0 s, 28414.2 tps, lat 0.240 ms stddev 0.306
progress: 2.0 s, 31192.2 tps, lat 0.255 ms stddev 0.605
progress: 3.0 s, 31022.8 tps, lat 0.256 ms stddev 0.451
progress: 4.0 s, 29587.1 tps, lat 0.268 ms stddev 0.671
......
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 8
number of threads: 8
duration: 30 s
number of transactions actually processed: 903467
latency average: 0.263 ms
latency stddev: 0.678 ms
tps = 30088.054150 (including connections establishing)
tps = 30229.295069 (excluding connections establishing)
statement latencies in milliseconds:0.002900        \setrandom id 1 10000000.259402        select * from tbl where id=:id;更新性能
$ vi test.sql
\setrandom id 1 1000000
update 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 30
progress: 1.0 s, 12500.2 tps, lat 0.570 ms stddev 0.864
progress: 2.0 s, 17456.9 tps, lat 0.456 ms stddev 0.641
progress: 3.0 s, 18242.3 tps, lat 0.435 ms stddev 0.234
progress: 4.0 s, 17693.0 tps, lat 0.450 ms stddev 0.909
progress: 5.0 s, 17753.3 tps, lat 0.448 ms stddev 0.758
......
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 8
number of threads: 8
duration: 30 s
number of transactions actually processed: 521331
latency average: 0.456 ms
latency stddev: 0.702 ms
tps = 17372.386945 (including connections establishing)
tps = 17430.542432 (excluding connections establishing)
statement latencies in milliseconds:0.003072        \setrandom id 1 10000000.452489        update tbl set id=1+:id where id=:id;postgres=# drop index idx_tbl1 ;
DROP INDEX
postgres=# create index idx_tbl1 on tbl using hash (id);
CREATE INDEXpostgres@digoal-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 30
progress: 1.0 s, 16321.8 tps, lat 0.411 ms stddev 0.521
progress: 2.0 s, 17372.0 tps, lat 0.458 ms stddev 0.409
progress: 3.0 s, 16731.5 tps, lat 0.475 ms stddev 1.094
progress: 4.0 s, 16972.9 tps, lat 0.469 ms stddev 0.880
progress: 5.0 s, 17392.7 tps, lat 0.457 ms stddev 0.607
......
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 8
number of threads: 8
duration: 30 s
number of transactions actually processed: 527778
latency average: 0.450 ms
latency stddev: 0.678 ms
tps = 17587.300360 (including connections establishing)
tps = 17671.181609 (excluding connections establishing)
statement latencies in milliseconds:0.002955        \setrandom id 1 10000000.446435        update tbl set id=1+:id where id=:id;SIZE
postgres=# create index idx_tbl1 on tbl using hash (id);
CREATE INDEX
postgres=# create index idx_tbl2 on tbl using btree (id);
CREATE INDEX
postgres=# \di+ idx_tbl*List of relationsSchema |   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。

PostgreSQL 为什么不要滥用unlogged table hash index相关推荐

  1. 不要滥用UNLOGGED table 和 hash index

    注意PostgreSQL的unlogged table是不记录xlog的,所以在备库上没有unlogged table的数据记录. 同时,数据库在进入恢复状态时,为了保证数据的一致性,postgres ...

  2. MySQL(七):InnoDB 自适应Hash索引(Adaptive Hash Index)

    文章目录 1.简述 2.AHI(Adaptive Hash index)创建条件及注意事项 3.AHI(Adaptive Hash index)监控 3.1.通过 *show engine innod ...

  3. mysql单表最大数据量_你的Mysql库真需要Adaptive Hash Index

    说起AHI(Adaptive Hash Index),有的同学估计很陌生,都没听说,没关系,下面我会详细解释说明的,AHI是什么,mysql库为什么要设计AHI,解决什么问题,只有了解这些原理之后,才 ...

  4. mysql hash创建_Mysql自适应哈希索引(Adaptive Hash Index)创建的条件

    官方文档: If a table fits almost entirely in main memory, a hash index can speed up queries by enabling ...

  5. 一文带你了解MySQL之Adaptive Hash Index

    前言 在InnoDB体系架构图的内存结构中,还有一块区域名为:Adaptive Hash Index,翻译成中文:自适应哈希索引,缩写:AHI,它是一个纯内存结构,我们今天就来了解它. 目录 一.My ...

  6. MySQL · 引擎特性 · InnoDB Adaptive hash index介绍

    一 序 先看官网上的介绍(翻译来自MK提丰 ) The adaptive hash index (AHI) lets InnoDB perform more like an in-memory dat ...

  7. Adaptive Hash Index(自适应hash索引)

    文章目录 1.1 什么是hash索引 1.2 什么是自适应hash索引 1.3 自适应hash索引原理 1.4 相关变量 1.5 监控指标 1.6 总结 1.1 什么是hash索引   哈希索引基于哈 ...

  8. ​ RROR 1221 (HY000): Incorrect usage of spatial/fulltext/hash index and explicit index orde ​

    错误判断: RROR 1221 (HY000): Incorrect usage of spatial/fulltext/hash index and explicit index orde 类似这样 ...

  9. [原理解析] Adaptive Hash Index 是如何建立的

    Adaptive Hash Index (以下简称AHI) 估计是 MySQL 的各大特性中,大家都知道名字但最说不清原理的一个特性.本期图解我们为大家解析一下AHI是如何构建的. 首先我们思考一下A ...

  10. 批量修改table和index 的表空间

    由于开发人员把ess 项目下的大部分对象放到user 表空间中,用imp/exp 导入正式库后,ess用户的对象还是在users 表空间中.为了把ESS 的对象放到ess 默认的表空间ess中,我按如 ...

最新文章

  1. redis4.0.6集群部署(5.0.2版本更新补充)
  2. [笔记]何为Linux及其文件系统(四)
  3. android 视图覆盖,如何在Android中添加覆盖视图超过其他视图?
  4. mfc 对话框透明 控件不透明_你不知道的丨透明胶用法
  5. memcached 使用 java_使用Java java_memcached client的陷阱
  6. linux常用的BootLoader U-boot的前世今生
  7. 计算机 子分数 游戏图形,Windows7下如何开启和关闭系统分级功能.doc
  8. Linux服务器php7.3,安装zip扩展
  9. TypeScript:对象
  10. Devcpp中编译出现[errror]Id returned 1 exit status
  11. svn合并分支到主干,工具操作
  12. 华为鸿蒙主题设计,3W品牌报:2020 华为全球主题设计大赛获奖作品公布;华为鸿蒙 OS 正式上线...
  13. 怎样把计算机里的W0rd放到电脑桌面,当电脑桌面没有WORD文档时怎么打开WORD文档...
  14. r 选取从小到大的数据_r 选取表格的一列数据库
  15. 蓝牙传输速率详细分析【针对蓝牙4.2]
  16. Java语言高级(第三部分)异常多线程 ->(个人学习记录笔记)
  17. 工具_在线生成安卓证书
  18. Android逆向分析之Xposed的hook技术
  19. USB 协议 (五) 枚举
  20. idea中启动vue项目

热门文章

  1. 例4.7 素数 - 九度教程第51题(素数筛法)
  2. 例4.1 特殊乘法 - 九度教程第39题(数位拆解)
  3. matlab一个figure画多个子图,和多个figure画多个图。
  4. 「每天一道面试题」AQS是什么?了解其内部同步队列实现结构吗?
  5. 前端进阶篇——02、CSS和JS
  6. 李洪强经典面试题32
  7. 什么程序员最易找工作? 十大热门语言大汇集
  8. Linux将文件复制粘贴到另外一个位置
  9. java 八进制 转义字符_string中转义字符
  10. 阿里巴巴Java开发文档2020版学习-日期时间