SQL INDEX

Back to home SQL INDEX
Logicmojo - Updated jan 28, 2024



Introduction

The amount of data saved in the database's tables is rather vast. In practice, seeking and retrieving such large amounts of data causes time limits. We employ a variety of approaches to make data searching and retrieval easier, and indexes are one of them. Because the system determines the searching algorithm in SQL, indexing is quite useful.

Indexes are stored internally and are not visible to users. Indexes are similar to unique labels assigned to data in order to facilitate data identification when a search query is issued. In the industry, indexing is a fairly popular strategy for dealing with enormous datasets.


What Is INDEX?

An index is a structure that contains the field that the index is sorting as well as a pointer from each record to its corresponding record in the initial table where the data is stored. Indexes are used in things like contact lists, where the data may be physically maintained in the order in which you add people's contact information, but it is easier to discover them when they are listed out alphabetically.

Assume you wish to find a piece of information in a vast database. The computer will search through every row in the database until it discovers this information. If the data you're looking for is near the end of the list, this query will take a long time to run.

What is SQL INDEX ?

A SQL index is used to quickly retrieve data from a database. Indexing a table or view is without a doubt one of the most effective techniques to increase query and application performance. A SQL index is a quick lookup table used to find records that users commonly search for. An index is designed to be short, fast, and optimised for speedy lookups. It is extremely handy for linking relational tables and searching huge tables.

SQL indexes are essentially a performance aid, hence they are especially useful when a database grows in size. The clustered index is one of the most frequent types of indexes supported by SQL Server. This type of index is built automatically using a main key.

It is the most effective SQL approach for increasing query performance. The disadvantage of employing indexes is that they increase the time it takes for UPDATE and INSERT commands to execute. They do, however, offer one advantage in that they shorten the execution time of SELECT and WHERE statements.

In SQL, an index is generated on the table's fields. On a table, we can easily create one or more indexes. The creation and deletion of the Index have no effect on the database's data.

The index can be used in two ways in SQL:

  1. CREATE INDEX

  2. DROP INDEX





Learn More

CREATE INDEX in SQL

This command is used in SQL to generate the table index using the build index expression. It will allow you to accept duplicated data onto the table when you create the table. By creating pointers to where data is kept within a database, indexing makes columns faster to query.

Assume you need to find a certain piece of information in a massive database. The machine will search every row in the database until it discovers this data. If the data you're looking for is near the end of the list, this query will take a long time to run.

Basic Syntax for CREATE INDEX

CREATE INDEX index_name
ON table_name (column1, column2, ...columnN);                       

CREATE INDEX for Single Column

A Single-column index is built using only one table column. Use the syntax below to generate an index for a one-way column from a table.

Syntax

CREATE INDEX index_name
ON table_name (column_name);                      

Example of CREATE INDEX

Lets take Customer table as an example :-


SQL INDEX

Now create index in the customer table with the country column,

CREATE INDEX customer_index
ON customers (country);                      

UNIQUE CREATE INDEX

Unique indexes are utilised for data integrity as well as performance. A unique index prevents duplicate values from being added into the table.

Syntax

CREATE UNIQUE INDEX index_name
on table_name (column_name);                       

Composite CREATE INDEX

A composite index is defined as an index on two or more columns of a table by Index in SQL. It is possible that it will generate the same index with a different number of columns.

Syntax

CREATE INDEX index_name
on table_name (column_name1, column_name2);                      

Consider whether to establish a single-column index or a composite index based on the column(s) that you may utilise frequently as filter conditions in a query's WHERE clause.

If only one column is used, a single-column index should be used. If two or more columns are often used as filters in the WHERE clause, the composite index is the ideal option.

Implicit INDEX

Implicit indexes are indexes that the database server creates automatically when an object is created. Indexes are generated automatically for primary key constraints and unique constraints.

Syntax

CREATE INDEX IN_Name 
ON TN_Name(column_name1);                      

Advantages of Indexing:

  1. Indexing speeds up query results and data retrieval.

  2. Indexing facilitates faster record sorting and grouping.

  3. Some indexing employs sorted and unique keys, which aids in the retrieval of sorted queries.

  4. Because index tables are smaller in size, they use less memory.

  5. Index tables are kept in main memory since they are less in size.

  6. Because the CPU speed and secondary memory speed are so different, the CPU employs this main memory index database to bridge the speed gap.

  7. Indexing contributes to higher CPU utilisation and performance.

SQL DROP INDEX

To delete an index, use the SQL DROP command. Be cautious while reducing an index because performance may either slow or improve. In most cases, this operation is used to delete the index on the provided data table.

Syntax

DROP INDEX index_name
ON table_name;                    

The following are the syntaxes for removing an index from different relational database:


SQL Server

DROP INDEX table_name.index_name;                    


MySQL

ALTER TABLE Table_Name 
DROP INDEX Index_Name;                    


Oracle, PostgreSQL

DROP INDEX Index_Name;                      

Rename an INDEX

Using the ALTER command, we can quickly rename the index of the table in the relational database.

Syntax

ALTER INDEX old_Index_Name 
RENAME TO new_Index_Name;                   

Why do we need indexing ?

Indexing is a crucial issue when discussing advanced MySQL; while most people grasp its concept and usage, they don't understand when and when to apply it to drastically improve the efficiency of our queries or stored procedures.

The following arguments explain why an index is required in SQL:

  1. SQL Indexes help swiftly search a large database's information.

  2. This is a short method for those columns with varying values.

  3. This data structure sorts column (field) data values in ascending or descending order. The entry is subsequently assigned to each value.

  4. Each Index table only has two columns. Row_id is the first column, while indexed-column is the second.

  5. When indexes are used with smaller tables, the index's performance may be overlooked.

When should indexes be avoided?

Although indexes are designed to improve database efficiency, they should be avoided at times.

  1. The criteria below indicate when using an index should be evaluated.

  2. On small tables, indexes should not be used.

  3. Tables with a high volume of massive batch updates or insert operations.

  4. Indexes should not be utilised on columns with a large percentage of NULL values.

  5. Indexing should be avoided for frequently modified columns.

How Does the Database Know Which Other Table Fields to Return?

The index table, as previously stated, has two columns. The first column contains the key that points (or refers) to a specific piece of information. Using the student table as an example, we can see that a specific roll number index will refer to a table entry. The record may have multiple columns or properties. As a result, the database may learn about the rest of the table's properties by using an index.

Conclusions

An index is a table that aids in the retrieval of data from the database and the generation of speedier query results. Index tables employ indexing, a technique that employs data structures to reduce the time required to complete a database query. Indexing improves database efficiency. It also takes up less space in the main memory. The database uses the column specified by the CREATE INDEX command to sort the column values using the B-tree data structure. Because index tables are smaller in size, they use less memory. Index tables are kept in main memory since they are less in size. Because the CPU speed and secondary memory speed are so different, the CPU employs this main memory index database to bridge the speed gap.

Good luck and happy learning!