In MySQL, we can create indexes on one or more columns of a table to improve query speed.
Indexes help the database engine to locate and retrieve records or rows more efficiently.
The drawback of using Indexes is that it increases the storage space of the database and potentially reduces the speed of write operations in the Table.
Single-column index: An Index have been created through single column.
Composite or Multiple index: We can create composite or Multiple columns index in the MySQL Table.
Unique index: It ensures that all rows in the table have uniqueness by using the Unique index on records.
Full-text index: It used for full-text searches in text columns.
Spatial index: Used for spatial data types.
An index in a relational database is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure.
CREATE INDEX indexName ON tableName (column1, column2);
`indexName` is the name of the index.
`tableName` is the name of the table on which the index is being created
`(column1, column2)` specifies the columns on which the index is created.
using the below command, we can DROP INDEX from SQL Table.
DROP INDEX indexName;
Single Column indexes a single column of a table. It speeds up searches for values in that column.
CREATE INDEX index_product_name ON product (index_product_name);
Multiple Columns also known as a compound index, this type of index is created on multiple columns. It can speed up queries that filter, sort, or join based on combinations of these columns.
CREATE INDEX index_product_name_price ON products (product_name, price);
If we want to enforce uniqueness on the indexed columns, we can create a unique index:
CREATE UNIQUE INDEX index_unique_product_code ON products (product_code);
FULLTEXT index is a type of index that is specifically designed for searching text-based data efficiently.
CREATE FULLTEXT INDEX product_description ON products (product_description);
A Spatial index is a special type of index used to enhance the performance of spatial queries.
Spatial index is essentially used on data types, such as points, lines, and polygons. It helps retrieve geographic information systems (GIS) and applications involving location-based data.
CREATE SPATIAL INDEX indexName ON tableName (spatialColumn);