The CREATE INDEX statement is used to create indexes in a database table. Indexes improve query performance by allowing the database to find data more quickly without scanning every row. They are particularly useful for columns that are frequently searched, sorted, or used in join conditions.
However, it is important to note that while indexes speed up retrieval, they can slow down data modification operations (like INSERT, UPDATE, or DELETE), as the index itself also needs to be updated.
Syntax for Creating Indexes:
-
Basic CREATE INDEX: This statement creates a non-unique index, meaning duplicate values are allowed in the indexed column(s).
CREATE INDEX index_name ON table_name (column1, column2, ...);index_name: The name you assign to the index.table_name: The name of the table where the index is created.column1, column2, ...: The list of columns to be indexed.
-
CREATE UNIQUE INDEX: This creates a unique index, meaning it ensures that the indexed column(s) do not contain duplicate values.
CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);- This type of index guarantees that no two rows can have the same value for the indexed columns.
Examples of Creating Indexes:
-
Basic Index Creation:
To create an index on the
LastNamecolumn of thePersonstable:CREATE INDEX idx_lastname ON Persons (LastName);This statement creates an index called
idx_lastnameon theLastNamecolumn. If there are frequent searches or sorting on this column, the index will speed up query performance. -
Composite Index Creation:
To create an index on a combination of columns, such as
LastNameandFirstName, in thePersonstable:CREATE INDEX idx_pname ON Persons (LastName, FirstName);This creates a composite index, which is useful when queries filter or sort based on multiple columns. The order of the columns matters; queries that use
LastNamefirst andFirstNamesecond will benefit most from this index. -
Unique Index Creation:
To create a unique index on the
Emailcolumn, ensuring no duplicate email addresses are allowed in theUserstable:CREATE UNIQUE INDEX idx_email ON Users (Email);This creates a unique index on the
Emailcolumn. It ensures that all email addresses in theUserstable are unique.
DROP INDEX Statement:
The DROP INDEX statement is used to delete an index from a table. The syntax for this command is:
ALTER TABLE table_name
DROP INDEX index_name;
table_name: The name of the table from which the index will be dropped.index_name: The name of the index to be removed.
Example of Dropping an Index:
To drop the idx_lastname index from the Persons table:
ALTER TABLE Persons
DROP INDEX idx_lastname;
This removes the idx_lastname index from the Persons table. After dropping the index, queries that previously benefited from the index may perform slower.
Considerations:
- Performance Trade-offs: While indexes speed up SELECT queries, they can slow down
INSERT,UPDATE, andDELETEoperations because the index also needs to be updated. - Choosing Indexed Columns: It's advisable to create indexes on columns that are frequently used in
WHEREclauses,ORDER BY,JOINconditions, or for quick lookups. - Indexing Multiple Columns: A composite index can improve performance for queries involving multiple columns, but the order of columns in the index should align with the most common query patterns.
Summary:
- CREATE INDEX: Speeds up queries but adds overhead for data modifications.
- CREATE UNIQUE INDEX: Ensures column values are unique.
- DROP INDEX: Removes an index from a table.
- Composite Index: Useful when queries involve multiple columns in the same condition.