if you notice poor performance in your oracle
database row chaining and migration may be one of several reasons, but
we can prevent some of them by properly designing and/or diagnosing the
database.
row migration & row chaining are two
potential problems that can be prevented. by suitably diagnosing, we
can improve database performance. the main considerations are:
- what is row migration & row chaining ?
- how to identify row migration & row chaining ?
- how to avoid row migration & row chaining ?
migrated rows affect oltp systems which use
indexed reads to read singleton rows. in the worst case, you can add an
extra i/o to all reads which would be really bad. truly chained rows
affect index reads and full table scans.
the operating system block size is the minimum
unit of operation (read /write) by the os and is a property of the os
file system. while creating an oracle database we have to choose the «data
base block size» as a multiple of the operating system block size. the
minimum unit of operation (read /write) by the oracle database would be
this «oracle block», and not the os block. once set, the «data
base block size» cannot be changed during the life of the database
(except in case of oracle 9i). to decide on a suitable block size for
the database, we take into consideration factors like the size of the
database and the concurrent number of transactions expected.
the database block has the following structure (within the whole database structure)
header
header contains the general information about the
data i.e. block address, and type of segments (table, index etc). it
also contains the information about table and the actual row (address)
which that holds the data.
free space
space allocated for future update/insert operations. generally affected by the values of pctfree and pctused parameters.
data
actual row data.
freelist, pctfree and pctused
while creating / altering any table/index, oracle used two storage parameters for space control.
- pctfree - the percentage of space reserved for future update of existing data.
- pctused - the percentage of minimum space used for insertion of new row data.
this value determines when the block gets back into the freelists structure.
- freelist - structure where oracle maintains a list of all free available blocks.
oracle will first search for a free block in the
freelist and then the data is inserted into that block. the
availability of the block in the freelist is decided by the pctfree
value. initially an empty block will be listed in the freelist
structure, and it will continue to remain there until the free space
reaches the pctfree value.
when the free space reach the pctfree value the
block is removed from the freelist, and it is re-listed in the freelist
table when the volume of data in the block comes below the pctused
value.
oracle use freelist to increase the performance.
so for every insert operation, oracle needs to search for the free
blocks only from the freelist structure instead of searching all blocks.
we will migrate a row when an update to that row
would cause it to not fit on the block anymore (with all of the other
data that exists there currently). a migration means that the entire
row will move and we just leave behind the «forwarding address». so,
the original block just has the rowid of the new block and the entire
row is moved.
the forwarding addresses are ignored. we know
that as we continue the full scan, we'll eventually get to that row so
we can ignore the forwarding address and just process the row when we
get there. hence, in a full scan migrated rows don't cause us to
really do any extra work -- they are meaningless.
when we index read into a table, then a migrated
row will cause additional io's. that is because the index will tell us
«goto file x, block y, slot z to find this row». but when we get there
we find a message that says «well, really goto file a, block b, slot c
to find this row». we have to do another io (logical or physical) to
find the row.
a row is too
large to fit into a single database block. for example, if you use a
4kb blocksize for your database, and you need to insert a row of 8kb
into it, oracle will use 3 blocks and store the row in pieces. some
conditions that will cause row chaining are: tables whose rowsize exceeds the blocksize. tables with long
and long raw columns are prone to having chained rows. tables with more
then 255 columns will have chained rows as oracle break wide tables up
into pieces. so, instead of just having a forwarding address on one
block and the data on another we have data on two or more blocks.
chained rows affect us differently. here, it
depends on the data we need. if we had a row with two columns that was
spread over two blocks, the query:
select column1 from table
where column1 is in block 1, would not cause any «table fetch continued row». it
would not actually have to get column2, it would not follow the chained
row all of the way out. on the other hand, if we ask for:
select column2 from table
and column2 is in block 2 due to row chaining, then you would in fact see a «table fetch continued row»
the following example was published by , it will show row migration and chaining. we are using an 4k block size:
select name,value from v$parameter where name = 'db_block_size';
name value -------------- ------ db_block_size 4096
create the following table with char fixed columns:
create table row_mig_chain_demo ( x int primary key, a char(1000), b char(1000), c char(1000), d char(1000), e char(1000) );
that is our table. the char(1000)'s will let us easily cause rows to migrate or chain. we
used 5 columns a,b,c,d,e so that the total rowsize can grow to about
5k, bigger than one block, ensuring we can truly chain a row.
insert into row_mig_chain_demo (x) values (1); insert into row_mig_chain_demo (x) values (2); insert into row_mig_chain_demo (x) values (3); commit;
we are not interested about seeing a,b,c,d,e - just fetching them. they are really wide so we'll surpress their display.
column a noprint column b noprint column c noprint column d noprint column e noprint
select * from row_mig_chain_demo;
x ---------- 1 2 3
check for chained rows:
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row'; name value ---------------------------------------------------------------- ---------- table fetch continued row 0
now that is to be expected, the
rows came out in the order we put them in (oracle full scanned this
query, it processed the data as it found it). also expected is the
table fetch continued row is zero. this data is so small right now, we know that all three rows fit on a single block. no chaining.
demonstration of the row migration
now, lets do some updates in a specific way. we want to demonstrate the row migration issue and how it affects the full scan:
update row_mig_chain_demo set a = 'z1', b = 'z2', c = 'z3' where x = 3; commit; update row_mig_chain_demo set a = 'y1', b = 'y2', c = 'y3' where x = 2; commit; update row_mig_chain_demo set a = 'w1', b = 'w2', c = 'w3' where x = 1; commit;
note the order of updates, we did last row first, first row last.
select * from row_mig_chain_demo;
x ---------- 3 2 1
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 0
interesting, the rows came out «backwards»
now. that is because we updated row 3 first. it did not have to
migrate, but it filled up block 1. we then updated row 2. it migrated
to block 2 with row 3 hogging all of the space, it had to. we then
updated row 1, it migrated to block 3. we migrated rows 2 and 1,
leaving 3 where it started.
so, when oracle full scanned the table, it found
row 3 on block 1 first, row 2 on block 2 second and row 1 on block 3
third. it ignored the head rowid piece on block 1 for rows 1 and 2 and
just found the rows as it scanned the table. that is why the table
fetch continued row is still zero. no chaining.
so, lets see a migrated row affecting the «table fetch continued row»:
select * from row_mig_chain_demo where x = 3;
x ---------- 3
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 0
this was an index range scan / table access by rowid using the primary key. we didn't increment the «table fetch continued row» yet since row 3 isn't migrated.
select * from row_mig_chain_demo where x = 1;
x ---------- 1
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 1
row 1 is migrated, using the primary key index, we forced a «table fetch continued row».
demonstration of the row chaining
update row_mig_chain_demo set d = 'z4', e = 'z5' where x = 3; commit;
row 3 no longer fits on block 1. with d and e set, the rowsize is about 5k, it is truly chained.
select x,a from row_mig_chain_demo where x = 3;
x ---------- 3
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 1
we fetched column «x» and «a» from row 3 which are located on the «head» of the row, it will not cause a «table fetch continued row». no extra i/o to get it.
select x,d,e from row_mig_chain_demo where x = 3;
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 2
now we fetch from the «tail» of the row via the primary key index. this increments the «table fetch continued row» by one to put the row back together from its head to its tail to get that data.
now let's see a full table scan - it is affected as well:
select * from row_mig_chain_demo;
x ---------- 3 2 1
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 3
the «table fetch continued row» was incremented here because of row 3, we had to assemble it to get the trailing columns. rows 1 and 2, even though they are migrated don't increment the «table fetch continued row» since we full scanned.
select x,a from row_mig_chain_demo;
x ---------- 3 2 1
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 3
no «table fetch continued row» since we didn't have to assemble row 3, we just needed the first two columns.
select x,e from row_mig_chain_demo;
x ---------- 3 2 1
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 4
but by fetching for d and e, we incemented the «table fetch continued row».
we most likely have only migrated rows but even if they are truly
chained, the columns you are selecting are at the front of the table.
so, how can you decide if you have migrated or truly chained?
count the last column in that table. that'll force to construct the entire row.
select count(e) from row_mig_chain_demo;
count(e) ---------- 1
select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) = 'table fetch continued row';
name value ---------------------------------------------------------------- ---------- table fetch continued row 5
analyse the table to verify the chain count of the table:
analyze table row_mig_chain_demo compute statistics;
select chain_cnt from user_tables where table_name = 'row_mig_chain_demo';
chain_cnt ---------- 3
three rows that are chained. apparently, 2 of them are migrated (rows 1 and 2) and one is truly chained (row 3).
the v$sysstat view tells you how many times, since the system (database) was started you did a «table fetch continued row» over all tables.
sqlplus system/
select 'chained or migrated rows = '||value from v$sysstat where name = 'table fetch continued row';
chained or migrated rows = 31637
you could have 1 table with 1 chained row that
was fetched 31'637 times. you could have 31'637 tables, each with a
chained row, each of which was fetched once. you could have any
combination of the above -- any combo.
also, 31'637 - maybe that's good, maybe that's bad. it is a function of
- how long has the database has been up
- how many rows is this as a percentage of total fetched rows.
for example if 0.001% of your fetched are table fetch continued row, who cares!
therefore, always compare the total fetched rows against the continued rows.
select name,value from v$sysstat where name like '%table%';
select name,value from v$sysstat where name like '%table%';
name value ---------------------------------------------------------------- ---------- table scans (short tables) 124338 table scans (long tables) 1485 table scans (rowid ranges) 0 table scans (cache partitions) 10 table scans (direct read) 0 table scan rows gotten 20164484 table scan blocks gotten 1658293 table fetch by rowid 1883112 table fetch continued row 31637 table lookup prefetch client count 0
the user_tables tells you immediately after an analyze (will be null otherwise) how many rows in the table are chained.
analyze table row_mig_chain_demo compute statistics;
select chain_cnt, round(chain_cnt/num_rows*100,2) pct_chained, avg_row_len, pct_free , pct_used from user_tables where table_name = 'row_mig_chain_demo';
chain_cnt pct_chained avg_row_len pct_free pct_used ---------- ----------- ----------- ---------- ---------- 3 100 3691 10 40
pct_chained shows 100% which means all rows are chained or migrated.
increasing pctfree can help to avoid migrated
rows. if you leave more free space available in the block, then the row
has room to grow. you can also reorganize or re-create tables and
indexes that have high deletion rates. if tables frequently have rows
deleted, then data blocks can have partially free space in them. if
rows are inserted and later expanded, then the inserted rows might land
in blocks with deleted rows but still not have enough room to expand.
reorganizing the table ensures that the main free space is totally
empty blocks.
the alter table ... move statement enables
you to relocate data of a nonpartitioned table or of a partition of a
partitioned table into a new segment, and optionally into a different
tablespace for which you have quota. this statement also lets you modify any of the storage attributes
of the table or partition, including those which cannot be modified
using alter table. you can also use the alter table ... move statement
with the compress keyword to store the new segment using table
compression.
- alter table move
first count the number of rows per block before the alter table move
select dbms_rowid.rowid_block_number(rowid) "block-nr", count(*) "rows" from row_mig_chain_demo group by dbms_rowid.rowid_block_number(rowid) order by 1;
block-nr rows ---------- ---------- 2066 3
now, de-chain the table, the alter table move rebuilds the row_mig_chain_demo table in a new segment, specifying new storage parameters:
alter table row_mig_chain_demo move pctfree 20 pctused 40 storage (initial 20k next 40k minextents 2 maxextents 20 pctincrease 0);
table altered.
again count the number of rows per block after the alter table move
select dbms_rowid.rowid_block_number(rowid) "block-nr", count(*) "rows" from row_mig_chain_demo group by dbms_rowid.rowid_block_number(rowid) order by 1;
block-nr rows ---------- ---------- 2322 1 2324 1 2325 1
- rebuild the indexes for the table
moving a table changes the rowids of the rows in the table. this causes indexes on the table to be marked unusable, and dml accessing the table using these indexes will receive an ora-01502
error. the indexes on the table must be dropped or rebuilt. likewise,
any statistics for the table become invalid and new statistics should
be collected after moving the table.
analyze table row_mig_chain_demo compute statistics;
error at line 1: ora-01502: index 'scott.sys_c003228' or partition of such index is in unusable state
this is the primary key of the table which must be rebuilt.
alter index sys_c003228 rebuild; index altered.
analyze table row_mig_chain_demo compute statistics; table analyzed.
select chain_cnt, round(chain_cnt/num_rows*100,2) pct_chained, avg_row_len, pct_free , pct_used from user_tables where table_name = 'row_mig_chain_demo';
chain_cnt pct_chained avg_row_len pct_free pct_used ---------- ----------- ----------- ---------- ---------- 1 33.33 3687 20 40
if the table includes lob column(s), this
statement can be used to move the table along with lob data and lob
index segments (associated with this table) which the user explicitly
specifies. if not specified, the default is to not move the lob data
and lob index segments.
using the chained_rows table, you can find out the tables with chained or migrated rows.
- create the chained_rows table
cd $oracle_home/rdbms/admin sqlplus scott/tiger @utlchain.sql
- analyse all or only your tables
select 'analyze table '||table_name||' list chained rows into chained_rows;' from user_tables /
analyze table row_mig_chain_demo list chained rows into chained_rows; analyze table dept list chained rows into chained_rows; analyze table emp list chained rows into chained_rows; analyze table bonus list chained rows into chained_rows; analyze table salgrade list chained rows into chained_rows; analyze table dummy list chained rows into chained_rows;
table analyzed.
-
show the rowids for all chained rows
this
will allow you to quickly see how much of a problem chaining is in each
table. if chaining is prevalent in a table, then that table should be
rebuild with a higher value for pctfree
select owner_name, table_name, count(head_rowid) row_count from chained_rows group by owner_name,table_name /
owner_name table_name row_count ------------------------------ ------------------------------ ---------- scott row_mig_chain_demo 1
migrated rows affect oltp systems
which use indexed reads to read singleton rows. in the worst case, you
can add an extra i/o to all reads which would be really bad. truly
chained rows affect index reads and full table scans.
-
row migration is typically caused by update operation
-
row chaining is typically caused by insert operation.
-
sql statements which are creating/querying these chained/migrated rows will degrade the performance due to more i/o work.
-
to diagnose chained/migrated rows use analyze command , query v$sysstat view
-
to remove chained/migrated rows use higher pctfree using alter table move.
|