MySQL study notes. Index operation

Introduction to Index

An index is a structure for sorting the values ​​of one or more columns in a database table. Using an index can quickly access specific information in a database table. If you want to find a specific employee by his or her last name, the index helps to obtain information faster than searching all rows in the table.
One of the main purposes of the index is to speed up the retrieval of the data in the table, that is, to assist the information searcher to find the auxiliary data structure of the record ID that meets the restricted conditions as soon as possible.
For example, such a query: select * from table1 where id=10000. If there is no index, you must traverse the entire table until the row with ID equal to 10000 is found; after having an index (must be an index established on the ID column), you can search in the index. Since the index is optimized by a certain algorithm, the number of searches is much less. It can be seen that the index is used for positioning.
-------------------------------------------------- -------------------------------------------------- -------------------------"Baidu Encyclopedia"

Advantages and disadvantages of index creation

Advantages
1. When designing a database, by creating a unique index, a one-to-one mapping relationship can be formed between the index and the information, and the uniqueness of the data can be increased.
2. It can improve the speed of data search and retrieval, in line with the original intention of database establishment.
3. It can speed up the connection speed between the table and the table, which plays an important role in improving the referential integrity of the data.
4. In the process of information retrieval, if grouping and sorting clauses are used, indexing can effectively reduce the grouping and sorting time required in the retrieval process and improve the retrieval efficiency.
5. After the index is established, the optimization hider can be used in the information query process, which is of great significance for improving the performance of the entire information retrieval system.
Disadvantages
1. It takes time to create and maintain indexes, and the time consumed will increase with the increase of data.
2. The index needs to take up disk space, and the index also takes up a certain amount of physical space. As the amount of index increases, the space occupied will further increase.
3. When the data in the table is added, deleted and modified, the index also needs to be maintained, which reduces the maintenance speed.

Create index

1. Create an ordinary index The
most basic index type, there is no restriction such as uniqueness, the purpose is only to speed up the speed of data access

mysql> CREATE TABLE STUDENT(
    -> ID INT PRIMARY KEY AUTO_INCREMENT,
    -> NAME CHAR(40) NOT NULL,
    -> AGE INT NOT NULL,
    -> GRADE DOUBLE,
    -> INDEX (ID)
    -> );
Query OK, 0 rows affected (0.10 sec)

mysql> SHOW CREATE TABLE STUDENT \G
*************************** 1. row ***************************
       Table: STUDENT
Create Table: CREATE TABLE `student` (
  `ID` int(11) NOT NULL auto_increment,
  `NAME` char(40) NOT NULL,
  `AGE` int(11) NOT NULL,
  `GRADE` double default NULL,
  PRIMARY KEY  (`ID`),
  KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

The index has been successfully created, use explain to check whether the index is in use

mysql>  EXPLAIN SELECT * FROM STUDENT WHERE ID=1 \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: STUDENT
         type: const
possible_keys: PRIMARY,ID
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra:
1 row in set (2.13 sec)

2. Create a unique index

mysql> CREATE TABLE STUDENT(
    -> ID INT PRIMARY KEY AUTO_INCREMENT,
    -> NAME CHAR(40) NOT NULL,
    -> AGE INT NOT NULL,
    -> GRADE DOUBLE,
    -> UNIQUE INDEX UNI_INDEX (ID)
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> SHOW CREATE TABLE STUDENT \G
*************************** 1. row ***************************
       Table: STUDENT
Create Table: CREATE TABLE `student` (
  `ID` int(11) NOT NULL auto_increment,
  `NAME` char(40) NOT NULL,
  `AGE` int(11) NOT NULL,
  `GRADE` double default NULL,
  PRIMARY KEY  (`ID`),
  UNIQUE KEY `UNI_INDEX` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

3. Create a single-column index A
single-column index is an index created in a certain field in the data table, and multiple single-column indexes can be created

mysql> drop table student;
Query OK, 0 rows affected (1.81 sec)

mysql> CREATE TABLE STUDENT(
    -> ID INT PRIMARY KEY AUTO_INCREMENT,
    -> NAME CHAR(40) NOT NULL,
    -> AGE INT NOT NULL,
    -> GRADE DOUBLE,
    -> INDEX SINGLE_INDEX(NAME(30))
    -> );
Query OK, 0 rows affected (0.10 sec)

mysql> SHOW CREATE TABLE STUDENT \G
*************************** 1. row ***************************
       Table: STUDENT
Create Table: CREATE TABLE `student` (
  `ID` int(11) NOT NULL auto_increment,
  `NAME` char(40) NOT NULL,
  `AGE` int(11) NOT NULL,
  `GRADE` double default NULL,
  PRIMARY KEY  (`ID`),
  KEY `SINGLE_INDEX` (`NAME`(30))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

4. Create a composite index

mysql> CREATE TABLE STUDENT(
    -> ID INT PRIMARY KEY AUTO_INCREMENT,
    -> NAME CHAR(40) NOT NULL,
    -> AGE INT NOT NULL,
    -> GRADE DOUBLE,
    -> INDEX MULT_INDEX(ID,NAME,GRADE)
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> SHOW CREATE TABLE STUDENT \G
*************************** 1. row ***************************
       Table: STUDENT
Create Table: CREATE TABLE `student` (
  `ID` int(11) NOT NULL auto_increment,
  `NAME` char(40) NOT NULL,
  `AGE` int(11) NOT NULL,
  `GRADE` double default NULL,
  PRIMARY KEY  (`ID`),
  KEY `MULT_INDEX` (`ID`,`NAME`,`GRADE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

5. Create a full-text index Create indexes
only for char, varchar and text columns

mysql> CREATE TABLE STUDENT(
    -> ID INT PRIMARY KEY AUTO_INCREMENT,
    -> NAME CHAR(40) NOT NULL,
    -> AGE INT NOT NULL,
    -> GRADE DOUBLE,
    -> FULLTEXT INDEX FULL(NAME)
    -> )ENGINE=MyISAM;
Query OK, 0 rows affected (2.14 sec)

mysql> SHOW CREATE TABLE STUDENT \G
*************************** 1. row ***************************
       Table: STUDENT
Create Table: CREATE TABLE `student` (
  `ID` int(11) NOT NULL auto_increment,
  `NAME` char(40) NOT NULL,
  `AGE` int(11) NOT NULL,
  `GRADE` double default NULL,
  PRIMARY KEY  (`ID`),
  FULLTEXT KEY `FULL` (`NAME`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (2.08 sec)

6. Create an index on an existing table.
Use alter table to create

ALTER TABLE STUDENT ADD [UNIQUE|FULLTEXT|SPATIAL] [INDEX|KEY] [INDEX_NAME](COL_NAME[LENGTH],...) [ASC|DESC]

Create with create index

CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX INDEX_NAME ON TABLE_NAME (COL_NAME[LENGTH,...]) [ASC|DESC]

Delete index

1. Use alter table to delete

alter table table_name drop index index_name;

Note: The unique index of the added AUTO_INCREMENT constraint field cannot be deleted
2. Use the drop index statement to delete

DROP INDEX INDEX_NAME ON TABLE_NAME;

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/109174183