当前位置:网站首页>MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
2022-07-06 05:10:00 【koping_ wu】
Mysql *** 9: Create index 、 Delete index 、 descending index 、 Hide index
1、 Declaration and use of index
1.1 Classification of indexes
mysql The index of includes the general index 、 Uniqueness index 、 Full-text index 、 Single index 、 Multi column index 、 Spatial index .
- according to Functional logic : The index mainly includes 4 Kind of , They are ordinary indexes 、 unique index 、 primary key 、 Full-text index ;
- according to Physical implementation : The index can be divided into 2 Kind of , Clustered index and non clustered index ;
- according to Number of action fields : Divided into single column index and joint index ;
1.2 Create index
Mysql Support multiple methods to create indexes on single or multiple columns :
1) In the definition statement of creating the table CREATE TABLE Specify the index column in ;
2) On an existing table , Use ALTER TABLE Statement to create an index , Or use CREATE INDEX Statement to add an index to an existing table ;
1.2.1 Create indexes when creating tables
1.2.1.1 Create an index implicitly
There are primary key constraints in the declaration 、 Uniqueness constraint 、 On the field of the foreign key constraint , It will automatically add relevant indexes
CREATE TABLE dept(
dept_id INT PRIMARY KEY AUTO_INCREMENT,
dept_name VARCHAR(20)
);
CREATE TABLE emp(
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(20) UNIQUE,
dept_id INT,
CONSTRAINT emp_dept_id_fk FOREIGN KEY(dept_id) REFERENCES dept(dept_id)
);
You can view the index through the command :
The way 1:SHOW INDEX FROM Table name ;
mysql> SHOW INDEX FROM emp;
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| emp | 0 | PRIMARY | 1 | emp_id | A | 0 | NULL | NULL | | BTREE | | |
| emp | 0 | emp_name | 1 | emp_name | A | 0 | NULL | NULL | YES | BTREE | | |
| emp | 1 | emp_dept_id_fk | 1 | dept_id | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.01 sec)
You can see the primary key emp_id The index name of is PRIMARY,emp_name The index name of is emp_name,dept_id The index name of is emp_dept_id_fk.
The way 2:SHOW CREATE TABLE Table name ;
mysql> SHOW CREATE TABLE emp;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp | CREATE TABLE `emp` (
`emp_id` int(11) NOT NULL AUTO_INCREMENT,
`emp_name` varchar(20) DEFAULT NULL,
`dept_id` int(11) DEFAULT NULL,
PRIMARY KEY (`emp_id`),
UNIQUE KEY `emp_name` (`emp_name`),
KEY `emp_dept_id_fk` (`dept_id`),
CONSTRAINT `emp_dept_id_fk` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.2.1.2 Create indexes explicitly
1) Create a normal index
mysql> CREATE TABLE book(
-> book_id INT ,
-> book_name VARCHAR(100),
-> AUTHORS VARCHAR(100),
-> info VARCHAR(100) ,
-> COMMENT VARCHAR(100),
-> year_publication YEAR,
-> # Declaration index
-> INDEX idx_bname(book_name)
-> );
Query OK, 0 rows affected (0.21 sec)
Or through show index from book To see the index :
mysql> SHOW INDEX FROM book;
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book | 1 | idx_bname | 1 | book_name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
besides , You can also use performance analysis tools :EXPLAIN To check whether the index is used :
mysql> EXPLAIN SELECT * FROM book WHERE book_name = 'mysql';
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | book | NULL | ref | idx_bname | idx_bname | 103 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
Such as through book_name Search , You can see the index used (key) yes idx_bname, This is the general index just established above .
2) Create unique index
Declare a field with a unique index , When adding data , Make sure it's unique , But you can add null
such as , The following is a comment comment As the only index
CREATE TABLE book1(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Declaration index
UNIQUE INDEX uk_idx_cmt(COMMENT)
);
mysql> SHOW INDEX FROM book1;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book1 | 0 | uk_idx_cmt | 1 | COMMENT | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
3) primary key
Define the primary key index by defining the primary key constraint
CREATE TABLE book2(
book_id INT PRIMARY KEY ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR
);
mysql> SHOW INDEX FROM book2;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book2 | 0 | PRIMARY | 1 | book_id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
4) Create a single column index
CREATE TABLE book3(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Declaration index
UNIQUE INDEX idx_bname(book_name)
);
5) Create a federated index
CREATE TABLE book4(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Declaration index
INDEX mul_bid_bname_info(book_id,book_name,info)
);
mysql> SHOW INDEX FROM book4;
+-------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book4 | 1 | mul_bid_bname_info | 1 | book_id | A | 0 | NULL | NULL | YES | BTREE | | |
| book4 | 1 | mul_bid_bname_info | 2 | book_name | A | 0 | NULL | NULL | YES | BTREE | | |
| book4 | 1 | mul_bid_bname_info | 3 | info | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
Be careful , Using a federated index follows Leftmost prefix principle , That is, if you use an index, you must include at least the leftmost index of the joint index to retrieve .
Like the following 2 Search statements , In the 2 Of the statements , Because directly with book_name Search , First of all, the joint index is based on book_id For sorting and retrieval , It can't be found here , So we didn't successfully use the joint index we just built .
mysql> EXPLAIN SELECT * FROM book4 WHERE book_id = 1001 AND book_name = 'mysql';
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------------+------+----------+-------+
| 1 | SIMPLE | book4 | NULL | ref | mul_bid_bname_info | mul_bid_bname_info | 108 | const,const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
mysql> EXPLAIN SELECT * FROM book4 WHERE book_name = 'mysql';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | book4 | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
6) Create full text index
CREATE TABLE test4(
id INT NOT NULL,
NAME CHAR(30) NOT NULL,
age INT NOT NULL,
info VARCHAR(255),
FULLTEXT INDEX futxt_idx_info(info(50))
)
1.2.2 Create an index on an existing table
The way 1: The table has been created , have access to **ALTER TABLE … ADD …** To add an index .
CREATE TABLE book5(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR
);
After creating the table , Add an index to the table :
ALTER TABLE book5 ADD INDEX idx_cmt(COMMENT);
ALTER TABLE book5 ADD UNIQUE uk_idx_bname(book_name);
ALTER TABLE book5 ADD INDEX mul_bid_bname_info(book_id,book_name,info);
The way 2: The table has been created , You can also use **CREATE INDEX … ON …** To add an index .
CREATE TABLE book6(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR
);
CREATE INDEX idx_cmt ON book6(COMMENT);
CREATE UNIQUE INDEX uk_idx_bname ON book6(book_name);
CREATE INDEX mul_bid_bname_info ON book6(book_id,book_name,info);
1.3 Delete index
The way 1:ALTER TABLE … DROP INDEX …
ALTER TABLE book5 DROP INDEX idx_cmt;
The way 2:DROP INDEX … ON …
DROP INDEX uk_idx_bname ON book5;
Be careful : When deleting columns in a table , If the column to be deleted is part of the index , The column is also removed from the index . If all the columns that make up the index are deleted , Then the entire index will be deleted .
2、Mysql8.0 Index new features
2.1 Support descending index
Mysql stay 8.0 An ascending index was created before version , Reverse scanning when in use , This greatly reduces the efficiency of the database .
For example, create a table ts1:
CREATE TABLE ts1(a INT,b INT,INDEX idx_a_b(a ASC,b DESC));
If it's often used b Search in descending order , that 8.0 Version can successfully use the index , Greatly reduce the database retrieval time . and 8.0 Previous version , Because there is no feature of this descending index , So the retrieval time is longer .
EXPLAIN SELECT * FROM ts1 ORDER BY a,b DESC LIMIT 5;
2.2 Hide index
sometimes , We are not sure whether the index is valid , Or I want to verify whether deleting the index will cause any impact , You can not delete the index directly , You can hide the index first .
from Mysql8.0 Start supporting hidden indexes (invisible indexes), You only need to set the index to be deleted as a hidden index , Make the query optimizer no longer use this index . Wait until you confirm that the index is set to hidden , The system will not cause any impact , You can delete the index completely .
How to hide the index , It is also divided into creating tables , And after creating the table :
The way 1: Create table time , Hide index
CREATE TABLE book7(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Create an invisible index
INDEX idx_cmt(COMMENT) invisible
);
The way 2: After creating the table , Hide index
ALTER TABLE book7
ADD UNIQUE INDEX uk_idx_bname(book_name) invisible;
You can also modify the visibility of the index :
For example, index idx_cmt From invisible to visible :
ALTER TABLE book7 ALTER INDEX idx_cmt visible;
And then index idx_cmt From visible to invisible :
ALTER TABLE book7 ALTER INDEX idx_cmt invisible;
边栏推荐
猜你喜欢
用StopWatch 统计代码耗时
RT thread analysis - object container implementation and function
Codeforces Round #804 (Div. 2)
February 12 relativelayout
[leetcode] 18. Sum of four numbers
[数学建模] 微分方程--捕鱼业的持续发展
Golang -- TCP implements concurrency (server and client)
Postman manage test cases
【LeetCode】18、四数之和
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
Leetcode dynamic planning day 16
内核判断i2c地址上是否挂载外设
Biscuits (examination version)
nacos-高可用seata之TC搭建(02)
Excel转换为Lua的配置文件
EditorUtility. The role and application of setdirty in untiy
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
MySQL if and ifnull use
Flody的应用
Postman断言
Realize a binary read-write address book
Modbus协议通信异常
树莓派3.5寸屏幕白屏显示连接
Set detailed map + interview questions
Summary of redis basic knowledge points
你需要知道的 TCP 三次握手
Knowledge points of circular structure
yolov5 tensorrt加速
[noip2008 improvement group] stupid monkey
Hyperledger Fabric2. Some basic concepts of X (1)