模拟Oracle行迁移和行链接
Posted 空白葛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟Oracle行迁移和行链接相关的知识,希望对你有一定的参考价值。
行链接
消除方法创建大的block块
------------------ 参考tom kyte的例子--------------------------------------------
--创建4k blocksize的表空间
SQL> alter system set db_4k_cache_size=1m scope=both;
System altered.
SQL> create tablespace tbs1 datafile ‘/u01/app/oracle/oradata/test/tbs_1.dbf‘ size 100m blocksize 4k;
Tablespace created.
--行链接测试
--使用定列宽的char类型来创建行链接测试表
SQL> create table row_chain_demo(
x int primary key,
a char(1000),
b char(1000),
c char(1000),
d char(1000)
) tablespace tbs1;
Table created.
--插入数据
SQL> insert into row_chain_demo(x,a,b,c,d) values(1,‘a‘,‘b‘,‘c‘,‘d‘);
1 row created.
SQL> commit;
Commit complete
--分析测试表,检查行链接
--首先建chaind_rows相关表
SQL> @?/rdbms/admin/utlchain.sql
Table created.
--分析表
SQL> analyze table row_chain_demo list chained rows into chained_rows;
Table analyzed.
--查询行链接
SQL> select * from chained_rows where table_name=‘ROW_CHAIN_DEMO‘;
OWNER_NAME TABLE_NAME CLUSTER_NAME PARTITION_NAME SUBPARTITION_NAME HEAD_ROWID ANALYZE_T
------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------ ---------
SYS ROW_CHAIN_DEMO N/A AAAFytAAGAAAAEIAAA 20-AUG-18
记录下此时的执行计划:
SQL> set autotrace traceonly statistics
SQL> set linesize 100
SQL> select /*+index(ROW_CHAIN_DEMO,x)*/* from ROW_CHAIN_DEMO where x=1;
Statistics
----------------------------------------------------------
6 recursive calls
0 db block gets
13 consistent gets
0 physical reads
0 redo size
4815 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--找出row_chain_demo这张表所对应的数据块
SQL> select dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block# from row_chain_demo;
FILE# BLOCK#
---------- ----------
6 264
--dump该数据块进行分析
SQL> alter system dump datafile 6 block 264;
System altered.
--以下为trace文件的内容
data_block_dump,data header at 0x7f28ca793264
===============
tsiz: 0xf98
hsiz: 0x14
pbl: 0x7f28ca793264
76543210
flag=--------
ntab=1
nrow=1
frre=-1
fsbo=0x14
fseo=0xba1
avsp=0xb8d
tosp=0xb8d
0xe:pti[0] nrow=1 offs=0
0x12:pri[0] offs=0xba1
block_row_dump:
tab 0, row 0, @0xba1
tl: 1015 fb: --H-F lb: 0x1 cc: 2
--正常的行记录为--H-FL--,而这里为只有F(fisrt)而没有L(last),说明在这个数据块中只有行的开始,而没有行的结束,同样cc为2说明这个块中只包含了表的两个列 */
nrid: 0x01c00105.0
-- nrid表示数据块的下一个指针,即其他列数据存放的数据块地址
--通过bbed分析
BBED> p kdbr
sb2 kdbr[0] @118 2977
BBED> p *kdbr[0]
rowdata[0]
----------
ub1 rowdata[0] @3077 0x28
BBED> x /rnccccccc
rowdata[0] @3077
----------
[email protected]: 0x28 (KDRHFF, KDRHFH)
[email protected]: 0x01
[email protected]: 2 --从这也可以看出数据块中只有表的两个列
[email protected]:0x01c00105.0
col 0[2] @3086: 1
col 1[1000] @3089: a
--消除行链接
--创建blocksize为32的表空间,并将测试表移动到该表空间
SQL> alter system set db_32k_cache_size=1m scope=both;
System altered.
SQL> create tablespace tbs2 datafile ‘/u01/app/oracle/oradata/test/tbs2.dbf‘ size 100m blocksize 32k;
Tablespace created.
SQL> alter table row_chain_demo move tablespace tbs2;
Table altered.
SQL> select index_name from dba_indexes where table_name=‘ROW_CHAIN_DEMO‘;
INDEX_NAME
------------------------------
SYS_C0010231
SQL> alter index SYS_C0010231 rebuild;
Index altered.
--检查行链接
SQL> delete from chained_rows;
1 row deleted.
SQL> commit;
Commit complete.
SQL> analyze table ROW_CHAIN_DEMO list chained rows into chained_rows;
Table analyzed.
SQL> select * from chained_rows where table_name=‘CHAIN_ROW_DEMO‘;
no rows selected
--查看消除行链接后的执行计划状态
SQL> select /*+index(ROW_CHAIN_DEMO,x)*/* from ROW_CHAIN_DEMO where x=1;
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
4668 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)http://write.blog.csdn.net/postedit/52963389
1 rows processed
--可以看出只发生了两次一致性读
行迁移
消除方法,重建数据表
--创建测试表,保证表的字段能够大于blocksize
SQL> create table row_mig_demo(
x int primary key,
a char(1000),
b char(1000),
c char(1000),
d char(1000)
) tablespace tbs1;
Table created.
--插入数据,只插入一个字段的值
SQL> insert into row_mig_demo(x) values(1);
1 row created.
SQL> commit;
Commit complete.
--分析表,查看是否有行迁移情况(此时必然没有)
SQL> analyze table row_mig_demo list chained rows into chained_rows;
Table analyzed.
SQL> select * from chained_rows where table_name=‘ROW_MIG_DEMO‘;
no rows selected
--查看测试表所在的数据块
SQL> select dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block# from row_mig_demo;
FILE# BLOCK#
---------- ----------
6 279
--dump数据块查看
SQL> alter system dump datafile 6 block 279;
System altered.
--trace文件内容
data_block_dump,data header at 0x7fa1ea9e1864
===============
tsiz: 0xf98
hsiz: 0x14
pbl: 0x7fa1ea9e1864
76543210
flag=--------
ntab=1
nrow=1
frre=-1
fsbo=0x14
fseo=0xf8c
avsp=0xf7b
tosp=0xf7b
0xe:pti[0] nrow=1 offs=0
0x12:pri[0] offs=0xf8c
block_row_dump:
tab 0, row 0, @0xf8c
tl: 6 fb: --H-FL-- lb: 0x1 cc: 1 --FL:说明此时数据行的头和尾都在block内,cc:1,即只有一个字段的数据
col 0: [ 2] c1 02
end_of_block_dump
--通过bbed查看block数据层内容
BBED> p kdbr
sb2 kdbr[0] @118 3980
BBED> p *kdbr[0]
rowdata[0]
----------
ub1 rowdata[0] @4080 0x2c
BBED> x /rnccccccc
rowdata[0] @4080
----------
[email protected]: 0x2c (KDRHFL, KDRHFF, KDRHFH)
[email protected]: 0x01
[email protected]: 1
col 0[2] @4083: 1
--此时的执行计划状态
SQL> select * from ROW_MIG_DEMO where x=1;
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
639 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
--update测试表,是表的行数据大于blocksize
SQL> update row_mig_demo set a=‘a‘,b=‘b‘,c=‘c‘,d=‘d‘ where x=1;
1 row updated.
SQL> commit;
Commit complete.
SQL> delete from chained_rows;
0 rows deleted.
SQL> commit;
Commit complete.
SQL> analyze table row_mig_demo list chained rows into chained_rows;
Table analyzed.
SQL> select * from chained_rows where table_name=‘ROW_MIG_DEMO‘;
OWNER_NAME TABLE_NAME
------------------------------ ------------------------------
CLUSTER_NAME PARTITION_NAME
------------------------------ ------------------------------
SUBPARTITION_NAME HEAD_ROWID ANALYZE_T
------------------------------ ------------------ ---------
SYS ROW_MIG_DEMO
N/A AAAVqrAAHAAAAEVAAA 29-OCT-16
--可以看到已经产生了行迁移
--dump数据块查看
SQL> select dbms_rowid.rowid_relative_fno(rowid) file#,dbms_rowid.rowid_block_number(rowid) block# from row_mig_demo;
FILE# BLOCK#
---------- ----------
6 279 --表所在数据块地址并未改变
SQL> alter system dump datafile 6 block 279;
System altered.
--trace文件内容
data_block_dump,data header at 0x7f28ca793264
===============
tsiz: 0xf98
hsiz: 0x14
pbl: 0x7f28ca793264
76543210
flag=--------
ntab=1
nrow=1
frre=-1
fsbo=0x14
fseo=0xba1
avsp=0xb8d
tosp=0xb8d
0xe:pti[0] nrow=1 offs=0
0x12:pri[0] offs=0xba1
block_row_dump:
tab 0, row 0, @0xba1
tl: 1015 fb: --H-F--- lb: 0x1 cc: 2
nrid: 0x01c00105.0
col 0: [ 2] c1 02
col 1: [1000]
--bbed查看
BBED> p kdbr
sb2 kdbr[0] @118 2965
BBED> p *kdbr[0]
rowdata[0]
----------
ub1 rowdata[0] @3065 0x28
BBED> x /rnccccccc
rowdata[0] @3065
----------
[email protected]: 0x28 (KDRHFF, KDRHFH)
[email protected]: 0x02
[email protected]: 2
[email protected]:0x01c00116.0
col 0[2] @3074: 1
col 1[1000] @3077: a
--消除行迁移
SQL> create table row_mig_demp1 as select * from row_mig_demo;
Table created
以上是关于模拟Oracle行迁移和行链接的主要内容,如果未能解决你的问题,请参考以下文章
基于月份的 Oracle SQL 数据迁移行到列因类型而失败
oracle中比较alter table t move 和alter table t shrink space
LeetCode 1260.二维网格迁移:两种方法解决(k次模拟/一步到位)