×

MySQL Composite Index

Composite Index

A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query called a composite index. It improves the speed of query performance during data retrieval.

This index contains a maximum of sixteen columns in one index. MySQL query optimizer uses this index to test the index column. This index is also known as a multiple-column index. This index assigns the column in the correct order.

Syntax

The basic syntax of the composite index shows below.

INDEX key name (column1, column2, column16)

The syntax of the MySQL composite index shows below.

 CREATE TABLE tname (
 Column1 data type NOT NULL PRIMARY KEY,
 Column2 datatype NOT NULL,
 Column3 datatype NOT NULL,
 INDEX iname (Column1, Column2, Column3)); 

The syntax of the MySQL composite index shows below.

 CREATE TABLE name (
 Column1 data type NOT NULL PRIMARY KEY,
 Column2 data type NOT NULL,
 Column3 data type NOT NULL,
 Column16 data type NOT NULL,
 INDEX iname (Column1, Column2, Column3, ...Column16)); 

The syntax of the MySQL composite index with the existing table shows below.

CREATE INDEXindex_nameON table name (Column1, Column2, Column3, ...Column16));

Examples of the Composite index

1) Example: The basic Composite indexexample shows below.

Execute the below query to understand the composite index in the MySQL table. Here, we have used two columns in a single index name.This index places the column in an ordered way in the table.

 mysql> create table index_table(
     ->roll_number INT NOT NULL PRIMARY KEY,
     -> phone INT NOT NULL ,
     ->name VARCHAR(10) NOT NULL,
     ->mark text NOT NULL,
     ->img blob NOT NULL,
     -> INDEX composites (roll_number, phone)); 

OUTPUT

Execute the below query to get the composite index and its information.

mysql> show index from index_table;
MySQL: Composite Index

The above output image shows a composite index of the column. The table shows the column name and sequence of the indexes. In addition, the output image also displays other index information in the table.

2) Example: The multiple Composite indexexample shows below.

Here, we have used four columns in a single index name. This index places an ordered column in the table.

 mysql> create table index_table(
     ->roll_number INT NOT NULL PRIMARY KEY,
     -> phone INT NOT NULL ,
     ->name VARCHAR(10) NOT NULL,
     ->mark text NOT NULL,
     ->img blob NOT NULL,
     -> INDEX composites (roll_number, phone, name, mark(10), img(200))); 

OUTPUT

Execute the below query to get the composite index and its information.

mysql> show index from index_table;
MySQL: Composite Index

The above output image shows the given composite index. The table image shows the key name and sequence of the indexes. In addition, the output image displays the column name and other index information in the table.

3) Example: The unordered Composite indexexample shows below.

Here, we have used two columns in a single index name. This index places an unordered column in the table.

 mysql> create table index_table(
     ->roll_number INT NOT NULL PRIMARY KEY,
     -> phone INT NOT NULL ,
     ->name VARCHAR(10) NOT NULL,
     ->mark text NOT NULL,
     ->img blob NOT NULL,
     -> INDEX composites (phone, mark(10), img(200),name)); 

OUTPUT

Execute the below query to get the composite index and its information.

mysql> show index from index_table;
MySQL: Composite Index

The table image shows the index name and sequence of the indexes. The above output image shows the given composite index for different columns. The image displays column names and other index information in the table. The sequence number shows as per order requirements.

4) Example: The composite index with the existing table example shows below.

Here, we have used three columns in a single index name. This index places an unordered column in the existing table.

mysql> CREATE INDEX composites ON index_table (phone, name, roll_number);

OUTPUT

Execute the below query to get the composite index and its information.

mysql> show index from index_table;
MySQL: Composite Index

The above output image shows the index name, column name, and sequence of the indexes. The output image shows the given composite index and its columns. The image displays column names and other index information in the table. The sequence number shows as per order requirements.


Related Topics

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL LOAD_FILE() function

In this context, we will learn how we can use the MySQL LOAD_FILE() function with proper syntax and good examples. Introduction of MySQL LOAD_FILE() function The LOAD_FILE() function in MySQL reads a...

3 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL LOG() function

In this context, we will learn how we can use the MySQL LOG () function with proper syntax and good examples. Introduction of MySQL LOG() function The LOG() function in MySQL is...

3 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL Character Length Function

In this context, we will learn how we can use the MySQL CHARACTER_LENGTH() function with proper syntax and good examples. Introduction of MySQL CHARACTER_LENGTH() function CHARACTER_LENGTH() function in MySQL is used to...

2 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.