top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Index in SQL


 

SQL commands are widely utilized to communicate with databases, allowing users to execute various operations on database systems. Mastering these commands is essential for efficiently managing and manipulating data.

There are various SQL sublanguage commands, such as Data Definition Language (DDL), Data Query Language (DQL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL)


Similarly, in Data Definition Language (DDL), the CREATE command can utilize an Index to facilitate data retrieval. For instance, if you want to find all the pages in a book that discuss a specific topic, you would start by looking at the index, which organizes topics in alphabetical order. From there, the index will direct you to the exact page numbers where that topic is covered, allowing you to quickly locate the information.

In this blog, we'll take a closer look at SQL indexes, explore the various types available, walk through the steps to create them, and share best practices for maximizing their effectiveness.

 

What is Index?

Indexes help retrieve data from a database much faster than searching without them. They are not visible to users, as their primary purpose is to enhance the speed of searches and queries.

Indexes are specialized data structures that databases use to accelerate data retrieval. Essentially, an index acts as a pointer to data stored in a table, allowing the database engine to find specific records more quickly. Think of a database index as similar to an index at the back of a book; it provides a quick way to locate information without having to search through every page. Users don’t see the index itself; it's used behind the scenes to enhance the speed of database queries.





Types of SQL Index

There are two main types of databases indexes Clustered and Non-clustered. Both clustered and non-clustered indexes are organized and searched using B-trees, a data structure similar to a binary tree. A B-tree is a "self-balancing tree data structure" that keeps data sorted and enables efficient searches, sequential access, insertions, and deletions in logarithmic time. Essentially, it forms a tree-like structure that arranges data for fast searching.

 

1.      Clustered-

A clustered index is created only when both the following conditions are satisfied:

  •  The data or file, that you are moving into secondary memory should be in sequential or sorted order.

  • There should be a key value, meaning it can not have repeated values.

 

 

If you apply the primary key to any column, then automatically it will become a clustered index. 

 

Key Differences:

  • Only one clustered index is allowed per table.

  • The clustered index directly affects the physical ordering of data.

  • It offers faster retrieval but may slow down insert and update operations.


2.      Non-clustered-

A non-clustered index is like the index of a book. Just as a book’s index lists chapter names and page numbers, allowing you to jump directly to the relevant page without flipping through the entire book, a non-clustered index lets you quickly locate data within a database. The actual data is stored in one location, while the index is stored elsewhere. Because the data and the non-clustered index are stored separately, you can create multiple non-clustered indexes for a single table.

 


Various types of indexes in SQL server:


   B-Tree

  • A B-Tree (Balanced Tree) index is a self-balancing tree data structure.

  • It ensures that all leaf nodes are at an equal depth.

  • It is designed to keep data sorted and support efficient searches, sequential access, insertions, and deletions, all in logarithmic time.

  • It is particularly well-suited for range queries, equality searches, and sorting tasks.


Syntax : CREATE INDEX index_name ON table_name (column_name);

Example : CREATE INDEX customer_id_index ON customer (customer_id);

 

  Hash

  • A hash index utilizes a hash function to transform indexed columns into unique hash codes.

  • It is optimized for rapid equality searches.

  • It is not ideal for range queries or sorting operations.

  • It is commonly applied to columns with unique values or low cardinality.


Syntax : CREATE INDEX index_name ON table_name USING HASH (column_name);

Example : CREATE INDEX employee_id_hash_index ON employee USING HASH (employee_id);

 

GiST

  • GiST stands for Generalized Search Tree.

  • A versatile and balanced tree structure that adapts to different data types and query methods.

  • It is not restricted to any particular data type, offering broad applicability.

  • It supports a diverse range of queries, including those involving non-standard data types, such as geometric data.

 

Syntax : CREATE INDEX index_name ON table_name USING GiST (column_name);

Example : CREATE INDEX geom_idx ON spatial_table USING GiST (geom);

 

SP-GiST

  • SP-GiST stands for Space-Partitioned Generalized Search Tree.

  • A unique type of GiST index tailored for managing partitioned search trees.

  • It effectively manages data that is inherently divided into segments or partitions.

  • It is especially beneficial for handling non-overlapping, distinct data types.


Syntax : CREATE INDEX index_name ON table_name USING SPGIST (column_name);

Example : CREATE INDEX location_idx ON spatial_data USING SPGIST (location);

 

  GIN

  • GIN stands for Generalized Inverted Index.

  • Built to manage columns with multiple values, like arrays, JSONB, or full-text search data.

  • Enhances performance for queries that search for elements within a collection.

  • Ideal for handling many-to-many relationships and efficient in text search operations.


Syntax : CREATE INDEX index_name ON table_name USING GIN (column_name);

Example : CREATE INDEX tags_idx ON articles USING GIN (tags);

 

  BRIN

  • BRIN stands for Block Range Index.

  •  Engineered to efficiently manage very large tables by summarizing ranges of physical blocks.

  • Instead of indexing each row, it stores a condensed summary of data for these block ranges.

  • Perfect for columns with inherently ordered data, like time-series or data inserted sequentially


Syntax : CREATE INDEX index_name ON table_name USING BRIN (column_name);

Example : CREATE INDEX orders_date_brin_idx ON orders USING BRIN (order_date);

 

 

When to Use Indexes:

Indexes prevent duplicate entries in the column or combination of columns on which it is created. Since SQL indexes are primarily a performance tool, they are most useful when a database grows in size.

 

  •   Frequent Query Columns

Columns frequently used in WHERE clauses.

  • High Read Operations

Tables with frequent read operations.

  • Sorting and Grouping

Columns used for sorting and grouping results.

 

Basic Create Index Command:

This command is used to create a table index in SQL by employing the build index expression. When the table is created, it allows duplicate data to be accepted. Indexing accelerates query performance on columns by creating pointers that direct to where the data is stored in the database.


Syntax: CREATE INDEX name_of_the_index ON name_of_the_table

 

Different ways of creating indexes :

Single-Column Index

It is an index created on a single column of a database table. It is one of the most common types of indexes used in database systems and is created to improve the performance of queries that involve searching,sorting, or filtering based on the values in that specific column.

Syntax : CREATE INDEX index_name ON table_name (column_name);

Example : CREATE INDEX customer_id_index ON customer (customer_id);


Multicolumn Index

Multicolumn indexes ,also known as a composite index are used to speed up queries that involve multiple columns in the WHERE clause, ORDER BY clause, or JOIN conditions. By creating an index that covers multiple columns, the database engine can efficiently retrieve the relevant rows based on the combined values of those columns.

Syntax : CREATE INDEX index_name ON table_name (column1_name, column2_name);

Example : CREATE INDEX customer_name_index ON customer (first_name, last_name);

 

Unique Index

The primary purpose of a unique index is to enforce data integrity by preventing duplicate values in the indexed columns. This ensures that each row in the table has a unique combination of values in the indexed columns.

Syntax : CREATE UNIQUE INDEX index_name on table_name (column_name);

Example : CREATE UNIQUE INDEX email_index on users (email);


Partial Index

Partial indexes are used to improve query performance for specific subsets of data within a table. They are particularly useful in scenarios where only a portion of the table is frequently queried or where certain conditions apply.

Syntax : CREATE INDEX index_name ON table_name (column_name) WHERE condition;

Example : CREATE INDEX active_customers_index ON customer (customer_id) WHERE active = 1;

 

Drop Index :

  • Dropping an index removes it from the database schema.

  • Consider the impact on query performance, storage space, and data integrity before dropping an index.

  • Use the DROP INDEX statement with the index name to remove an index from the database.


  Example Situation :

The orders_date_brin_index index was created for optimizing queries on the order_ column.

Due to changes in data distribution or query patterns, the index is no longer providing significant performance benefits.


Decision: Drop the index to reduce storage overhead and simplify database maintenance.

SQL Statement : DROP INDEX orders_date_brin_idx;

 

Conclusion :

Indexes play a crucial role in optimizing SQL query performance. Proper implementation and upkeep of indexes can greatly boost database efficiency. It's important to balance the advantages of quicker data retrieval with the potential downsides, such as slower write operations and increased storage requirements.


 

Reference Links:


33 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page