当前位置:网站首页>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、索引原则
- 索引不是越多越好
- 不要对经常变动数据加索引
- 小数据量的表不需要加索引
- 索引一般加在常用来查询的字段上
边栏推荐
- MySQL 8.0 data backup and recovery
- 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%
- C 10 new feature [caller parameter expression] solves my confusion seven years ago
- Test access criteria
- Do you really know how old you are?
- How can the outside world get values when using nodejs to link MySQL
- Cannot load driver class: com. mysql. cj. jdbc. Driver
- Analyse de REF nerf
- 19、 MySQL -- SQL statements and queries
- The 12th Blue Bridge Cup
猜你喜欢

Visiontransformer (I) -- embedded patched and word embedded

Scientific research document management Zotero

From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events

"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media

TLS environment construction and plaintext analysis

强化學習-學習筆記1 | 基礎概念

Rhcsa third day notes

浅议.NET遗留应用改造
![Capture de paquets et tri du contenu externe - - autoresponder, composer, statistiques [3]](/img/bf/ac3ba04c48e80b2d4f9c13894a4984.png)
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
随机推荐
不同业务场景该如何选择缓存的读写策略?
Is flush account opening and registration safe and reliable? Is there any risk?
MySQL 8.0 data backup and recovery
Redis data migration (II)
Transformation between yaml, Jason and Dict
Task of gradle learning
Advanced collaboration: coroutinecontext
Baohong industry | good habits that Internet finance needs to develop
Leetcode daily question 540 A single element in an ordered array Valentine's Day special article looking for a single dog in a pile of lovers ~ the clown is myself
(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
Borui data and Sina Finance released the 2021 credit card industry development report
jvm jni 及 pvm pybind11 大批量数据传输及优化
Go learning notes (4) basic types and statements (3)
Preliminary practice of niuke.com (11)
JMeter plug-in installation
Qtablewidget control of QT
Pytorch sets the weight and bias of the model to zero
Basic knowledge of dictionaries and collections
Basic preprocessing and data enhancement of image data
Line segment tree blue book explanation + classic example acwing 1275 Maximum number