当前位置:网站首页>[high concurrency foundation] MySQL index optimization
[high concurrency foundation] MySQL index optimization
2022-06-28 16:03:00 【Ch.yang】
Preface
This paper is compiled from MySQL Official website , The connection to the official website is trivial . Avoid repeatedly opening more pages , Here are some key points .
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 Stored in B In the tree ( The official website says B Trees , The essence is to B+ The tree is implemented as a technology )
- Indexes are divided into clustered indexes and secondary indexes Original address
The Physical Structure of an InnoDB Index
- Clustered index
InnoDB The clustered index of is actually saved in the same structure B-Tree Indexes ( Technically B+Tree) And data lines .
More detailed data structure diagram
MySQL uses indexes for these operations
- hit where The condition in the statement
- Filter out inappropriate rows ( The value on the column exceeds the storage length , Can't equal hit , Only unequal values can be filtered out )
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. ( Original address )
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).
Example of the official website of the leftmost matching principle :
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) have access to name Indexes :
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) Can't use name Indexes
SELECT * FROM test WHERE first_name='John';
SELECT * FROM test
WHERE last_name='Jones' OR first_name='John';
Implicit compatibility of Federated indexes :(col1, col2, col3) It's kind of set up (col1), (col1, col2), and (col1, col2, col3) Indexes
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; -- You can use the index
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2; -- You can use the index
SELECT * FROM tbl_name WHERE col2=val2; -- You cannot use an index
SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3; -- You cannot use an index
- Table connecting , Same type 、 length 、 The index of character set can be used as a connection condition to improve the efficiency
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.
- Use different parts of the index to improve efficiency
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;
Change the meaning of the above :max(key_part2) where col = key_part1; If there is a federated index index(key_part1,key_part2) So this query is very fast . Query optimization is based on the index B+ The ordering of the tree is very fast ,max The declared column is in the second position of the union index , Then the first position also needs to use equivalent hit . And so on ,index(key_part1,key_part2,key_part3) max(key_part3) You need to make the first two index columns do where Equal hit . It is worth mentioning that index(key_part1,key_part2,key_part3) stay SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE key_part1=10; Can still go through the index . because max(key_part2) Only required key_part1 Equivalent hit
- order by When using composite index , Note the leftmost match and reverse order .DESC The index is read in the reverse order of the expression .
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.
- Index overlay
In some cases, a query can be optimized to retrieve values without consulting the data rows
Look up the index directly B Trees can get data , No need to go through the index
SELECT key_part3 FROM tbl_name
WHERE key_part1=1
- Avoid full table scanning (EXPLAIN come out Type The value of the field is ALL )
The output from EXPLAIN shows ALL in the type column when MySQL uses a full table scan to resolve a query.
Full table scanning will occur :
- A table with very few rows
- ON perhaps WHERE The selected column is not indexed
- Query optimizer pair where Clause , Found that full table scans are faster than indexes .
- Query keywords used on the index , There is not much differentiation in the index distribution
- For tables with large amounts of data , Avoid wrong index selection by the query optimizer
SELECT * FROM t1, t2 FORCE INDEX (index_for_column)
WHERE t1.col_name=t2.col_name;
边栏推荐
- 大神详解开源 BUFF 增益攻略丨直播讲座
- [leetcode] 13. Roman numeral to integer
- Focus on the 35 year old Kan: fear is because you don't have the ability to match your age
- Big God explains open source buff gain strategy live lecture
- MIPS assembly language learning-03-cycle
- Qt 界面库
- Go zero micro Service Practice Series (VII. How to optimize such a high demand)
- Basic grammar of C language
- 今天睡眠质量记录80分
- What is the difference between treasury bonds and time deposits
猜你喜欢

Go zero micro Service Practice Series (VII. How to optimize such a high demand)
PostgreSQL enables grouping statistics by year, month, day, week, hour, minute and second

全球陆续拥抱Web3.0,多国已明确开始抢占先机

The world has embraced Web3.0 one after another, and many countries have clearly begun to seize the initiative

Visual Studio 2010 配置和使用Qt5.6.3

今天睡眠质量记录80分

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

隆重推出 Qodana:您最爱的 CI 的代码质量平台

平台即代码的未来是Kubernetes扩展

使用Karmada实现Helm应用的跨集群部署
随机推荐
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
【高并发基础】MySQL 不同事务隔离级别下的并发隐患及解决方案
ROS knowledge points - definition and use of topic messages
tablestore中可以使用sql查询可以查出表中所有的数据吗?
关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
Talking about open source - Linus and Jim talk about open source in China
Android, eclipse and MySQL upload pictures and get
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
The sadness of software testers is Their own technical ability can not meet the requirements of large manufacturers?
10: 00 interview, came out at 10:02, the question is really too
[recommendation system] esmm model of multi task learning (updating)
关于针对tron API签名广播时使用curl的json解析问题解决方案及针对json.loads方法的问题记录
薅羊毛的机会了,点个“赚”即有机会赚取高额佣金
FFmpeg之禁止输出banner log(三十)
3. Caller 服务调用 - dapr
【Proteus仿真】L297驱动步进电机
成功迁移到云端需要采取的步骤
物联网云融合安全指南
经典模型——Transformer
首次失败后,爱美客第二次冲刺港交所上市,财务负责人变动频繁