当前位置:网站首页>MySQL performance optimization index function, hidden, prefix, hash index usage (2)
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
2022-06-27 21:15:00 【Ink Sky Wheel】

Then the last issue said ,MYSQL 8 Is indexed in a relatively MYSQL 5.7 There has been a lot of progress , One of the most prominent is the addition of functional indexes , When did this function start .
8.013 , Yes MYSQL 8.013 Functional indexes have been added in version , Here's an example
create table function_test (id bigint primary key auto_increment,
name varchar(20) not null,
birth_day datetime);

mysql> insert into function_test (name,birrth_day) values ('Tim','2018-09-09');
Query OK, 1 row affected (0.01 sec)
mysql> insert into function_test (name,birrth_day) values ('Sam','2020-01-09');
Query OK, 1 row affected (0.01 sec)
mysql> insert into function_test (name,birrth_day) values ('Pam','2021-01-09');
Query OK, 1 row affected (0.00 sec)
Alter table function_test add index month(birrth_day);

We actually get an index with functions
CREATE TABLE `function_test` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`birrth_day` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `month` (`birrth_day`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |

Based on this function ,MYSQL The version of should be in 8.013 Above version .
The prefix index is MYSQL Unique features , But there are scenarios when using , That is to say, the amount type to which the prefix index is first added is the character type , The data can be searched in detail through the value of the field in the front part of the field , Its main purpose is to reduce the index bytes at the same time , It also improves efficiency .
Let's give an example
create table prefix_test (id bigint primary key auto_increment,name varchar(20) not null, type_comments varchar(2000));


mysql> insert into prefix_test (name,type_comments) values ('Tim','1_ I don't like ');
Query OK, 1 row affected (0.01 sec)
mysql> insert into prefix_test (name,type_comments) values ('Sam','3_ I am very fond of ');
Query OK, 1 row affected (0.01 sec)
mysql> insert into prefix_test (name,type_comments) values ('Semon','2_ have no feelings ');
Query OK, 1 row affected (0.00 sec)
mysql> alter table prefix_test add index (type_comments(1));
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
explain select name from prefix_test where type_comments like '1%';
It can also be seen from the above example , In fact, we just need to give 2000 Character , One of them 1 Characters to index , You can complete some quick searches .

Or it can be done in this way GROUP BY And so on .
After all this is MYSQL Hidden index for , This is not a new feature , There are similar index types in other databases , In fact, the hidden index is used to verify the validity of the index for some queries , And let him go online or offline .

mysql>
mysql> alter table prefix_test add index (type_comments(1)) invisible;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table prefix_test;
CREATE TABLE `prefix_test` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`type_comments` varchar(2000) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `type_comments` (`type_comments`(1)) *!80000 INVISIBLE */
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
mysql> explain select name from prefix_test where type_comments like '1%';
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
| 1 | SIMPLE | prefix_test | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
1 row in set, 1 warning (0.00 sec)
The figure above shows that although the new index is created, the invisible The state of is unusable . If you want to use it, you can immediately let it into visible The state of .
And finally hash Indexes , In the last article, I mentioned that I have established HASH The method solves the problem that multiple fields in the equivalent index establish a joint index , Let's take an example to see how to operate .
mysql> alter table hash_test add hash_col varchar(20) generated always as (crc32(concat(type1,type2)));
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from hash_test;
+----+------+-------+-------+------------+
| id | name | type1 | type2 | hash_col |
+----+------+-------+-------+------------+
| 1 | Tim | 10 | 20 | 2249310613 |
+----+------+-------+-------+------------+
1 row in set (0.00 sec)
mysql> insert into hash_test (name,type1,type2) values ('Sam','20','30');
Query OK, 1 row affected (0.00 sec)
mysql> select * from hash_test;
+----+------+-------+-------+------------+
| id | name | type1 | type2 | hash_col |
+----+------+-------+-------+------------+
| 1 | Tim | 10 | 20 | 2249310613 |
| 2 | Sam | 20 | 30 | 2378129210 |
+----+------+-------+-------+------------+
2 rows in set (0.00 sec)

Now we can give hash Index the virtual fields of

So the next time we check , You can use it directly hash_col As the index field for the lookup , Instead of caring about ytype1 type2 If there is 50 Fields to be indexed , Then you can ignore , Just one field can express N Values for fields , In this way, the query speed is fast , At the same time, your index has only one at any time , The problem of space efficiency has been solved .

边栏推荐
- Shell command used in actual work - sed
- eval函数,全局、本地变量
- [STL programming] [common competition] [Part 3]
- Model reasoning acceleration based on tensorrt
- # Leetcode 821. Minimum distance of characters (simple)
- Yyds dry goods counting SQL sub query
- It took me 6 months to complete the excellent graduation project of undergraduate course. What have I done?
- 华为伙伴暨开发者大会2022开源时刻全纪录
- Installing services for NFS
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
猜你喜欢

划重点!国产电脑上安装字体小技巧

Csdn Skills Tree use Experience and Product Analysis (1)

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献

It took me 6 months to complete the excellent graduation project of undergraduate course. What have I done?

难怪大家丢掉了postman而选择 Apifox

Massive data attended the Lanzhou opengauss meetup (ECOLOGICAL NATIONAL trip) activity, enabling users to upgrade their applications with enterprise level databases

How dbeaver restores and backs up databases

谈谈我写作生涯的画图技巧

Implementation string mystring

众昂矿业:新能源或成萤石最大应用领域
随机推荐
Flutter隐藏AppBar的返回按钮
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
Runmaide medical opened the offering: without the participation of cornerstone investors, the amount of loss doubled
银河麒麟系统局域网文件共享教程
Cocoscreator plays audio and synchronizes progress
Openharmony hisysevent dotting and calling practice of # Summer Challenge (L2)
Safe and efficient, non-contact "hand brushing" identification helps epidemic prevention and control
优维HyperInsight:掘金164.94亿美元可观测市场的“金锄头”?
Select auto increment or sequence for primary key selection?
使用storcli工具配置RAID,收藏这一篇就够了
非常全面的DolphinScheduler(海豚调度)安装使用文档
MySQL客户端工具推荐,一定想不到最好用巨然是它
Batch insert data using MySQL bulkloader
展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
A distribution fission activity is more than just a circle of friends
基于微信小程序的高校党员之家服务管理系统系统小程序#毕业设计,党员,积极分子,学习,打卡,论坛
原创翻译 | 机器学习模型服务工具对比:KServe,Seldon Core和BentoML
Eval function, global, local variables
1029 Median
Logcli Loki command line tool