当前位置:网站首页>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、索引原则
- 索引不是越多越好
- 不要对经常变动数据加索引
- 小数据量的表不需要加索引
- 索引一般加在常用来查询的字段上
边栏推荐
- Get log4net log file in C - get log4net log file in C
- Golang type assertion and conversion (and strconv package)
- Rhcsa third day notes
- Summary of common operation and maintenance commands
- How can the outside world get values when using nodejs to link MySQL
- 2022 high voltage electrician examination and high voltage electrician reexamination examination
- @Scenario of transactional annotation invalidation
- 2022 safety officer-c certificate examination and safety officer-c certificate registration examination
- Capture de paquets et tri du contenu externe - - autoresponder, composer, statistiques [3]
- How to handle wechat circle of friends marketing activities and share production and release skills
猜你喜欢
leetcode-540. A single element in an ordered array
Battle drag method 1: moderately optimistic, build self-confidence (1)
Capture de paquets et tri du contenu externe - - autoresponder, composer, statistiques [3]
Nmap and masscan have their own advantages and disadvantages. The basic commands are often mixed to increase output
It is discussed that the success of Vit lies not in attention. Shiftvit uses the precision of swing transformer to outperform the speed of RESNET
Qt6 QML Book/Qt Quick 3D/基础知识
如临现场的视觉感染力,NBA决赛直播还能这样看?
Hcie security Day12: supplement the concept of packet filtering and security policy
Basic preprocessing and data enhancement of image data
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
随机推荐
Offset related concepts + drag modal box case
How to handle wechat circle of friends marketing activities and share production and release skills
Visiontransformer (I) -- embedded patched and word embedded
Rhcsa third day operation
11-grom-v2-04-advanced query
Brief analysis of ref nerf
Capture de paquets et tri du contenu externe - - autoresponder, composer, statistiques [3]
Advanced collaboration: coroutinecontext
TLS environment construction and plaintext analysis
Etcd raft Based Consistency assurance
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
Kubernetes 通信异常网络故障 解决思路
不同业务场景该如何选择缓存的读写策略?
jvm jni 及 pvm pybind11 大批量数据传输及优化
Software testing skills, JMeter stress testing tutorial, obtaining post request data in x-www-form-urlencoded format (24)
Strange way of expressing integers (expanding Chinese remainder theorem)
Based on laravel 5.5\5.6\5 X solution to the failure of installing laravel ide helper
设计电商秒杀系统
The "boss management manual" that is wildly spread all over the network (turn)
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles