当前位置:网站首页>【高并发基础】MySQL索引优化
【高并发基础】MySQL索引优化
2022-06-28 15:33:00 【Ch.yang】
前言
本文整理自MySQL官方网站,官网的连接比较琐碎。防止反复多开页面,这边记录一些重点。
How MySQL Uses Indexes
Most MySQL indexes (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT) are stored in B-trees. Exceptions: Indexes on spatial data types use R-trees; MEMORY tables also support hash indexes; InnoDB uses inverted lists for FULLTEXT indexes.
PRIMARY KEY, UNIQUE, INDEX 被存储在B树中(官网是说的B树,实质是以B+树作为技术实现)
- 索引分聚集索引和二级索引 原文地址
The Physical Structure of an InnoDB Index
- 聚集索引
InnoDB的聚簇索引其实就是在同一个结构中保存了B-Tree索引(技术上来说是B+Tree)和数据行。
更详细的数据结构图解
MySQL uses indexes for these operations
- 命中where 语句中的条件
- 筛选掉不合适的行(列上的值超过了存储长度,无法等值命中,只能筛掉不等的值)
If a search term exceeds the index prefix length, the index is used to exclude non-matching rows, and the remaining rows are examined for possible matches. (原文地址)
For additional information about index prefixes,
if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3).
CREATE TABLE test (
id INT NOT NULL,
last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
INDEX name (last_name,first_name)
);
(1)可以使用name索引:
SELECT * FROM test WHERE last_name='Jones';
SELECT * FROM test
WHERE last_name='Jones' AND first_name='John';
SELECT * FROM test
WHERE last_name='Jones'
AND (first_name='John' OR first_name='Jon');
SELECT * FROM test
WHERE last_name='Jones'
AND first_name >='M' AND first_name < 'N';
(2)无法使用name索引
SELECT * FROM test WHERE first_name='John';
SELECT * FROM test
WHERE last_name='Jones' OR first_name='John';
联合索引的隐含兼容能力:(col1, col2, col3) 相当于建立了 (col1), (col1, col2), and (col1, col2, col3) 索引
if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3).
SELECT * FROM tbl_name WHERE col1=val1; -- 可以用索引
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2; -- 可以用索引
SELECT * FROM tbl_name WHERE col2=val2; -- 不可以用索引
SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3; -- 不可以用索引
- 表连接中,同样类型、长度、字符集的索引作为连接条件可以提效
To retrieve rows from other tables when performing joins. MySQL can use indexes on columns more efficiently if they are declared as the same type and size.
- 用索引的不同部分提升效率
To find the MIN() or MAX() value for a specific indexed column key_col. This is optimized by a preprocessor that checks whether you are using WHERE key_part_N = constant on all key parts that occur before key_col in the index. In this case, MySQL does a single key lookup for each MIN() or MAX() expression and replaces it with a constant. If all expressions are replaced with constants, the query returns at once. For example:
SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE key_part1=10;
转化一下上文的意思:max(key_part2) where col = key_part1; 如果存在联合索引index(key_part1,key_part2) 那么这个查询很快。查询优化依据的是索引B+树的有序性取最大最小值非常快,max声明的列处于联合索引的第二个位置,那么第一个位置也需要用等值命中。以此类推,index(key_part1,key_part2,key_part3) max(key_part3) 需要让前两个索引列做where的等值命中。值得一提的是index(key_part1,key_part2,key_part3) 在 SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE key_part1=10; 依旧能走索引。因为max(key_part2) 只要求key_part1 等值命中
- order by 的时候使用组合索引,注意最左匹配和倒序情况。DESC读取索引的顺序跟表达式相反。
To sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable index (for example, ORDER BY key_part1, key_part2). If all key parts are followed by DESC, the key is read in reverse order.
- 索引覆盖
In some cases, a query can be optimized to retrieve values without consulting the data rows
直接查索引的B树就能获得数据,不用在通过索引
SELECT key_part3 FROM tbl_name
WHERE key_part1=1
- 避免全表扫描(EXPLAIN 出来Type字段取值为ALL )
The output from EXPLAIN shows ALL in the type column when MySQL uses a full table scan to resolve a query.
会产生全表扫描的情况:
- 行数非常少的表
- ON 或者 WHERE 选择的列没有建立索引
- 查询优化器对 where 子句进行优化,发现全表扫描比索引更快。
- 在索引上使用的查询关键字,在索引分布中区分度不大
- 对于数据量大的表,避免查询优化器错误得选择索引
SELECT * FROM t1, t2 FORCE INDEX (index_for_column)
WHERE t1.col_name=t2.col_name;
边栏推荐
- Facebook! Adaptive gradient defeats manual parameter adjustment
- The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)
- go-zero 微服务实战系列(七、请求量这么高该如何优化)
- Grand launch of qodana: your favorite CI code quality platform
- 使用Karmada实现Helm应用的跨集群部署
- DBMS in Oracle_ output. put_ Line output problem solving process
- OpenHarmony—内核对象事件之源码详解
- 一种跳板机的实现思路
- 最长连续序列
- SAP mts/ato/mto/eto topic 9: front and back desk operation in m+m mode, strategy 50, preparation of raw materials and semi-finished products in advance
猜你喜欢

Fleet |「後臺探秘」第 3 期:狀態管理

C语言学习-20-归并排序

【LeetCode】13、罗马数字转整数

Application of mongodb in Tencent retail premium code

Flutter简单实现多语言国际化

Practice of curve replacing CEPH in Netease cloud music

Xinchuang operating system -- kylin kylin desktop operating system (project 10 security center)

智慧园区数智化供应链管理平台如何优化流程管理,驱动园区发展提速增质?

What useful supplier management systems are available

经典模型——Transformer
随机推荐
MySQL主从切换的超详细步骤
笔试面试算法经典–最长回文子串
Web worker poll request
Oracle11g database uses expdp to back up data every week and upload it to the backup server
Openharmony - detailed source code of Kernel Object Events
With a return of 5000 times, the South African newspaper invested in Tencent to make a province
一文教你快速生成MySQL数据库关系图
【LeetCode】13、罗马数字转整数
雷科防务:4D毫米波雷达产品预计可以在年底量产供货
C语言学习-19-全排列
SaaS application management platform solution in the education industry: help enterprises realize the integration of operation and management
Facebook出手!自适应梯度打败人工调参
Fleet | background Discovery issue 3: Status Management
NFT pledge LP liquidity mining system development details
Qt 界面库
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
Facebook! Adaptive gradient defeats manual parameter adjustment
GBASE南大通用亮相第六届世界智能大会
The k-th element in the array [heap row + actual time complexity of heap building]
Yiwen teaches you to quickly generate MySQL database diagram