Updated June 3, 2023
Introduction to MySQL Index Types
The following article provides an outline for MySQL index types. MySQL Index is a type of database structure that is responsible for progressing the speed of table operations. In MySQL, the Indexes can be formed by using one or many columns, which provides the base for both speedy casual lookups and proficient assembling of access to records. Indexes are essentially used in the server database tables to swiftly find the table rows with specific table column values. If an index is absent in the table, MySQL needs to scan the entire table to locate and check the appropriate table rows.
This may cause the search process to be much slower when the table and data are large. Therefore, MySQL Indexes and their various types help create, remove, search, and fetch a list of records by scanning the particular values in the table data. Indexes are not directly viewable by users since their primary benefit is to optimize SQL queries and facilitate rapid record retrieval. Database Search Engines implement indexes to locate records swiftly.
Different Types of Index in MySQL
While building up an index, a user should take into deliberation which all table columns will be implemented to write SQL queries having WHERE clause swiftly and form one or multiple indexes on those table columns.
The statements such as INSERT and UPDATE consume more time on tables with indexes. On the other hand, the statement SELECT performs faster on those tables because, during the insert or update query execution, the server also performs insert or update related index values. Also, it optimizes queries such as ORDER BY and GROUP BY with Constraints and MAX() and MIN().
In MySQL, we have three different methods for creating indexes:
a. Using the keyword CREATE INDEX.
CREATE INDEX Indexname ON Tablename (indexcolumn1, indexcolumn2, ….);
b. Using the keyword CREATE TABLE to create a table.
CREATE TABLE Tablename (column1 CHAR (30) NOT NULL, INDEX (column2));
c. Using keyword ALTER TABLE.
ALTER TABLE Tablename ADD INDEX (column1, column2);
Now, let us see the types of MySQL Indexes where each one of all six types server for diverse purposes:
The Unique index in MySQL specifies that all values in the columns of a table must be distinct when applied. There can be no duplicate values in the indexed column existing in a single-column unique index. Whereas in a multi-column unique index, column values can be replicated in a single table column, the grouping of column values in every row should be unique. Hence, this Unique index is helpful to avoid the duplicity of values in the columns, and generally, we define the MySQL Index after we have created the table.
CREATE UNIQUE INDEX Indexname ON Tablename(indexcolumn1, indexcolumn2,…); OR, CREATE TABLE Tablename(Column1 CHAR (30) NOT NULL, UNIQUE INDEX (column2)); OR, ALTER TABLE Tablename ADD UNIQUE INDEX (column1, column2);
2. Primary Key
Primary Key is a type of Unique Index that specifies that the column mentioned as Primary key must contain no NULL value. Each table row should have a value for the table or group of columns. As a result, we must typically define a primary index on the lowest number of columns possible. In most cases, the primary key is set on a single column. Modifying the primary key is impossible once the column values are set in.
Let us view the query below to create the index – primary key:
CREATE TABLE Tablename (Columnname Data_type PRIMARY KEY ); OR, CREATE TABLE Tablename (Column1 CHAR(30) NOT NULL, Column2 CHAR(30) NOT NULL, PRIMARY KEY (Column1, Column2)); OR, ALTER TABLE Tablename ADD PRIMARY KEY(Column1, Column2);
3. Simple | Regular | Normal
In this type of Index named simple, regular, or normal, the specified column values do not require to be unique and can be NULL. They essentially supplement the database searching process to make record retrieval faster.
Find the query code below to create the simple, regular, and normal types of index:
CREATE INDEX Indexname ON Tablename (Indexcolumn1, Indexcolumn2, …); OR, CREATE TABLE Tablename(Column1 CHAR(30) NOT NULL, INDEX(Column2)); OR, ALTER TABLE Tablename ADD INDEX(Column1, Column2);
This index type is implemented for full-text searches, as the name implies. Occasionally, suppose you need to find a blob that includes a certain word or combination of words, or it can even be a substring within the higher block text. Search engines and e-commerce platforms widely implement and consider the full-text index vital for their operations. But these indexes are maintained only for MyISAM and InnoDB tables in the database and can consist of only VARCHAR, CHAR, and TEXT table columns.
Find the query below to create a Full-text index in MySQL:
CREATE FULLTEXT INDEX Indexname ON Tablename(indexcolumn1, indexcolumn2, …); OR, CREATE TABLE Tablename (column1 TEXT, FULLTEXT(column1)); OR, ALTER TABLE Tablename ADD FULLTEXT(column1, column2);
Spatial indexes in MySQL are a new index type and have not gained widespread usage. MySQL allows the creation of Spatial indexes on geometry-valued columns with NOT NULL constraint. The spatial index creates an R-tree index. The engine builds up a B-tree index for storage engines that maintain nonspatial indexing of the spatial table columns. This B-tree index applied to spatial values provides benefits for precise value lookups, but not for the range scans. The optimizer may apply spatial indexes to columns that have SRID restrictions.
Below is the code to query the Spatial Index:
CREATE INDEX Indexname ON Tablename(Indexcolumn1); OR, CREATE TABLE Tablename (column1 GEOMETRY NOT NULL SRID 4326,SPATIAL INDEX(Column2)); OR, ALTER TABLE Tablename ADD SPATIAL INDEX (column1, column2);
Version 8+ of MySQL offers the Descending index, a consistent index stored in the inverse order and only available. It supports users when they run queries to find out the most newly added data, such as viewing the six newest posts or the five most current comments on all the posts. Scanning the indexes in inverse order previously has caused a performance issue. It will be more resourceful to use the descending index in onward order. The optimizer is likely to apply multiple table column indexes due to its descending index, and it does this by fusing the most effective scan order with ascending order for some table columns and descending order for others.
View the following query to create Descending index:
CREATE INDEX Indexname ON Tablename (Indexcolumn1 DESC); OR, CREATE TABLE Tablename (Column1 INT, Column2 INT, INDEX asc_index1(Column1 ASC, Column2 ASC), INDEX desc_index1 (Column1 DESC, Column2 DESC)); OR, ALTER TABLE Tablename ADD INDEX(column1 DESC, column2 ASC);
A MySQL Index is a technique for tuning the performance for quicker retrieval of data records. In the table, an index makes an insert for every value, which displays in the indexed table column. Indexes are essential for table operations also. Indexes in MySQL practically store the index field or primary key and a pointer to every record row in the authentic table.
We hope that this EDUCBA information on “MySQL Index Types” was beneficial to you. You can view EDUCBA’s recommended articles for more information.