Está en la página 1de 53

Tablespace Table and Index Fragmentation

Presented by
Joel Goodman
Oracle University EMEA

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Agenda

Causes of Tablespace Fragmentation


Defragmenting Tablespaces
Causes of Table Fragmentation
Space Management for Tables
Defragmenting Tables
Types of Indexes
Space Management for Indexes
Causes of Index Fragmentation
Defragmenting Indexes
Segment Advisor

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Causes of Tablespace Fragmentation

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Causes of Tablespace Fragmentation

Tablespace has free extents of different sizes


Caused by

Use of Dictionary Managed Tablespaces

EXTENT MANAGEMENT = DICTIONARY

ALLOCATION_TYPE = USER
Use of Converted Locally Managed Tablespaces

EXTENT MANAGEMENT = LOCAL

ALLOCATION_TYPE = USER
Less with Autoallocate Locally Managed Tablespaces

EXTENT MANAGEMENT = LOCAL

ALLOCATION_TYPE = SYSTEM

No Fragmentation with Uniform Locally Managed


Tablespaces

EXTENT MANAGEMENT = LOCAL


ALLOCATION_TYPE = UNIFORM

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Tablespace Attributes

Look at DBA_TABLESPACES

SQL> select tablespace_name, extent_management,


allocation_type from dba_tablespaces order by 1;
TABLESPACE_NAME
--------------DEMO
SYSTEM
SYSAUX
TEMP
UNDOTBS1

EXTENT_MANAGEMENT
----------------LOCAL
DICTIONARY
LOCAL
LOCAL
LOCAL

ALLOCATON_TYPE
-------------USER
USER
SYSTEM
UNIFORM
SYSTEM

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Defragmenting Tablespaces Manually

Export, drop and Import all the objects


Create Table as Select (CTAS) and drop original tables
New Segment will have minimum number of extents
May result in less fragmentation
Does not retain constraints and other dependent attributes

Switch to using Uniform Size for Zero Fragmentation


But this could waste space if objects vary in size

On-Line Table Redefinition


Can retains constraints and other dependent attributes

Shrinking Tables or Indexes


May result in freeing extents
Adjacent free extents combined implicitly using Locally
Managed tablespaces

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Causes of Table Fragmentation

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Types and Causes of Table Fragmentation

Internal Fragmentation within the Extents

Caused by DML
Inserts may grow the segment
Deletes may or may not shrink segments
Eventually the extents may be partially used
More extents may exist than are required
This affects query performance
ASSM has less Internal fragmentation than Free List Managed
Segments
PDDL

Block Fragmentation within table blocks


Caused by DML
Blocks get defragmented on the fly if row to be inserted will fit when
the internal block space is fragmented.
Row is moved and new offset is stored in the Row Directory

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Space Management for Tables

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

The High-Water Mark

The high-water mark is:


Recorded in the segment header block
Set to

Just past the segment header for Free List segments

Just past the first group of formatted blocks for ASSM

Incremented as rows are inserted


Reset by the TRUNCATE command

10

Never reset by using DELETE statements

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

The High-Water Mark

Extent 1

Highwater
mark

Empty blocks
(rows deleted)
Extent 2

Segment header block

Empty blocks (never used)

11

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Free List Managed Segments

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Manual Space Management PCTFREE and


PCTUSED
Inserts

Inserts

Inserts

Inserts

13

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Guidelines for PCTFREE and PCTUSED

PCTFREE
Default is 10
Zero if no UPDATE activity

PCTFREE = 100 UPD / (Average row length)


PCTUSED
Default is 40
Set if rows are deleted

PCTUSED = 100 PCTFREE 100 Rows (Average


row length) / Block size

14

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Row Chaining and Migration


Example:
On update: Row length increases,
exceeding the available free space
in the block.
Data needs to be stored in a new block.
Original physical identifier of row
(ROWID) is preserved.
The Oracle Database server needs
to read two blocks to retrieve data.
The Segment Advisor finds segments
containing the migrated rows.
There is automatic coalescing of fragmented
free space inside the block.
15

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Old

Original block
with pointer
to migrated
row

New data

Migration and Chaining


Migration

Chaining

Index

16

Table

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Table Statistics
Populate the table statistics using the dbms_stats
package and then query the values in dba_tables:
SQL> EXECUTE dbms_stats.gather_table_stats >
('HR','EMPLOYEES');
PL/SQL procedure successfully completed.
SQL> SELECT num_rows, blocks, empty_blocks as
empty,
2
avg_space, chain_cnt, avg_row_len
3 FROM
dba_tables
4 WHERE
owner = 'HR'
5 AND
table_name = 'EMPLOYEES';
NUM_ROWS BLOCKS EMPTY AVG_SPACE CHAIN_CNT
AVG_ROW_LEN
-------- ------ ----- --------- ------------------13214
615
35
1753
946
184
17

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

ASSM Segments

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Automatic Segment-Space Management

Automatic Segment-Space Management has the


