当前位置:网站首页>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,得不偿失.
边栏推荐
猜你喜欢

48页智慧城市规划蓝图 解决方案

The first part of the R language
![[MySQL exercises] Chapter 5 · SQL single table query](/img/11/66b4908ed8f253d599942f35bde96a.png)
[MySQL exercises] Chapter 5 · SQL single table query

SSM整合案例分析(详解)

SSM框架简单介绍

mysql安装教程【安装版】

奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!

2019 NeurIPS | Graph Convolutional Policy Network for Goal-Directed Molecular Graph Generation

MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql

SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
随机推荐
Ubuntu安装Mysql5.7
SQL 嵌套 N 层太长太难写怎么办?
MySQL 5.7升级到8.0详细过程
数组every和some方法的区别?
2022杭电杯超级联赛3
SSM框架讲解(史上最详细的文章)
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
35-Jenkins-共享库应用
MySQL中InnoDB的多版本并发控制(MVCC)的实现
ONES 入选 CFS 财经峰会「2022数字化创新引领奖」
免安装版的Mysql安装与配置——详细教程
35-Jenkins-Shared library application
科目三:左转弯
【MySQL功法】第2话 · 数据库与数据表的基本操作
NK-RTU980烧写裸机程序
二维坐标工具API
中软国际携手深开鸿发布(1+1) x N 战略,以数字化、智慧化改变人类生产和生活方式
7/28-7/29 期望+思维+后缀数组+ST表
UML图及在drawio中的绘制
The first part of the R language