Read the index of mysql in one article

Preface

A MySQL index is a data structure used to increase the speed and performance of database queries.

The establishment of MySQL index is very important for the efficient operation of MySQL. The index can greatly improve the retrieval speed of MySQL.

MySQL indexes are similar to book indexes, allowing you to quickly locate and access specific data in a table by storing pointers to rows of data.

For example, if MySQL that is properly designed and uses indexes is a Lamborghini, then MySQL that is not designed and uses indexes is a human tricycle.

Take the table of contents page (index) of a Chinese dictionary as an example. We can quickly find the words we need through the table of contents (index) sorted by pinyin, strokes, radicals, etc.

Indexes are divided into single column indexes and combined indexes:

  • Single-column index, that is, an index only contains a single column, and a table can have multiple single-column indexes.
  • Combined index, that is, an index contains multiple columns.

When creating an index, you need to ensure that the index is applied to the conditions of the SQL query statement (usually as a condition of the WHERE clause).

In fact, the index is also a table that saves the primary key and index fields and points to the records of the entity table.

Although indexes can improve query performance, you also need to pay attention to the following points:

  • Indexes require additional storage space.
  • When inserting, updating, and deleting operations are performed on the table, the index needs to be maintained, which may affect performance.
  • Excessive or unreasonable indexes may cause performance degradation, so indexes need to be selected and planned carefully.

1. Ordinary index

Indexes can significantly speed up queries, especially when searching in large tables. By using indexes, MySQL can directly locate the data rows that meet the query conditions without scanning the entire table row by row.

1. Create an index

Use CREATE INDEX statement to create a normal index.

Normal indexes are the most common type of index and are used to speed up queries on data in a table.

Syntax of CREATE INDEX :

CREATE INDEX index_name
ON table_name (column1 [ASC|DESC], column2 [ASC|DESC], ...);
  • CREATE INDEX: Keyword used to create a normal index.
  • index_name: Specifies the name of the index to be created. Index names must be unique within the table.
  • table_name: Specify the table on which to create the index.
  • (column1, column2, ...): Specify the table column name to be indexed. You can specify one or more columns as a combination of indexes. The data type of these columns is usually numeric, text, or date.
  • ASC and DESC (optional): used to specify the sort order of the index. By default, the index is sorted in ascending order (ASC).

The following example assumes that we have a table named students with columns id, name, and age, and we will create a normal index on the name column.

CREATE INDEX idx_name ON students (name);

The above statement will create a normal index named idx_name on the name column of the students table, which will help improve query performance for searches by name.

It should be noted that if the amount of data in the table is large, index creation may take some time, but once the creation is completed, query performance will be significantly improved.

2. Modify the table structure (add index)

We can use the ALTER TABLE command to create an index in an existing table.

ALTER TABLE allows you to modify the structure of a table, including adding, modifying, or removing indexes.

ALTER TABLE syntax for creating an index:

ALTER TABLE table_name
ADD INDEX index_name (column1 [ASC|DESC], column2 [ASC|DESC], ...);
  • ALTER TABLE: Keyword used to modify the table structure.
  • table_name: Specifies the name of the table to be modified.
  • ADD INDEX: Clause to add index. ADD INDEX is used to create ordinary indexes.
  • index_name: Specifies the name of the index to be created. Index names must be unique within the table.
  • (column1, column2, ...): Specify the table column name to be indexed. You can specify one or more columns as a combination of indexes. The data type of these columns is usually numeric, text, or date.
  • ASC and DESC (optional): used to specify the sort order of the index. By default, the index is sorted in ascending order (ASC).

Here is an example where we will create a normal index on an existing table named employees:

ALTER TABLE employees
ADD INDEX idx_age (age);

The above statement will create a normal index named idx_age on the age column of the employees table.

3. Specify it directly when creating the table

When we create a table, you can directly specify the index in the CREATE TABLE statement to create a combination of table and index.

