An index is a unique lookup table in SQL databases that the database search engine can use to expedite data retrieval. When an index is built on a table’s columns, the database can locate rows considerably more quickly than in the absence of an index. See an index as a book’s reference guide; it will assist you in finding the information you require quickly and save you from reading the full text.
Indexes are essential when it comes to improving SQL query performance. They are helpful when there is a lot of data in the database. In the absence of indexes, the database engine would have to run a full table scan, going over every row to see which ones satisfy the requirements of the query. It can take a long time to do this. Indexes allow the engine to find the relevant rows quickly, which speeds up the process considerably.
Here are the types of indexes:
In SQL, you can implicitly or explicitly specify which indexes to use in your queries. Let’s see them below:
When constructing indexes on a table, the SQL query optimizer automatically selects the appropriate indexes for a specific query. This decision is based on several parameters, including the query structure, table statistics, and index availability. Because the optimizer can typically determine which index to utilize best, this is the most popular and recommended technique for using indexes.
You may wish to force the query optimizer to use a particular index in certain situations. This can be helpful if you know that a specific index is more effective for your query or if you believe the optimizer is using the incorrect index because of out-of-date statistics or other factors. The index can be explicitly specified by using the USE INDEX hint.
Also read: SQL: A Full Fledged Guide from Basics to Advance Level
Let’s first create a table and sample it with sample records.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Gender CHAR(1),
Email VARCHAR(100),
HireDate DATE
);
INSERT INTO Employees (EmployeeID, FirstName, LastName, Gender, Email, HireDate) VALUES
(1, 'John', 'Doe', 'M', '[email protected]', '2020-01-15'),
(2, 'Jane', 'Smith', 'F', '[email protected]', '2019-07-10'),
(3, 'Alice', 'Johnson', 'F', '[email protected]', '2021-03-22'),
(4, 'Bob', 'Williams', 'M', '[email protected]', '2018-11-30'),
(5, 'Charlie', 'Brown', 'M', '[email protected]', '2022-05-17');
When a primary key is defined on a table in an SQL database, a clustered index is frequently automatically created in that primary key field. This indicates that the data rows are physically kept on the disc according to the values of the main keys.
We can verify the existence of the primary key and associated index using the SHOW INDEX command
SHOW INDEX FROM Employees;
From the above image, we can see a key called PRIMARY under the column EmployeeID.
CREATE INDEX idx_lastname
ON Employees (LastName);
The above code will create a non-clustered index in the column LastName.
CREATE UNIQUE INDEX idx_unique_email
ON Employees (Email);
The above code will create a unique key in the email column, and it will be named idx_unique_email.
CREATE INDEX idx_composite_name
ON Employees (FirstName, LastName);
The above code creates a composite key using the columns FirstName and LastName.
Here are queries with Indexes:
The below query will use a non-clustered index on LastName, thereby increasing the operation speed.
EXPLAIN SELECT * FROM Employees WHERE LastName = 'Smith';
We have used the explain clause to learn about the query, and we can see that it uses the idx_lastname key.
EXPLAIN SELECT * FROM Employees WHERE FirstName = 'Jane' AND LastName = 'Smith';
The above image shows that it is using the key idx_lastname, but idx_composite_name can also be used. It will automatically pick the best key according to the query.
EXPLAIN SELECT * FROM Employees WHERE Email = '[email protected]';
In the above code, SQL will use idx_unique_email to query.
Also read: SQL For Data Science: A Beginner Guide!
The code below can be used to drop a non-clustered index on LastName.
DROP INDEX idx_lastname ON Employees;
The code below can be used to rebuild the unique index on Email.
ALTER TABLE Employees DROP INDEX idx_unique_email;
CREATE UNIQUE INDEX idx_unique_email ON Employees (Email);
This code can disable the composite index on FirstName and LastName.
ALTER TABLE Employees DROP INDEX idx_composite_name;
In conclusion, the SQL query optimizer usually implicitly uses indexes, choosing the optimum course of action depending on the information. In some cases, explicitly declaring indexes with USE INDEX can be helpful; nevertheless, this should be done cautiously and usually as a last option. A useful tool for comprehending and improving index utilisation in your queries is the EXPLAIN statement.
Ans. The database search engine uses an SQL index, which is a unique lookup table, to expedite data retrieval. Enabling the database to locate entries more rapidly than it could without an index enhances query performance.
Ans. Because they improve SQL query performance, especially in databases with vast volumes of data, indexes are crucial. They eliminate the need for full table scans by enabling the database engine to find pertinent information swiftly.
Ans. A composite index is an index on multiple columns. It is useful for queries that filter or sort based on multiple columns.
Ans. Yes, a table can have multiple indexes, including both clustered and non-clustered indexes. However, a table can have only one clustered index.
Ans. A unique index ensures that all values in the indexed column(s) are unique. It is often used to enforce the uniqueness of values in a primary key column.