当前位置:网站首页>MySql之索引
MySql之索引
2022-08-05 05:13:00 【来一杯奶喵】
文章目录
前言
MySql中的索引可以很大程度的提高MySql的检索效率,提高MySql的运行效率,可以确保所查数据的唯一性。因此学习索引也是十分重要的。下面我带大家稍微了解一下MySql中的索引。
一、索引是什么?
索引是对数据库表中一列或多列的值进行排序的一种结构。使用索引可以快速访问数据库表中的特定信息。
二、常见的索引种类
1.普通索引
这是最基本的索引,它没有任何限制。
代码如下:
create table 表名(
字段1 int(11) not null,
index/key(字段1)
)
--或者
create index 索引名 on 表名 (字段名)
2.唯一索引
不允许有索引值相同的行,从而禁止重复的索引或键值。
代码如下:
create table 表名(
字段名 int(11) not null unique,
#或 unique key(字段名)
)
--或
create unique index 索引名 on 表名 (字段名)
3.主键索引
在数据库关系中为表定义一个主键将自动创建主键索引。一个表只能有一个主键,不允许有空值。
代码如下:
create table 表名(
字段名 int(11) auto_increment primary key,
#或 primary key(字段名)
)
4.组合索引
在多个字段上创建的索引,只有在查询条件中使用了创建索引时的第一个字段,索引才会被使用。使用组合索引时遵循最左前缀匹配原则。
最左前缀匹配原则(比如创建了多列索引(stu_name,stu_age,stu_sex)会优先匹配stu_name字段,再到age,sex中间不能跳过,遇到范围查询就会停止匹配)。
代码如下:
create table 表名(
字段名1 int(11) not null,
字段名2 varchar(10) not null,
index(字段名1,字段名2)
)
5. 全文索引
搜索引擎的关键技术,用于检索文本信息,可以是词语或者段落。
注意:
- 只有char/varchar/text类型的字段能够创建全文索引。
- 当写入大量数据时,先写入数据再创建全文索引,提高效率。
- MySql内置ngram解析器,可解析中日韩文字,有汉字时要启用。
代码如下:
create table 表名(
字段名 varchar(32) not null,
fulltext key(字段名,字段名,字段名)with parser ngram
)ENGINE=innodb
三、关于索引失效的情况
- 使用%XXX左模糊(like)查询
- 组合索引,不满足最左原则时
- 使用索引比全表扫描还慢时,不要使用索引
- 用or分割条件,or前后只要有一个列没有索引,就不会使用索引
- 使用!=或<>操作符时
- 对文字进行null值判断时
- 索引列带有运算时
总结
- 索引是对数据库表中一列或多列的值进行排序的一种结构。
- 索引分为:普通索引、主键索引、唯一索引、组合索引、全文索引。
- 索引失效情况:%开头模糊查询、不满足最左原则、or、!=或<>、null值判断、运算、索引慢
希望以上的内容对看到这篇文章的你有所帮助。
边栏推荐
- 机器学习(二) —— 机器学习基础
- flink实例开发-batch批处理实例
- day8字典作业
- vscode要安装的插件
- Do you use tomatoes to supervise your peers?Add my study room, come on together
- Flink 状态与容错 ( state 和 Fault Tolerance)
- What field type of MySQL database table has the largest storage length?
- [Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
- day12函数进阶作业
- 对数据排序
猜你喜欢
Lecture 4 Backpropagation Essays
Error creating bean with name 'configDataContextRefresher' defined in class path resource
CAP+BASE
Lecture 5 Using pytorch to implement linear regression
[Let's pass 14] A day in the study room
Mesos学习
A blog clears the Redis technology stack
多线程查询结果,添加List集合
The software design experiment four bridge model experiment
第四讲 反向传播随笔
随机推荐
Difference between for..in and for..of
【After a while 6】Machine vision video 【After a while 2 was squeezed out】
"PHP8 Beginner's Guide" A brief introduction to PHP
flink中文文档-目录v1.4
The fourth back propagation back propagation
学习总结week2_5
day10-字符串作业
Flink accumulator Counter 累加器 和 计数器
02.01-----参数的引用的作用“ & ”
Returned object not currently part of this pool
2022杭电多校第一场01
【过一下 17】pytorch 改写 keras
学习总结week2_4
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
【MySQL】数据库多表链接的查询方式
小白一枚各位大牛轻虐虐
【过一下12】整整一星期没记录
ES6 生成器
coppercam入门手册[6]
vscode要安装的插件