当前位置:网站首页>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、索引原則
- 索引不是越多越好
- 不要對經常變動數據加索引
- 小數據量的錶不需要加索引
- 索引一般加在常用來查詢的字段上
边栏推荐
- Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp
- Baohong industry | good habits that Internet finance needs to develop
- MySQL——索引
- "Designer universe" argument: Data Optimization in the design field ultimately falls on cost, safety and health | chinabrand.com org
- Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
- String and+
- Offset related concepts + drag modal box case
- jvm jni 及 pvm pybind11 大批量数据传输及优化
- In 2021, the global revenue of syphilis rapid detection kits was about US $608.1 million, and it is expected to reach US $712.9 million in 2028
- MySQL——数据库备份
猜你喜欢

Go learning notes (4) basic types and statements (3)

Example of peanut shell inner net penetration

Etcd 基于Raft的一致性保证

强化學習-學習筆記1 | 基礎概念
![[Tang Laoshi] C -- encapsulation: member variables and access modifiers](/img/be/0b38c0f1a27f819f7c79bcf634fbd4.jpg)
[Tang Laoshi] C -- encapsulation: member variables and access modifiers

全网都在疯传的《老板管理手册》(转)

2022 low voltage electrician examination and low voltage electrician simulation examination question bank

XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向

【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装

Interval product of zhinai sauce (prefix product + inverse element)
随机推荐
Thread, thread stack, method stack, the difference of creating thread
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
【c】 Digital bomb
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
不同业务场景该如何选择缓存的读写策略?
Qualcomm platform WiFi update disconnect end open event
Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp
jvm jni 及 pvm pybind11 大批量数据传输及优化
Producer consumer mode (multithreading, use of shared resources)
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
2022 safety officer-c certificate examination and safety officer-c certificate registration examination
Battle drag method 1: moderately optimistic, build self-confidence (1)
Do you really know how old you are?
设计电商秒杀系统
C 10 new feature [caller parameter expression] solves my confusion seven years ago
11-grom-v2-05-initialization
[secretly kill little buddy pytorch20 days -day02- example of image data modeling process]
QT6 QML book/qt quick 3d/ Basics
同花顺开户注册安全靠谱吗?有没有风险的?
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar