Index in database
Indexes are database objects used to improve the retrieval performance of data from tables by providing efficient access paths to the data. In SQL databases, there are several types of indexes, each with its own purpose and advantages. Here, I'll explain the most common types of indexes with examples and SQL code:
-
Single-Column Index:
- A single-column index is created on a single column of a table.
Example:
CREATE INDEX idx_lastname ON employees (last_name);In this example, we create an index named
idx_lastnameon thelast_namecolumn of theemployeestable. -
Composite Index:
- A composite index is created on multiple columns of a table.
Example:
CREATE INDEX idx_fullname ON employees (last_name, first_name);Here, we create an index named
idx_fullnameon both thelast_nameandfirst_namecolumns of theemployeestable. Composite indexes are useful for queries that involve multiple columns. -
Unique Index:
- A unique index enforces the uniqueness constraint on the indexed column(s), preventing duplicate values.
Example:
CREATE UNIQUE INDEX idx_email ON users (email);This creates a unique index named
idx_emailon theemailcolumn of theuserstable. It ensures that no two rows can have the same email address. -
Clustered Index (SQL Server) or Primary Key (most databases):
- A clustered index determines the physical order of data rows in a table. In most databases, a table can have only one clustered index.
Example:
CREATE CLUSTERED INDEX idx_employees_id ON employees (employee_id);In SQL Server, this creates a clustered index named
idx_employees_idon theemployee_idcolumn of theemployeestable. Rows in the table will be physically ordered byemployee_id. -
Non-Clustered Index:
- A non-clustered index is a separate structure from the table data and provides a logical order for quick data retrieval.
Example:
CREATE NONCLUSTERED INDEX idx_salary ON employees (salary);This creates a non-clustered index named
idx_salaryon thesalarycolumn of theemployeestable. It allows for efficient lookups based on salary values. -
Full-Text Index (SQL Server and some other databases):
- A full-text index is used for searching text data efficiently, providing features like keyword searching and proximity searches.
Example:
CREATE FULLTEXT INDEX idx_product_description ON products (product_description);This creates a full-text index named
idx_product_descriptionon theproduct_descriptioncolumn of theproductstable. It's useful for text-based search queries. -
Spatial Index (for spatial databases like PostgreSQL with PostGIS):
- A spatial index is used to optimize spatial queries on geographic or geometric data.
Example:
CREATE INDEX idx_geom_location ON spatial_data USING GIST (geom);This creates a spatial index named
idx_geom_locationon thegeomcolumn of thespatial_datatable using the GIST index type. -
Bitmap Index (used in some databases for specific cases):
- A bitmap index stores a bitmap for each distinct value in a column. It's suitable for columns with low cardinality.
Example:
CREATE BITMAP INDEX idx_category ON products (category);Here, we create a bitmap index named
idx_categoryon thecategorycolumn of theproductstable.
These are some of the common types of indexes you may encounter in SQL databases. The choice of index type depends on the specific requirements of your application and the types of queries you need to optimize.
What's Your Reaction?