Introduction to MySQL Clustered Index
Normally, indexes in MySQL are a distinct data structure that is implemented to find table rows with particular table column values swiftly. Basically, an index is a B-Tree structure which is used to store the key values applied for quicker lookups in the MySQL databases. MySQL clustered index, on the other side, is explained as an index which administers the ordering on the table rows physically. Actually Clustered index is explained as the index that is managed in InnoDB tables in MySQL. If we have created a clustered index, then all the table rows will be stored on the basis of the key columns implemented while creating the clustered index. This is because the table rows are stored in an organized order by the clustered index where every distinct table have only a single clustered index.
We have the following basic structure of syntax to be followed for creating a clustered index but you should note first that before we define a column as clustered index make sure that we have removed the previous one. It means that when a column is defined as a primary key will make that column a clustered index of that table, so to make any other table column as clustered index initially we need to delete the previous one as follows:
DROP INDEX TableName.IndexName
//Creating Clustered Index
CREATE Clustered Index IndexName_TableName_ColumnName
ON TableName(ColumnName ASC)
How does Clustered Index work in MySQL?
- Each database InnoDb table needs a clustered index basically it represents the Primary key that supports in data handlings like INSERT, UPDATE, SELECT and DELETE. Therefore, whenever a user states a PRIMARY Key in the database for an InnoDBtable, then MySQL practices the Primary key as MySQL clustered index.
- Suppose, in case the table does not contain any primary key for a specific table then, the MySQL will examine for the initial UNIQUE index which defines all the key columns specifies NOT NULL attribute and applies this UNIQUE index as MySQL Clustered index.
- Also, if the InnoDB table contains no Primary Key or any UNIQUE key index, then MySQL will internally produce a concealed clustered index as GEN_CLUST_INDEX on a fake column that consists the row ID values. Therefore, we can say that each database InnoDB table contains only a single clustered index always.
- Now, all the remaining indexes other than clustered index are defined as secondary indexes or non-clustered indexes. Each record in an InnoDB table in the secondary index consists the Primary key columns for the columns as well as rows stated in the non-clustered index. Hence, MySQL will use this primary key value in the clustered index for the row lookups.
- Thus, it is essential that a short primary key is present otherwise when MySQL implements the secondary indexes then it will take more space. Usually in MySQL, a table column with the auto-increment integer attribute is defined for the primary key column.
- A clustered index table is helpful to store indexes and data at the same time which is based on key values sorted in one direction. MySQL clustered index can use one or multiple column to create an index in a database table.
- Using, MySQL Clustered Index, the cache hits are maximized and the page transfer is minimized.
- MySQL Clustered index is an ultimate option for group or range with min, max as well as count queries.
- In the beginning of the range, it implements a location mechanism to find an index entry.
- Also, clustered index supports for fragmentation and other operations.
- It includes several insert records present in a non-sequential direction.
- It creates various constant page separations like index pages or data page.
- It constantly takes lengthy time to update the table records.
- It requires additional work for query statements such as updates, inserts and deletes.
Examples of MySQL Clustered Index
Given below are the examples of MySQL Clustered Index:
Suppose, we are a creating a table named Training using the CREATE command as follows:
CREATE TABLE Training(TID INT PRIMARY KEY AUTO_INCREMENT, Label VARCHAR(255), Information TEXT) ENGINE = INNODB;
Here, you can see that after mentioning the columns and data types with related attributes, we have also added engine i.e. InnoDB to make sure that the table create in MySQL in InnoDB apart from other engine like MyISAM and others.
Again, we have added here FULLTEXT indexes to Label and Information columns in the table Training create above using the query below:
CREATE FULLTEXT INDEX Information ON Training(Infromation);
CREATE FULLTEXT INDEX Label ON Training(Label);
Now, let us also insert some sample record data into the table Training by the following query:
INSERT INTO Training (TID,Label, Information) VALUES(‘1’,’MySQL JOINS’,‘MySQL JOINS are the clauses that are applied on our database tables to combine two or more tables to provide the result set.’);
Let us display the contents and structure of the table Training as follows:
SELECT * FROM Training;
Again, let us view the indexes created on this table that makes a clustered index for InnoDB table Training as follows:
It shows that in a InnoDB table the Primary key column index itself represents the MySQL clustered index for storing the table data with associated key values in a sorted way.
We can even create table like this to have a clustered index where we have defined two integer columns as primary keys in InnoDB table:
CREATE TABLE Students( Stud_Id INT NOT NULL AUTO_INCREMENT, User_Id INT NOT NULL,
PRIMARY KEY (User_Id, Stud_ID) //clustered index ) ENGINE = InnoDB ;
MySQL Clustered Index defines the direction of each table data in a database according to the key values which can be physically stored in a unique way. If there exists a Unique key or Primary key in a relational database table column then, MySQL will permit to create a clustered index on the basis of that definite column, named as PRIMARY. It helps in key lookups, index scanning and other index related data operations in the table.
This is a guide to MySQL Clustered Index. Here we discuss the introduction, how does clustered index work in MySQL? and examples respectively. You may also have a look at the following articles to learn more –