capability of tracking in-segment free and used space
via bitmaps instead of through free lists.
The benefits provided by this capability include:

19

Ease of use
Better space utilization
Better concurrency handling
Better performance
Fewer Buffer Busy Waits on inserts

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Automatic Segment Space Management

BMB

BMB

BMB

BMB

Data
block

Extent

Automatic
Enabled by the use of
locally managed
tablespaces
Tracked by bitmaps in
segments
Benefits:
More flexible space
utilization
Run-time adjustment
Multiple process
search of BMBs

Segment
20

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Automatic Segment-Space Management


at Work

BMB

S
E
G
M
E
N
T

BMB

BMB

BMB

BMB
BMB

BMB

BMB

Block

DATA

Extent

21

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

BMB

ASSM Block Space Management

PCTFREE = 10
FS2
FS3

FS1

FS1

FS2
Inserts,
updates

FS3

Deletes

Deletes

FS4
Full block
22

Full block

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Space Management for Parallel DDL

Creating tables or indexes in parallel could lead to:


External fragmentation
Internal fragmentation

Each parallel execution server creates its own


temporary segment using specified storage attributes
Internal fragmentation will almost always happen
External fragmentation should be taken care of only for
dictionary managed tablespaces:
Temporary segments might be trimmed
Use MINIMUM EXTENT at tablespace level

23

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Fragmentation and Parallelism

PQ1

PQ3

PQ2

HWM

HWM

PQ4

HWM

Internal Fragmentation

HWM

External Fragmentation
HWM

Data

24

Unused portion below HWM


Unused portion above HWM

Free space

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Defragmenting Tables

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Defragmenting Tables Manually

Create Table as Select (CTAS)


Locks table
Prevents Updates until CTAS ends
Does not duplicate Indexes and Constraints

Alter Table Shrink Space [COMPACT]

26

Coalesces
Also Moves the Table Segment High Watermark
May Free up Extents
Maintains Indexes
Is an On-Line Operation

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Defragmenting Tables Manually

Use the Export and Import utilities to:


Export the table
Drop or truncate the table
Import the table

Or use

27

Alter Table Employees Move

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Reclaiming Space Within ASSM Segments

Online and in-place operation


Applicable only to segments residing in ASSM tablespaces
Candidate segment types:

28

Heap-organized tables and index-organized tables


Indexes
Partitions and sub-partitions
Materialized views and materialized view logs

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Shrinking Segments by Using SQL


ALTER SHRINK SPACE [CASCADE][COMPACT]
TABLE [OVERFLOW]

INDEX

MODIFY PARTITION

29

MATERIALIZED VIEW

MODIFY SUBPARTITION

MATERIALIZED VIEW LOG

MODIFY LOB

ALTER TABLE employees ENABLE ROW MOVEMENT;

ALTER TABLE employees SHRINK SPACE CASCADE;

ALTER TABLE employees MODIFY LOB(resume) (SHRINK


SPACE);

ALTER TABLE employees OVERFLOW SHRINK SPACE;

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Shrinking Segments by Using Enterprise Manager

30

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Shrinking Segments

HWM

ALTER TABLE employees SHRINK SPACE COMPACT;

HWM

DML operations and queries can be issued during compaction.

ALTER TABLE employees SHRINK SPACE;

HWM

DML operations are blocked when the HWM is adjusted.


31

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Results of Shrink Operation

Improved performance and space utilization


Indexes maintained
Triggers not executed
Number of migrated rows may be reduced.
Rebuilding secondary indexes on IOTs recommended

Index

Table

Shrink

Triggers not executed


32

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

DBA

Detecting Migration and Chaining


Use this query after obtaining current statistics to detect
migration.
SQL> SELECT num_rows, chain_cnt FROM dba_tables
2
WHERE table_name='ORDERS';
NUM_ROWS CHAIN_CNT
--------- --------168
102

Detect migration and chaining using Statspack or AWR:


Statistic
Total Per transaction ...
------------------------- ----- --------------- ...
table fetch continued row
495
.02

33

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Table Statistics Showing Chain Count

Populate the table statistics using the dbms_stats


package and then query the values in dba_tables:
SQL> EXECUTE dbms_stats.gather_table_stats >
('HR','EMPLOYEES');
PL/SQL procedure successfully completed.

34

SQL> SELECT num_rows, blocks, empty_blocks as


empty,
2
avg_space, chain_cnt, avg_row_len
3 FROM
dba_tables
4 WHERE
owner = 'HR'
5 AND
table_name = 'EMPLOYEES';
NUM_ROWS BLOCKS EMPTY AVG_SPACE CHAIN_CNT
AVG_ROW_LEN
-------- ------ ----- --------- ------------------Copyright
Oracle and/or its
affiliates. All rights reserved.
13214
615 2015,35
1753
946

Selecting Migrated Rows


