当前位置:网站首页>MySQL——索引
MySQL——索引
2022-07-03 21:02:00 【Java学不会】
文章目录
1、简介
MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构。
索引在小数据量的时候,用处不大,但是在大数据的时候,区别十分明显。
2、索引的分类
2.1、主键索引(PRIMARy KEY)
- 唯一的标识,主键不可重复,一个表只能有一个主键。
2.2、唯一索引(UNIQUE KEY)
- 避免重复的数据出现,唯一索引可以重复,一个表中可以有多个唯一索引
2.3、常规索引(KEY/INDEX)
- 默认的,index,key关键字来设置
2.4、全文索引(FullText)
- 在特定的数据库引擎下才有,MySAM
- 快速定位数据
3、测试索引
3.1、创建100万条数据
CREATE TABLE `app_user` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) DEFAULT'' COMMENT'用户昵称',
`email` VARCHAR(50) NOT NULL COMMENT'用户邮箱',
`phone` VARCHAR(20) DEFAULT'' COMMENT'手机号',
`gender` TINYINT(4) UNSIGNED DEFAULT '0'COMMENT '性别(0:男;1:女)',
`password` VARCHAR(100) NOT NULL COMMENT '密码',
`age` TINYINT(4) DEFAULT'0' COMMENT '年龄',
`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT = 'app用户表'
-- 插入100玩数据
DELIMITER $$ -- 写函数之前必须要写,标志
CREATE FUNCTION mock_data()
RETURNS INT
BEGIN
DECLARE num INT DEFAULT 1000000;
DECLARE i INT DEFAULT 0;
WHILE i<num DO
INSERT INTO app_user(`name`,`email`,`phone`,`gender`,`password`,`age`)
VALUES(CONCAT('用户',i),'[email protected]',CONCAT('15',FLOOR(RAND()*(999999999-100000000)+100000000)),
FLOOR(RAND()*2),UUID(),FLOOR(RAND()*100));
SET i=i+1;
END WHILE;
RETURN i;
END;
SELECT mock_data()
- 查询第999999条数据:SELECT * FROM app_user WHERE
name
=‘用户999999’;
3.2、添加索引
-- id_表名_字段名
CREATE INDEX id_app_user_name ON app_user(`name`)
- 查询第999999条数据:SELECT * FROM app_user WHERE
name
=‘用户999999’;
4、索引原则
- 索引不是越多越好
- 不要对经常变动数据加索引
- 小数据量的表不需要加索引
- 索引一般加在常用来查询的字段上
边栏推荐
- How to set the system volume programmatically- How to programmatically set the system volume?
- Mysql database ----- common commands of database (based on database)
- Borui data and Sina Finance released the 2021 credit card industry development report
- Software testing skills, JMeter stress testing tutorial, obtaining post request data in x-www-form-urlencoded format (24)
- leetcode-540. A single element in an ordered array
- How to do Taobao full screen rotation code? Taobao rotation tmall full screen rotation code
- Baohong industry | good habits that Internet finance needs to develop
- From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
- AST (Abstract Syntax Tree)
- jvm jni 及 pvm pybind11 大批量数据传输及优化
猜你喜欢
SQL injection - Fundamentals of SQL database operation
Summary of common operation and maintenance commands
Link aggregation based on team mechanism
内存分析器 (MAT)
一台服务器最大并发 tcp 连接数多少?65535?
How to handle wechat circle of friends marketing activities and share production and release skills
How to choose cache read / write strategies in different business scenarios?
强化学习-学习笔记1 | 基础概念
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
Qt6 QML Book/Qt Quick 3D/基础知识
随机推荐
In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
Etcd 基于Raft的一致性保证
Do you really know how old you are?
How to set the system volume programmatically- How to programmatically set the system volume?
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
上周内容回顾
Link aggregation based on team mechanism
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...
Study diary: February 14th, 2022
Kubernetes 通信异常网络故障 解决思路
Go learning notes (4) basic types and statements (3)
Hcie security Day10: six experiments to understand VRRP and reliability
jvm jni 及 pvm pybind11 大批量数据传输及优化
Custom view incomplete to be continued
Summary of common operation and maintenance commands
MySQL dump - exclude some table data - MySQL dump - exclude some table data
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
MDM mass data synchronization test verification
Is flush account opening and registration safe and reliable? Is there any risk?
thrift go