KingbaseES V8R6 中unlogged表

前言

KingbaseESV8R6有一种表称为unlogged,在该表新建的索引也属于unlogged。和普通表的区别是,对该表进行DML操作时候不将该表的变更记录变更写入到wal文件中。在数据库异常关机或者异常崩溃后该表的数据会被truncate掉,但是在写入性能上会比普通表快几倍。
这个特性类似于oracle表的nologging特性。

下面分别测试普通表和unlogged表对于数据插入速度的区别

test=# \dt+
                                  List of relations
 Schema |            Name             | Type  |   Owner    |    Size    | Description
--------+-----------------------------+-------+------------+------------+-------------
 public | ee                          | table | system     | 8192 bytes |
 public | f                           | table | system     | 8192 bytes |
 public | h                           | table | system     | 104 kB     |
 public | hH                          | table | system     | 8192 bytes |
 public | kong                        | table | system     | 16 kB      |
 public | newspace                    | table | system     | 48 kB      |


test=# \timing 
Timing is on.

test=# create table h_loggod as select * from h ;
SELECT 1000
Time: 19.280 ms
可以看到在新建普通表并插104kb的数据耗时19.2毫秒

新建一张unlogged表并插入104kb数据进行测试
test=# create unlogged table h_unloggod as select * from h ;
SELECT 1000
Time: 4.653 ms

可以看到同样大小unlogged表的插入速度为4.6毫秒,性能提高了近4倍。
注意unlogged意思是表不被归档保护,所以这时候如果数据库出现断电等异常故障,这个表数据库是不被保护了,这又是老生常谈的话题,效率与安全往往不可兼得。

如何查看当前数据库中所有的unlogged表
test=# select n.nspname as schema_name,c.relname as table_name from pg_catalog.pg_namespace n, pg_catalog.pg_class c where c.relnamespace=n.oid and n.nspname != 'pg_toast' and c.relkind='r' and c.relpersistence = 'u';
 schema_name | table_name
-------------+------------
 public      | h_unloggod
(1 row)

如果需要将unlogged表修改为普通表,则执行如下语句
test=# select 'ALTER TABLE'||' '||concat(n.nspname,'.' ,c.relname)||' '||'SET LOGGED ;' AS convert_logged_sql from pg_catalog.pg_namespace n, pg_catalog.pg_class c where c.relnamespace=n.oid and n.nspname != 'pg_toast' and c.relkind='r' and c.relpersistence = 'u';
             convert_logged_sql
--------------------------------------------
 ALTER TABLE public.h_unloggod SET LOGGED ;
(1 row)

总结

如果需要加快业务上某些大表的insert速度可以开启这个特性,但是不能保证数据的安全性,不过我们可以在DML 执行完毕后,对unlogged表更为logged属性。

posted @ 2023-01-09 15:47  KINGBASE研究院  阅读(68)  评论(0编辑  收藏  举报