SQL> ANALYZE TABLE oe.orders LIST CHAINED ROWS;
Table analyzed.
SQL> SELECT owner_name, table_name, head_rowid
2
FROM chained_rows
3
WHERE table_name = 'ORDERS';
OWNER_NAME TABLE_NAME HEAD_ROWID
---------- ---------- -----------------SALES
ORDER_HIST AAAAluAAHAAAAA1AAA
SALES
ORDER_HIST AAAAluAAHAAAAA1AAB
...

35

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Eliminating Migrated Rows

Export/Import
Export the table.
Drop or truncate the table.
Import the table.

Move table command:


Alter Table Employees Move
Copying migrated rows
Find migrated rows using ANALYZE.
Copy migrated rows to new table.
Delete migrated rows from original table.
Copy rows from new table to original table.

36

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Types of Indexes

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Types of Indexes
These are several types of index structures available to
you, depending on the need:
A B-tree index is in the form of a binary tree and is the
default index type.
Reverse Key indexes use the same structure

A bitmap index has a bitmap for each distinct value


indexed, and each bit position represents a row that
may or may not contain the indexed value. This is best
for low-cardinality columns.
Bitmap Join Indexes use the same structure

38

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

B-Tree Index
Index entry
Root

Branch

Index entry header


Leaf

Key column length


Key column value
ROWID

39

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Schema
Constraints
>
Indexes
Views
Sequences
Temp Tables
Data Dict

Indexes

WHERE key = 22
Row
pointer

Key

22
22
Index

40

Table

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Bitmap Indexes
File 3

Table

Block 10
Block 11
Block 12

Index

Start
End
Key ROWID ROWID
<Blue,
<Green,
<Red,
<Yellow,
41

10.0.3,
10.0.3,
10.0.3,
10.0.3,

12.8.3,
12.8.3,
12.8.3,
12.8.3,

Bitmap
1000100100010010100>
0001010000100100000>
0100000011000001001>
0010001000001000010>

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Space Management for Indexes

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Differences in Space Management for Indexes

43

High Watermark used as in tables


For Manual Space management a block is only on the
free list if it is formatted contains no leaf or branch
rows
PCTFREE is used to reserve block space for
INSERTS not for UPDATES as is the case with tables.

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Causes of Index Fragmentation

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Types and Causes of Index Fragmentation

Internal Fragmentation within Extents

Caused by DML causing Block Splits


Can be 90/10 split when using sequence generated keys
Can be 50/50 split when using random keys
Can also be caused by DML deleting keys
Leaf blocks kept in Balanced B*Tree as long as one key is in
a block
Branch blocks kept in Balanced B*Tree as long as one
Key/Separator pair is in a block

Block Fragmentation within the Index Blocks


Index Keys Kept in Collating Sequence
No Real Fragmentation inside Index Blocks
But blocks may be relatively sparsely populated

45

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Defragmenting Indexes

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Index Reorganization

47

Indexes on volatile tables are a performance problem.


Only entirely empty index blocks go to the
free list for Free List managed segments or the Free
category for ASSM
If a block contains only one entry,
it must be maintained.
You may need to rebuild indexes.

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Monitoring Index Space

To collect usage statistics regarding an index:


SQL> ANALYZE INDEX oe.customers_pk VALIDATE
STRUCTURE;

To view statistics collected:


SQL> SELECT name,
2
(DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS wastage
3 FROM index_stats;

Rebuild indexes with wastage greater than 20%:


SQL> ALTER INDEX oe.customers_pk REBUILD;

To coalesce indexes (alternative to REBUILD):


SQL> ALTER INDEX oe.customers_pk COALESCE;
48

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Deciding Whether to Rebuild


or Coalesce an Index

49

Rebuild

Coalesce

Quickly moves index to


another tablespace.

Cannot move index to


another tablespace.

Higher costs: Requires more


disk space.

Lower costs: Does not


require more disk space.

Creates new tree, shrinks


height if applicable.

Coalesces leaf blocks within


same branch of tree.

Enables you to quickly change


storage and tablespace
parameters without having to
drop the original index.

Quickly frees up index


leaf blocks for use.

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Defragmenting Indexes Manually

Alter Index Rebuild


Uses Old Index to Create New Index
Can be an On-Line Operation
Good for very Fragmented Indexes

Alter Index Coalesce


Simply Defragments the Index
Merges Leaf Rows from Adjacent Leaves
May Merge Same Rows Multiple Times for Sparse Leaves

Alter Index Shrink Space


Coalesces
Also Moves the Index Segment High Watermark
May Free up Extents

50

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Segment Advisor

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Automatic Segment Advisor


The Automatic Segment Advisor:
Is started by a Scheduler job set to run during the default
maintenance window:
Weeknights, MondayFriday, from 10:00 PM to 2:00 AM
Saturday and Sunday, both windows start at 6:00 AM and
last for 20 hours

Examines database statistics, samples segment data, and


then selects the following objects to analyze:
Tablespaces that have exceeded a critical or warning
threshold
Segments that have the most activity
Segments that have the highest growth rate

52

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Using the Segment Advisor

53

Copyright 2015, Oracle and/or its affiliates. All rights reserved.

También podría gustarte