You are on page 1of 20

SQL indexes

INDEXING
• An index is a schema object.
• Used by the server to speed up the retrieval of rows by using
a pointer.

Advantages:
• It can reduce disk I/O(input/output) by using a rapid path
access method to locate data quickly.
• An index helps to speed up select queries and where clauses,
but it slows down data input, with the update and the insert
statements.
• Indexes can be created or dropped with no effect on the
data.
• Single-level indexing:
• Primary indexing
• Clustered indexing
• Secondary indexing
• Multi-level indexing:
• B Tree
• B+ Tree
Different indexing strategies

Dense Indexing

index entry is created for every search key value


• Sparse Indexing: If an index entry is created
only for some records, then it is sparse
indexing. Here's a diagram describing this
pictorially.
Primary indexing

• The first field -> primary key of data file.


• The second field is a pointer to the data block
where the primary key is available.
• Index created for the first record of each block
is called block anchors.
number of index entries = the number of
original data blocks
Clustering indexing
• A Clustering index is created on data file
whose file records are physically ordered on a
non-key field which does not have a distinct
value for each record
Secondary indexing

emp id,name,address
Creating an Index

Syntax:
CREATE INDEX index ON TABLE (column);

For multiple columns:


Syntax:
CREATE INDEX index ON TABLE (column1,
column2,.....);
Unique Indexes

• Used for the maintenance of the integrity of the


data present in the table as well as for the fast
performance

• It does not allow multiple values to enter into the


table.

Syntax:
CREATE UNIQUE INDEX index ON TABLE column;
When should indexes be avoided

• The table is small


• The columns are not often used as a condition
in the query
• The column is updated frequently
When should indexes be created

• A column contains a wide range of values.


• A column does not contain a large number of
null values.
• One or more columns are frequently used
together in a where clause or a join condition.
Removing an Index
• To remove an index from the data dictionary
by using the DROP INDEX command.

Syntax:
DROP INDEX index;
Altering an Index

• To modify an existing table’s index by


rebuilding, or reorganizing the index.
Syntax:
ALTER INDEX IndexName ON TableName
REBUILD;
Confirming Indexes

• Can check the different indexes present in a


particular table given by the user or the server
itself and their uniqueness.
Syntax:
select * from USER_INDEXES;
SQL - Transactions

• A transaction is a unit of work that is


performed against a database
• A transaction is the propagation of one or
more changes to the database
Properties of Transactions

• ACID

• Atomicity − ensures that all operations within the work unit are
completed successfully. Otherwise, the transaction is aborted at the point
of failure and all the previous operations are rolled back to their former
state.

• Consistency − ensures that the database properly changes states upon a


successfully committed transaction.

• Isolation − enables transactions to operate independently of and


transparent to each other.

• Durability − ensures that the result or effect of a committed transaction


persists in case of a system failure.
Transaction Control

• COMMIT − to save the changes.


• ROLLBACK − to roll back the changes.
• SAVEPOINT − creates points within the groups
of transactions in which to ROLLBACK.
• SET TRANSACTION − Places a name on a
transaction.

You might also like