CREATE TABLE table_name (
  column1 data_type,
  column2 data_type,
  ...,
  INDEX index_name (column1 [ASC|DESC], column2 [ASC|DESC], ...)
);
  • CREATE TABLE: Keyword used to create new tables.
  • table_name: Specify the name of the table to be created.
  • (column1, column2, ...): Define the column names and data types of the table. You can specify one or more columns as a combination of indexes. The data type of these columns is usually numeric, text, or date.
  • INDEX: Keyword used to create a normal index.
  • index_name: Specifies the name of the index to be created. Index names must be unique within the table.
  • (column1, column2, ...): Specify the table column name to be indexed. You can specify one or more columns as a combination of indexes. The data type of these columns is usually numeric, text, or date.
  • ASC and DESC (optional): used to specify the sort order of the index. By default, the index is sorted in ascending order (ASC).

Below is an example where we want to create a table called students and create a normal index on the age column.

CREATE TABLE students (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  age INT,
  INDEX idx_age (age)
);

In the above example, we created a normal index called idx_age on the age column of the students table.

4. Syntax for deleting indexes

We can use the DROP INDEX statement to delete the index.

Syntax of DROP INDEX:

DROP INDEX index_name ON table_name;
  • DROP INDEX: Keyword used to delete index.
  • index_name: Specifies the name of the index to delete.
  • ON table_name: Specify the table on which the index is to be dropped.

The syntax for deleting an index using the ALTER TABLE statement is as follows:

ALTER TABLE table_name
DROP INDEX index_name;
  • ALTER TABLE: Keyword used to modify the table structure.
  • table_name: Specifies the name of the table to be modified.
  • DROP INDEX: Clause for deleting an index.
  • index_name: Specifies the name of the index to delete.

The following example assumes that we have a table named employees and an index named idx_age on the age column, and now we want to drop this index:

DROP INDEX idx_age ON employees;

or use ALTER TABLE Word:

ALTER TABLE employees
DROP INDEX idx_age;

Both commands delete the index named idx_age from the employees table.

If the index does not exist, an error will occur when executing the command. Therefore, it is best to confirm whether the index exists before deleting it, or use an error handling mechanism to handle possible error conditions.


2. Unique index

In MySQL, you can use the CREATE UNIQUE INDEX statement to create a unique index.

A unique index ensures that the values ​​in the index are unique and no duplicate values ​​are allowed.

Create index

CREATE UNIQUE INDEX index_name
ON table_name (column1 [ASC|DESC], column2 [ASC|DESC], ...);
  • CREATE UNIQUE INDEX: Keyword combination used to create a unique index.
  • index_name: Specifies the name of the unique index to be created. Index names must be unique within the table.
  • table_name: Specify the table on which to create a unique index.
  • (column1, column2, ...): Specify the table column name to be indexed. You can specify one or more columns as a combination of indexes. The data type of these columns is usually numeric, text, or date.
  • ASC and DESC (optional): used to specify the sort order of the index. By default, the index is sorted in ascending order (ASC).

The following is an example of creating a unique index: Suppose we have a table named employees that contains id and email columns. Now we want to create a unique index on the email column to ensure that each employee's email address is unique. .

CREATE UNIQUE INDEX idx_email ON employees (email);

The above example will create a unique index named idx_email on the email column of the employees table.

Modify table structure

We can use the ALTER TABLE command to create a unique index.

The ALTER TABLE command allows you to modify the existing table structure, including adding new indexes.

ALTER table mytable ADD UNIQUE [indexName] (columnName(length))
  • ALTER TABLE: Keyword used to modify the table structure.
  • table_name: Specifies the name of the table to be modified.
  • ADD CONSTRAINT: This is the keyword used to add constraints including unique indexes.
  • index_name: Specifies the name of the unique index to be created. Constraint names must be unique within the table.
  • UNIQUE (column1, column2, ...): Specify the table column name to be indexed. You can specify one or more columns as a combination of indexes. The data type of these columns is usually numeric, text, or date.
  • ASC and DESC (optional): used to specify the sort order of the index. By default, the index is sorted in ascending order (ASC).

