Professional Documents
Culture Documents
Oracle INDEXES
Just like we have index present in the textbooks to help us find the
particular topic in the book, Oracle index behaves the same way.
Indexes are used to search the rows in the table quickly. If the index
is not present, the select query has to read the whole table and
returns the rows. With Index, the rows can be retrieved quickly
Indexes are the primary means of reducing disk I/O when properly
used.
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
constraints
Composite
ROWID returns the address of each row in the table. Oracle assigns a
ROWID to each row. ROWID consists of following
The datafile in which the row resides (first file is 1). The file number is
relative to the tablespace.
Oracle uses ROWID internally to access rows. For instance, Oracle stores
ROWID in index and uses it to access the row in the table.
You can display ROWID of rows using SELECT command as follows:
select rowid, emp_name from emp;
ROWID EMP_NAME
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
AAADC576474722aSAAA
John
B TREE INDEX:
B-Tree Indexes (balanced tree) are the most common type of index.
B-Tree index stored the ROWID and the index key value in a tree
structure.
Each branch holds the range of data its leaf blocks hold, and each root
holds the range of data its branches hold:
B-Tree indexes are most useful on columns that appear in the where
clause (SELECT WHERE EMPNO=1).
The Oracle server, keeps the tree balanced by splitting index blocks,
when new data is inserted to the table.
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
0-30
Root block
31-60
Branch Block
0-10
30-40
11-20
41-50
0-rowid
11 -rowid
21 -rowid
31 -rowid
41 -rowid
51 -rowid
1-rowid
12-rowid
22-rowid
32-rowid
42-rowid
52-rowid
Leaf Block
Advantages
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
Inserts, updates, and deletes are efficient, maintaining key order for
fast retrieval.
B-tree performance is good for both small and large tables, and does
not degrade as the size of a table grows.
Example
Create index
scott.exp_idx
Tablespace TOOLS;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
TABLESPACE <tablespace_name>
Compress <column number>;
Bitmap Indexes generally work on columns with low data cardinality (as
opposed to B-Tree indexes).
This type of index, creates a binary map of all index values, and store that
map in the index blocks, this means that the index will require less space
than B-Tree index.
Each bit in the bitmap corresponds to a possible rowid. If the bit is set, then
it means that the row with the corresponding rowid contains the key value. A
mapping function converts the bit position to an actual rowid, so the bitmap
index provides the same functionality as a regular index even though it uses
a different representation internally. If the number of different key values is
small, then bitmap indexes are very space efficient
The Bitmap index can be useful on large columns with low-DML activity like
marital status (M/S) or gender (M/F).
Bitmap Index structure contain a map of bits which indicate the value in the
column, for example, for the GENDER column, the index block will hold the
starting ROWID, the ending ROWID and the bit map:
Bitmap indexes are very useful when created on columns with low
cardinality, used with the AND & OR operator in the query condition:
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
TABLESPACE <tablespace_name>;
Example
CREATE BITMAP INDEX ON emp_data(gender);
SELECT COUNT(*) FROM emp_data
WHERE GENDER=M;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
TABLESPACE <tablespace_name>;
Example
CREATE INDEX EMP_IDX on EMP(UPPER(ENAME));
SELECT *
FROM Emp
WHERE UPPER(Ename) like JOHN`;
They are special types of B-Tree indexes and are very useful when created
on columns contain sequential numbers.
When using a regular B-Tree, the index will grow to have many branches
and perhaps several levels, thus causing performance degradation, the RKI
solve the problem by reversing the bytes of each column key and indexing
the new data.
This method distributes the data evenly in the index. Creating a RKI is done
using the REVERSE keyword: CREATE INDEX ON REVERSE;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
Oracle INDEXES
Example
CREATE INDEX emp_idx ON emp_table (firstname,lastname) REVERSE;
When we are using B-Tree, Bitmap and Reverse key indexes are used for
tables that store data in an unordered fashion (Heap Tables).
These indexes contain the location of the ROWID of required table row, thus
allowing direct access to row data
An index-organized table differs from an ordinary table because the data for
the table is held in its associated index. Changes to the table data, such as
adding new rows, updating rows, or deleting rows, result in updating the
index.
The index-organized table is like an ordinary table with an index on one or
more of its columns, but instead of maintaining two separate storages for
the table and the B-tree index, the database system maintains only a single
B-tree index which contains both the encoded key value and the associated
column values for the corresponding row. Rather than having a rows rowid
as the second element of the index entry, the actual data row is stored in
the B-tree index. The data rows are built on the primary key for the table,
and each B-tree index entry contains <primary_key_value,
non_primary_key_column_values>. Index-organized tables are suitable for
accessing data by the primary key or any key that is a valid prefix of the
primary key.
There is no duplication of key values because only non-key column values
are stored with the key. You can build secondary indexes to provide efficient
access by other columns. Applications manipulate the index-organized table
just like an ordinary table, using SQL statements. However, the database
system performs all operations by manipulating the corresponding B-tree
index.
Features of Index organized table
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
10
Oracle INDEXES
There are 2 benefits of using IOT: 1. table rows are indexes, access to table
is done using its primary key, the row is returned quickly from IOT than
heap tables. 2.
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
11
Oracle INDEXES
The Mapping Table maps the indexs physical ROWIDs to logical ROWIDs in
the IOT. IOT use logical ROWIDs to manage table access by index because
physical ROWIDs are changed whenever data is added to or removed from
the table. In order to distinct the IOT from other indexes, query the
USER_INDEXES view using the pct_direct_access column. Only IOT will have
a non-NULL value for this column.
12
Oracle INDEXES
You can use the NO_INDEX hint to give the CBO maximum
flexibility while disallowing the use of a certain index.
You can use the FULL hint to force the optimizer to choose a full
table scan instead of an index scan.
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
13
Oracle INDEXES
14
Oracle INDEXES
Query 3
select * from emp where dept_no =5;
The above query will not use index as the where clause does not select the
column which has index
Query 4
select * from emp where substr(emp_name,1,4) =XYZW;
The above query will not use index as the where clause uses the function on
the column and we dont have functional index on emp_name
Oracle used to lock the table on which index is being created throughout
creation process in older versions. This makes table unavailable for data
manipulation during the creation of index.
Now with 8i , Oracle introduced online rebuilding of index where Oracle
doesnt lock the table on which index is being built.
Online indexing is provided through the keyword ONLINE.
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
15
Oracle INDEXES
Basically with online rebuild, Oracle locks the table at the start and end of
the creation of the index. It allows transaction in between. The mechanism
has been quite improved with 11g and 12c
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
16
Oracle INDEXES
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
17
Oracle INDEXES
DML statements against the table proceed, but unusable indexes are not
maintained.
DML statements terminate with an error if there are any unusable indexes
that are used to enforce the UNIQUE constraint.
For nonpartitioned indexes, the optimizer does not consider any unusable
indexes when creating an access plan for SELECT statements. The only
exception is when an index is explicitly specified with the INDEX() hint.
Beginning with Oracle Database 11g Release 1, you can create invisible
indexes or make an existing index invisible. An invisible index is ignored by
the optimizer unless you explicitly set the
OPTIMIZER_USE_INVISIBLE_INDEXES initialization parameter to TRUE at
the session or system level. Unlike unusable indexes, an invisible index is
maintained during DML statements. Although you can make a partitioned
index invisible, you cannot make an individual index partition invisible while
leaving the other partitions visible. Using invisible indexes, you can do the
following:
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
18
Oracle INDEXES
DBA_INDEXES
ALL_INDEXES
USER_INDEXES
DBA_IND_COLUMNS
ALL_IND_COLUMNS
USER_IND_COLUMNS
DBA_IND_EXPRESSIONS
ALL_IND_EXPRESSIONS
USER_IND_EXPRESSIONS
DBA_IND_STATISTICS
ALL_IND_STATISTICS
USER_IND_STATISTICS
19
Oracle INDEXES
where
Size of INDEX
select segment_name,sum(bytes)/1024/1024/1024 as "SIZE in GB" from
user_segments where segment_name='INDEX_NAME' group by
segment_name;
OR
select owner,segment_name,sum(bytes)/1024/1024/1024 as "SIZE in GB"
from dba_segments where owner='SCHEMA_NAME' and
segment_name='INDEX_NAME' group by owner,segment_name;
List of Size of all INDEXES of a USER
select segment_name,sum(bytes)/1024/1024/1024 as "SIZE in GB" from
user_segments where segment_type='INDEX' group by segment_name
order by "SIZE in GB" desc;
OR
select owner,segment_name,sum(bytes)/1024/1024/1024 as "SIZE in GB"
from dba_segments where owner='SCHEMA_NAME' and
segment_type='INDEX' group by owner,segment_name order by "SIZE in
GB" desc;
Sum of sizes of all indexes
select owner,sum(bytes)/1024/1024/1024 as "SIZE in GB" from
dba_segments where owner='SCHEMA_NAME' and segment_type='INDEX'
group by owner;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only
20
Oracle INDEXES
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only