`
gong_10140
  • 浏览: 146738 次
  • 性别: Icon_minigender_1
  • 来自: 太原
文章分类
社区版块
存档分类
最新评论

DB2 声明临时表

 
阅读更多
DB2支持session临时表. 该表只能存在于某一session或某一交易上下文中,对其他用户不可见. DB2 V8增加了对临时表的索引的支持, 这对于提升查询速度比较有意义;

下面举例子来说明session临时表的使用:

1. 在创建session临时表前, 您必须创建临时表空间
create user temporary tablespace test
pagesize 4 k managed by system
using ('[curdir]\temp');

2. 定义session临时表,该定义拷贝transactions的表结构, on commit preserve rows用来指定即使该交易提交了, t1临时表里的数据仍然存在,
只有当session终止后,t1表才会消失.

如果不加上  on commit preserve rows 当insert临时表提交之后临时表里的数据会自动删除掉。

declare global temporary table t1
like transactions
on commit preserve rows not logged in test;

3. 把transaction表的记录插入t1临时表中.
insert into session.t1
select * from transactions

4.从t1表中选择记录, 请注意这条语句的执行速度.
select * from session.T1
where store='Rockwood';

5. 在t1表上创建索引, db2 v8允许在临时表上创建索引,可以提升对临时表的查询速度.
create index session.t1index on session.t1(store);

6. 您再执行同样的select语句, 可以看出查询速度有所提高.
select * from session.T1
where store='Rockwood';

7.在db2 9中 不必自己创建临时表空间了,创建临时表时会自动创建一个用户临时表空间SYSTOOLSTMPSPACE。


http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0311yip/index.html








分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics