postgresql 支持会话级别的临时表,表的存续期只在创建临时表的会话存活期间,会话退出后,临时表自动删除,表结构及数据也无法跨会话共享。KINGBASE除了支持PG原生的临时表机制外,还支持类似oracle 的临时表机制,也就是全局
postgresql 支持会话级别的临时表,表的存续期只在创建临时表的会话存活期间,会话退出后,临时表自动删除,表结构及数据也无法跨会话共享。KINGBASE除了支持PG原生的临时表机制外,还支持类似oracle 的临时表机制,也就是全局临时表。全局临时表支持表结构共享,避免用户每次了都需要创建临时表的操作。以下以例子的形式,介绍PG 临时表与全局临时表的机制与差异。
会话A:创建临时表
test=# create temporary table temp_t1(id integer);
CREATE TABLE
test=# insert into temp_t1 values(1);
INSERT 0 1
test=# d
List of relations
Schema | Name | Type | Owner
-----------+---------------------+-------+--------
pg_temp_4 | temp_t1 | table | system
test=# select * from temp_t1;
id
----
1
会话B:
test=# select * from pg_temp_4.temp_t1;
ERROR: cannot access temporary tables of other sessions
PG 临时表机制总结:
PG 建临时表时也可以用 create global temporary table,但实际创建的都是local 级别的。KINGBASE 如果用create global temporary table,实际意义上创建的是全局的临时表,临时表的机制与oracle相似。
A会话:创建临时表
test=# create global temporary table g_temp_t1(id integer) on commit preserve rows;
CREATE TABLE
test=# insert into g_temp_t1 values(1);
INSERT 0 1
test=# select * from g_temp_t1;
id
----
1
(1 row)
test=# d
List of relations
Schema | Name | Type | Owner
-----------+---------------------+-------+--------
pg_temp_4 | temp_t1 | table | system
public | g_temp_t1 | table | system
B会话:可以访问A会话创建的临时表
test=# select * from g_temp_t1;
id
----
(0 rows)
test=# insert into g_temp_t1 values(2);
INSERT 0 1
test=# select * from g_temp_t1;
id
----
2
A会话:会话退出后,表结构还在,但A会话插入的数据没有了。
test=# select * from g_temp_t1;
id
----
1
(1 row)
test=# q
[kingbase@dbhost03 ~]$ ksql -d test -U system
ksql (V8.0)
Type "help" for help.
test=# d
List of relations
Schema | Name | Type | Owner
--------+---------------------+-------+--------
public | g_temp_t1 | table | system
public | sys_stat_statements | view | system
test=# select * from g_temp_t1;
id
----
(0 rows)
KINGBASE 全局临时表总结:
test=# select relnamespace::regnamespace, relname,relpersistence,relkind,reltablespace from pg_class where relname in ("t1","temp_t1","g_temp_t1");
relnamespace | relname | relpersistence | relkind | reltablespace
--------------+-----------+----------------+---------+---------------
public | t1 | p | r | 0
public | g_temp_t1 | s | r | 0
pg_temp_5 | temp_t1 | t | r | 0
以下几点需要注意:
test=# create table t1(id integer,name varchar(200));
CREATE TABLE
test=# create temporary table temp_t1(id integer,name varchar(200));
CREATE TABLE
test=# create global temporary table g_temp_t1(id integer,name varchar(200));
CREATE TABLE
BEGIN
for i in 1..200000 loop
insert into g_temp_t1 values(i,repeat("a",200));
end loop;
END;
/
ANONYMOUS BLOCK
Time: 1684.636 ms (00:01.685)
BEGIN
for i in 1..200000 loop
insert into temp_t1 values(i,repeat("a",200));
end loop;
END;
/
ANONYMOUS BLOCK
Time: 741.746 ms
BEGIN
for i in 1..200000 loop
insert into t1 values(i,repeat("a",200));
end loop;
END;
/
ANONYMOUS BLOCK
Time: 944.549 ms
test=# insert into temp_t1 select generate_series(1,500000),repeat("a",200);
INSERT 0 500000
Time: 484.780 ms
test=# insert into g_temp_t1 select generate_series(1,500000),repeat("a",200);
INSERT 0 500000
Time: 2606.722 ms (00:02.607)
test=# insert into t1 select generate_series(1,500000),repeat("a",200);
INSERT 0 500000
Time: 1856.685 ms (00:01.857)
通过观察日志,可以看到本地临时表、全局临时表基本不产生redo日志。但从实际执行效率看,全局临时表的效率是最差的,甚至不如普通的表。但这不妨碍全局临时表的使用,不产生Redo就是最好的优点。
根据全局临时表的relfilenode,我们查找了相关的表,发现数据库目录下有如下文件:
-rw------- 1 kingbase kingbase 8192 Jun 16 15:45 t5_19820
-rw------- 1 kingbase kingbase 8192 Jun 16 15:47 t4_19820
每个文件实际对应一个事务,事务提交后,对应文件也就删除。
--结束END--
本文标题: Kingbase 全局临时表
本文链接: https://www.lsjlt.com/news/8522.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0