The following is an example of using the ALTER TABLE command to create a unique index: Suppose we have a table named employees, containing id and email column, now we want to create a unique index on the email column to ensure that each employee's email address is unique.

ALTER TABLE employees
ADD CONSTRAINT idx_email UNIQUE (email);

The above example will create a unique index named idx_email on the email column of the employees table.

Note that adding a unique index will fail if there are already duplicate email values ​​in the table. Before creating a unique index, you may want to make sure there are no duplicate values ​​for the email column in the table.

Specify it directly when creating the table

We can also use UNIQUE in the CREATE TABLE statement while creating the table  keyword to create a unique index.

This will define the unique index constraint at table creation time.

Syntax for creating a unique index in the CREATE TABLE statement:

CREATE TABLE table_name (
  column1 data_type,
  column2 data_type,
  ...,
  CONSTRAINT index_name UNIQUE (column1 [ASC|DESC], column2 [ASC|DESC], ...)
);
  • CREATE TABLE: Keyword used to create new tables.
  • table_name: Specify the name of the table to be created.
  • (column1, column2, ...): Define the column names and data types of the table. You can specify one or more columns as a combination of indexes. The data type of these columns is usually numeric, text, or date.
  • CONSTRAINT: Keyword used to add constraints.
  • index_name: Specifies the name of the unique index to be created. Constraint names must be unique within the table.
  • UNIQUE (column1, column2, ...): Specify the table column name to be indexed.

The following is an example of creating a unique index when creating a table: Suppose we want to create a table named employees, which contains the id, name and email columns. We want the value of the email column to be unique, so we need to create a unique index when creating the table. Define a unique index.

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  email VARCHAR(100) UNIQUE
);

In this example, the email column is defined as a unique index because the UNIQUE keyword is appended to it.

Please note that after using the UNIQUE keyword, the index name will be automatically generated. You can also specify the index name as needed.


3. Use the ALTER command to add and delete indexes

There are four ways to add an index to a data table:

  • ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): This statement adds a primary key, which means that the index value must be unique and cannot be NULL.

  • ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): The value of the index created by this statement must be unique (except NULL, NULL may appear multiple times).
  • ALTER TABLE tbl_name ADD INDEX index_name (column_list): Add a normal index, the index value can appear multiple times.
  • ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list):This statement specifies the index as FULLTEXT, which is used for full-text indexing.

The following example adds an index to the table.

mysql> ALTER TABLE testalter_tbl ADD INDEX (c);

You can also use the DROP clause in the ALTER command to drop an index. Try the following example to delete an index:

mysql> ALTER TABLE testalter_tbl DROP INDEX c;

4. Use the ALTER command to add and delete primary keys

The primary key acts on a column (one column or multiple columns can be combined with the primary key). When adding a primary key index, you need to ensure that the primary key is not NULL by default. Examples are as follows:

mysql> ALTER TABLE testalter_tbl MODIFY i INT NOT NULL;
mysql> ALTER TABLE testalter_tbl ADD PRIMARY KEY (i);

You can also delete a primary key using the ALTER command:

mysql> ALTER TABLE testalter_tbl DROP PRIMARY KEY;

When deleting a primary key, you only need to specify the PRIMARY KEY, but when deleting an index, you must know the index name.


5. Display index information

You can use the SHOW INDEX command to list related index information in a table.

The output information can be formatted by adding \G .

SHOW INDEX statement::

mysql> SHOW INDEX FROM table_name\G
........
  • SHOW INDEX: Keyword used to display index information.
  • FROM table_name: Specify the name of the table for which you want to view index information.
  • \G: Format output information.

After executing the above command, detailed information about all indexes in the specified table will be displayed, including index name (Key_name), index column (Column_name), whether it is a unique index (Non_unique), sorting method (Collation), and index cardinality (Cardinality). wait.

Guess you like

Origin blog.csdn.net/m0_61243965/article/details/133915992