Oracle8: Database Administration 13-25 ......Reorganizing Indexes Analyze the index to perform the following: does not verify whether index entries correspond to data in the table.. Orac
Trang 1Oracle8: Database Administration 13-25
Reorganizing Indexes
Analyze the index to perform the following:
does not verify whether index entries correspond to data in the table
Syntax
ANALYZE INDEX [ schema.]index VALIDATE STRUCTURE
After running this command, query INDEX_STATS to obtain information
about the index as shown in the following example:
SVRMGR> SELECT blocks, pct_used, distinct_keys
Reorganize the index if it has a high proportion of deleted rows—for
example, when the ratio of DEL_LF_ROWS to LF_ROWS exceeds 30%
13-15 Copyright Oracle Corporation, 1998 All rights reserved.
Checking Index Validity
ANALYZE INDEX scott.ord_region_id_idx
VALIDATE STRUCTURE;
INDEX_STATS
Trang 2Lesson 13: Managing Indexes
Dropping Indexes
Indexes may need to be dropped in the following scenarios:
index prior to large data loads and recreating them after the load:
unnecessarily, especially if they are based on volatile tables This is
generally the case in an OLTP system, where ad hoc queries are
generated at year-end or quarter-end to gather information for review
meetings
during certain types of operations such as loading In this case the index
13-16 Copyright Oracle Corporation, 1998 All rights reserved.
Dropping Indexes
• Drop and re-create an index before bulk
loads.
• Drop indexes that are infrequently
needed and build them when necessary.
• Drop and recreate invalid indexes.
DROP INDEX scott.dept_dname_idx;
Trang 3Oracle8: Database Administration 13-27
Dropping Indexes
Syntax
Use the following command to drop an index:
DROP INDEX [schema.] index
OEM
Note
An index cannot be dropped if it is used to implement an integrity constraint
that is enabled Constraints are discussed in the lesson “Maintaining Data
Integrity.”
Trang 4Lesson 13: Managing Indexes
Obtaining Index Information
The data dictionary views DBA_INDEXES and DBA_IND_COLUMNS
give the information on the indexes and the columns indexed
Checking Indexes and Their Validity
Use the following command to verify the name, type, and status of the
indexes owned by the user SCOTT:
SVRMGR> SELECT index_name, tablespace_name, index_type,
13-17 Copyright Oracle Corporation, 1998 All rights reserved.
Obtaining Index Information
Trang 5Oracle8: Database Administration 13-29
Obtaining Index Information
The column INDEX_TYPE indicates whether the index is bitmap or normal
Use the following query to list the names of all reverse key indexes:
Finding Columns in an Index
The following query lists all the indexes owned by the user SCOTT and
shows the tables and columns on which the indexes are built:
SVRMGR> SELECT index_name, table_owner, table_name, column_name 2> FROM dba_ind_columns
3> WHERE index_owner = 'SCOTT'
4> ORDER BY index_name, column_position;
INDEX_NAME TABLE_OWNER TABLE_NAME COLUMN_NAME - - - - EMP_LNAME_IDX SCOTT EMP LAST_NAME ORD_ORD_NO_IDX SCOTT ORD ORD_NO
ORD_REGION_ID_IDX SCOTT ORD REGION_ID
3 rows selected.
Trang 6Lesson 13: Managing Indexes
Trang 7Oracle8: Database Administration 13-31
Summary
Quick Reference
Initialization parameters CREATE_BITMAP_AREA_SIZE
Dynamic performance views None
Data dictionary tables/views DBA_INDEXES
DBA_IND_COLUMNS DBA_OBJECTS IND$
INDEX_STATS
CREATE UNIQUE INDEX CREATE BITMAP INDEX CREATE INDEX REVERSE ALTER INDEX STORAGE ALTER INDEX INITRANS MAXTRANS ALTER INDEX ALLOCATE EXTENT ALTER INDEX DEALLOCATE UNUSED ALTER INDEX REBUILD
ALTER INDEX REBUILD REVERSE ALTER INDEX REBUILD NOREVERSE ANALYZE INDEX VALIDATE STRUCTURE DROP INDEX
Packaged procedures and
functions
None
Trang 8Lesson 13: Managing Indexes
Trang 914
Maintaining Data Integrity
Trang 10Lesson 14: Maintaining Data Integrity
Trang 11Oracle8: Database Administration 14-3
• Obtaining constraint and trigger
information from the data dictionary
Trang 12Lesson 14: Maintaining Data Integrity
Overview
Data integrity guarantees that data in a database adheres to business rules
There are three primary ways in which data integrity can be maintained:
Mapping the business rules using one of the three methods is a design
decision made by the designer The database administrator is primarily
concerned with implementing the methods chosen by the designer and
balancing the performance needs against integrity requirements
Application code may be implemented either as stored procedures within the
database or as applications running on the client This lesson focuses on the
use of database triggers and integrity constraints
14-3 Copyright Oracle Corporation, 1998 All rights reserved.
Data Integrity
Application
code
Integrity constraint
Database trigger
Table Data
Trang 13Oracle8: Database Administration 14-5
Overview
Database Triggers
Database triggers are PL/SQL programs that are executed when a certain
event such as an insert or an update of a column occurs on a table Triggers
can be enabled or disabled—that is, they can be set to execute when the
event occurs, or they can be set not to execute even though they are defined
Database triggers are usually created only to enforce a complex business
rule that cannot be defined as an integrity constraint
Integrity Constraints
Integrity constraints are the preferred mechanism for enforcing business
rules because they:
coding
The following sections explain the behavior of integrity constraints and
discuss how they are implemented by the Oracle server
Trang 14Lesson 14: Maintaining Data Integrity
Integrity Constraints and Triggers
As shown in the slide, there are five types of declarative integrity constraints
Although the NOT NULL and CHECK constraints do not directly require
DBA attention, the primary key, unique, and foreign key constraints must be
managed to ensure high availability and acceptable performance levels
Instructor Note
A foreign key constraint can be defined to disable the deletion of a parent
row when child rows exist or the deletion of all the child rows when the
corresponding parent row is deleted
14-4 Copyright Oracle Corporation, 1998 All rights reserved.
Designates a column or combination of columns as unique
Designates a column or combination of columns as the table’s primary key Designates a column or combination of columns as the foreign key in a
referential integrity constraint Specifies a condition that each row of the table must satisfy
Trang 15Oracle8: Database Administration 14-7
Integrity Constraints and Triggers
An integrity constraint can be in one of the following states:
Disabled
A constraint that is disabled is not checked, even though the constraint
definition is still stored in the data dictionary Data in the table as well as
new data that is entered or updated may not conform to the rules defined by
the constraint
Enabled Novalidate (Enforced)
If a constraint is enabled novalidate, new data that violates the constraint
cannot be entered However, the table may contain data that is invalid—that
is, data that violates the constraint This is usually an intermediate stage that
ensures that all new data is checked before being accepted into the table
14-5 Copyright Oracle Corporation, 1998 All rights reserved.
Constraint States
Disabled Enabled
novalidate
Enabled validate
Existing data New data
Trang 16Lesson 14: Maintaining Data Integrity
Enabled Validate
If a constraint is in this state, then all data in the table is guaranteed to adhere
to the constraint In addition, this state prevents any invalid data from being
entered This is the normal state of operation of a constraint for online
transaction processing
When a constraint is enabled validate from a disabled state, the table is
locked and all data in the table is checked for conformance This may cause
DML operations such as a data load to wait, so it may be advisable to move
from a disabled state to enable novalidate and then to move to enable
validate
Trang 17Oracle8: Database Administration 14-9
Integrity Constraints and Triggers
The point in a transaction at which a constraint is checked can be controlled
by defining the constraint appropriately
Nondeferred or Immediate Constraints
Nondeferred constraints, also known as immediate constraints, are enforced
at the end of every DML statement A constraint violation causes the
statement to be rolled back If a constraint causes an action such as delete
cascade, the action is taken as part of the statement that caused it
A constraint that is defined as nondeferrable cannot be modified to be
enforced at the end of a transaction
Deferred Constraints
Deferred constraints are constraints that are checked only when a transaction
commits If any constraint violations are detected at commit time, the entire
transaction is rolled back These constraints are most useful when both the
parent and child rows in a foreign key relationship are entered at the same
time, as in the case of an order entry system, where the order and the items
in the order are entered at the same time
14-6 Copyright Oracle Corporation, 1998 All rights reserved.
Deferred Constraints
DML statement
Check nondeferred constraints
COMMIT
Check deferred constraints
Trang 18Lesson 14: Maintaining Data Integrity
For a constraint to be deferred, it must be defined as a deferrable constraint
at the time of creation A constraint that is defined as deferrable can be
specified as one of the following:
• Initially immediate specifies that by default it should function as an
immediate constraint, unless explicitly set otherwise
• Initially deferred specifies that by default the constraint should only be
enforced at the end of the transaction
Although the default mode of enforcement of a deferrable constraint is
specified and stored in the data dictionary, applications can modify the
constraint to work as either deferred or immediate This is accomplished by
using either an ALTER SESSION command or a SET CONSTRAINT
command as shown below:
Trang 19Oracle8: Database Administration 14-11
Integrity Constraints and Triggers
Primary and unique keys are enforced using indexes You can control the
location and type of index that is used for enforcing these constraints
Oracle Server follows this procedure to implement unique and primary key
constraints:
leading part of an index, the index will be used to enforce the constraint
3 If the constraint is enabled and there is no index that uses the constraint
columns as a leading part of the index, an index with the same name as
the constraint is created using the following rules:
created
14-7 Copyright Oracle Corporation, 1998 All rights reserved.
Use existing index
Is an index available for use?
Yes
Yes
Constraint deferrable?
Create nonunique index
Yes
No Key enabled?
Trang 20Lesson 14: Maintaining Data Integrity
You need to consider several factors in maintaining tables that are in a
foreign key relationship
DDL Involving Parent Table
the following command to perform both actions using a single
statement:
DROP TABLE table CASCADE CONSTRAINTS
foreign key
parent is dropped The following command can be used to achieve this:
DROP TABLESPACE tablespace INCLUDING CONTENTS
14-8 Copyright Oracle Corporation, 1998 All rights reserved.
Foreign Key Considerations
To
Drop parent table
Truncate parent table
Drop tablespace
containing parent
table
Avoid locks on child
table while performing
DML on parent table
Perform DML on child
table
Perform Cascade constraints Disable/drop foreign key Use CASCADE CONSTRAINTS clause
Create index on foreign key
Ensure tablespace containing parent key index online
Trang 21Oracle8: Database Administration 14-13
Integrity Constraints and Triggers
DML on Tables in a Foreign Key Relationship
If the DELETE CASCADE option is not used when rows are deleted from
the parent table, Oracle Server needs to ensure that there are no rows in the
child table with the corresponding foreign key Similarly, an update to the
parent key is permitted only when there are no child rows with the old key
value If there is no index on the foreign key on the child table, the Oracle
server locks the child table and prevents changes to ensure referential
integrity If there is an index on the table, the referential integrity is
maintained by locking the index entries and avoiding more restrictive locks
on the child table If both tables need to be updated concurrently from
different transactions, create an index on the foreign key columns
When data is inserted into or the foreign key column is updated in the child
table, the Oracle server checks the index on the parent table that is used for
enforcing the referenced key Therefore, the operation succeeds only if the
tablespace containing the index is online Note that the tablespace
containing the parent table does not need to be online to perform DML
operations on the child table
Note
Besides the advantage mentioned above, creating indexes on foreign key
columns is recommended because it has other performance advantages
Trang 22Lesson 14: Maintaining Data Integrity
Database triggers are PL/SQL programs that are implicitly executed when a
DML statement is executed on the associated table
Trigger Types
There are twelve possible trigger combinations that can be defined on a table
based on the following three properties that can be specified for a trigger:
DML Statement
A trigger can be set to execute when INSERT, UPDATE, or DELETE, or
any combination of these statements, is executed on a table When defining
an update trigger, if column names are specified, the trigger fires only when
the columns mentioned are updated
Trigger types
• INSERT or UPDATE or DELETE
• BEFORE or AFTER
• ROW or STATEMENT
Trang 23Oracle8: Database Administration 14-15
Integrity Constraints and Triggers
Trigger States
Triggers can be enabled or disabled A disabled trigger does not execute
when the corresponding DML operation is carried out on the table A trigger
that is enabled fires on execution of the corresponding DML command
Triggers are event driven, so when a trigger is enabled, no action is carried
out on an existing data
Creating a Trigger: Example
The following trigger uses the INITCAP function to convert an employee’s
last name before it is stored
CREATE TRIGGER scott.emp_conv_ln
BEFORE INSERT OR UPDATE OF last_name ON scott.employees
FOR EACH ROW
Trang 24Lesson 14: Maintaining Data Integrity
Implementing Constraints and Triggers
A constraint can be defined either when a table is created or when a table is
altered to add the constraint
Syntax: In-Line Constraint
At the time the table is created, the constraint can be created in line using the
following syntax to define the column:
column datatype [CONSTRAINT constraint]
in_line_constraint
[defer_spec]
in_line_constraint :==
{[NOT] NULL
|PRIMARY KEY [USING INDEX index_clause]
14-10 Copyright Oracle Corporation, 1998 All rights reserved.
Defining Constraints While
Creating a TableCREATE TABLE scott.employees(
empno NUMBER(4)
CONSTRAINT emp_pk PRIMARY KEY DEFERRABLE
USING INDEX STORAGE(INITIAL 100K NEXT 100K) TABLESPACE indx01,
last_name VARCHAR2(30)
CONSTRAINT emp_ln_nn NOT NULL, deptno NUMBER(2))
TABLESPACE data01;