当前位置:网站首页>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,得不偿失.
边栏推荐
猜你喜欢
随机推荐
regex bypass
哆啦a梦教你页面的转发与重定向
Read Elephant Swap in one article, why does it bring such a high premium to ePLATO?
LED flashing on CY7C68013A
SSM框架简单介绍
CY7C68013A之LED闪烁
Regarding "computing power", this article is worth reading
【黄啊码】MySQL入门—3、我用select ,老板直接赶我坐火车回家去,买的还是站票
Unreal基础概念
如何使用mysql binlog 恢复数据
【idea 报错】 无效的目标发行版:17 的解决参考
7/28-7/29 Expectation + thinking + suffix array + ST table
torch分布式训练
《C语言小游戏》扫雷
The Spark run on Yarn Spark application
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)-- 组件UI
[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
PowerCLi 一键批量部署OVA 到esxi 7
PHP中 比较 0、false、null,‘‘ “
【小程序项目开发-- 京东商城】uni-app之商品列表页面 (上)