当前位置:网站首页>Hematemesis summarizes thirteen experiences to help you create more suitable MySQL indexes
Hematemesis summarizes thirteen experiences to help you create more suitable MySQL indexes
2022-07-31 08:35:00 【One Light Architecture】
The previous article talked about using itMySQL的Explain命令可以分析SQL性能瓶颈,优化SQL查询,and to see if the index is used.
We all know that creating indexes can improve query efficiency,But how to create an index specifically?
哪些字段适合创建索引?
Which fields are not suitable for indexing?
This article will learn with you how to create a suitable database index.
1. MySQL索引的分类
Learn about it before creating an indexMySQL有哪些索引,Then we can choose the appropriate index.
常见的索引有,普通索引、唯一索引、主键索引、联合索引、全文索引等.
普通索引
A normal index is the most basic index,没有任何限制.
Ordinary indexes can be created using the command:
ALTER TABLE `table_name` ADD INDEX index_name (`column`);
复制代码
唯一索引
Unlike normal indexes,唯一索引的列值必须唯一,允许为null.
This is how it is created:
ALTER TABLE `table_name` ADD UNIQUE index_name (`column`);
复制代码
主键索引
主键索引是一种特殊的唯一索引,And a table has only one primary key,不允许为null.
This is how it is created:
ALTER TABLE `table_name` ADD PRIMARY KEY (`column`);
复制代码
联合索引
A federated index creates an index on multiple fields at the same time,查询效率更高.
This is how it is created:
ALTER TABLE `table_name` ADD INDEX index_name (`column1`, `column2`, `column3`);
复制代码
全文索引
Full-text indexing is mainly used to match keywords in string text.
When it is required whether the string contains keywords,我们一般用like,如果是以%开头的时候,index cannot be used,这时候就可以使用全文索引了.
This is how it is created:
ALTER TABLE `table_name` ADD FULLTEXT (`column`);
复制代码
2. 哪些字段适合创建索引?
I have summarized the following points:
2.1 Fields that are frequently queried are suitable for indexing
There are always hot and cold fields in a table,Obviously those fields that are used frequently are more suitable for indexing it.
2.2 在where和onThe fields where the condition appears will be indexed first
为什么不是在selectFields that appear later are given priority to create an index?
因为查询SQL会先匹配on和where条件的字段,The specific matching order is as follows:
from > on > join > where > group by > having > select > distinct > order by > limit
2.3 Fields with a high degree of discrimination are suitable for creating indexes
For example, for a user table,Birthdays are more discriminating than gender,Better for creating indexes.
You can use the following methods to manually count,Discrimination for each field,值越大,区分度越高:
select
count(distinct birthday)/count(*),
count(distinct gender)/count(*)
from user;
复制代码
For indexes that have already been created,我们还可以使用MySQLcommand to view the discriminative ranking of each index:
图中CardinalityThe column represents the discriminative ranking of the index,Also known as the base.
2.4 Ordered fields are suitable for creating indexes
Ordered fields are inserted into the database,仍能保持B+树的索引结构,The index file does not need to be updated frequently,性能更好.
3. Which fields are not suitable for indexing?
Say which fields are suitable for indexing,There are fields that are not suitable for creating indexes.
3.1 Fields with low discrimination are not suitable for indexing.
I just said that the gender discrimination in the user table is low,Not as good as the birthday field for creating an index.
3.2 频繁更新的字段不适合创建索引
in the process of updating the field,需要维护B+树结构,The index file is updated frequently,降低SQL性能.
3.3 Fields that are too long are not suitable for indexing
Fields that are too long take up more space,不适合创建索引.
3.4 Unordered fields are not suitable for indexing
Unordered fields are in the process of inserting into the database,为了维护B+树索引结构,The index file needs to be updated frequently,性能较差.
4. Additional considerations for index creation
4.1 优先使用联合索引
查询的时候,The joint index can match the required data more accurately than the ordinary index.
It is in the picture(age,name)A joint index built on two fields,在B+Storage structure in the tree.
可以看出,是先age排序,age相等的数据,再按name排序.
对于这条查询SQL:
select age,name from user where age=18 and name='李四';
复制代码
The federated index can find the required data only once,如果我们只在age字段上建立索引,会先匹配到age=18的三条数据,Then traverse one by one,效率更差,Therefore, the joint index should be used first.
4.2 使用联合索引时,The field of discrimination is placed first
This reduces the number of queries,Match the required data faster.
4.3 Strings that are too long can be indexed using a prefix
For example, when matching user addresses,If the township can already distinguish most users,There is no need to be precise to the street area.
创建普通索引的时候,指定索引长度,就可以创建前缀索引了.
ALTER TABLE `user` ADD INDEX idx_address (address(3));
复制代码
4.4 Fields with unique values,使用唯一索引
使用唯一索引,可以避免程序bugresulting in duplicate data.
4.5 Sorting and grouping fields also try to create indexes
在order by和group byAlso try to create an index on the fields in the ,Avoid using file sorting,Index sorting can be used to provide performance.
4.6 避免创建过多索引
Index is easy to use,适度即可.创建过多的索引,Will take up more storage space,也会严重影响SQL性能,每次更新SQL,Both require updating a large number of index files,得不偿失.
边栏推荐
猜你喜欢
@Transactional注解的失效场景
The first part of the R language
【Unity】编辑器扩展-02-拓展Hierarchy视图
关于Error EPERM operation not permitted, mkdir...几种解决办法的比较
实用生物信息学2:多组学数据整合和挖掘
Flutter Paystack implements all options
科目三:右转弯
I advise those juniors and juniors who have just started working: If you want to enter a big factory, you must master these core skills!Complete Learning Route!
MySQL 5.7详细下载安装配置教程
[转载] Virtual Studio 让系统找到需要的头文件和库
随机推荐
Vscode:Project-tree插件
深度理解递归,手撕经典递归问题(汉诺塔,青蛙跳台阶),保姆级教学。
【MySQL功法】第5话 · SQL单表查询
如何在一台机器上(windows)安装两个MYSQL数据库
TypeError The view function did not return a valid response. The function either returned None 的解决
LED flashing on CY7C68013A
报销流程|By天放师兄
C语言三子棋(井字棋)小游戏
"The C language games" mine clearance
[Yellow ah code] Introduction to MySQL - 3. I use select, the boss directly drives me to take the train home, and I still buy a station ticket
数组every和some方法的区别?
哪些字符串会被FastJson解析为null呢
【云原生&微服务五】Ribbon负载均衡策略之随机ThreadLocalRandom
Flutter Paystack implements all options
Locust 1.0版本引入的变化
The first part of the R language
48页智慧城市规划蓝图 解决方案
会话技术之Coookie && Session详解
求职产品经理【九】求职季,如何写好一份简历?
[MySQL exercises] Chapter 2 Basic operations of databases and data tables