当前位置:网站首页>MySQL index
MySQL index
2022-06-12 23:52:00 【InfoQ】
Indexes
- Improve the efficiency of data retrieval , Reduce the IO cost
- Sort data through index columns , Reduce the cost of sorting data , Reduce CPU Consumption of
- Index columns also take up space
- Indexing greatly improves query efficiency , But it reduces the speed of update , such as INSERT、UPDATE、DELETE
Index structure
B-Tree



B+Tree

- All the data will appear in the leaf node
- Leaf nodes form a one-way linked list

Hash

- Hash Indexes can only be used for peer-to-peer comparisons (=、in), Range query is not supported (betwwn、>、<、...)
- Cannot complete sort operation with index
- High query efficiency , Usually only one search is needed , Efficiency is usually higher than B+Tree Indexes
- Memory
- InnoDB: It has adaptive function hash function ,hash The index is the storage engine B+Tree The index is automatically built under specified conditions
Interview questions
- Why? InnoDB The storage engine chooses to use B+Tree Index structure ?
- Relative to a binary tree , Fewer levels , High search efficiency
- about B-Tree, Whether leaf nodes or non leaf nodes , Data will be saved , This results in fewer key values stored in a page , The pointer decreases as well , Save a lot of data as well , Can only increase the height of the tree , Resulting in reduced performance
- be relative to Hash Indexes ,B+Tree Support range matching and sorting operations
Index classification


- If there is a primary key , A primary key index is a clustered index
- If there is no primary key , The first unique... Will be used (UNIQUE) Index as clustered index
- If the table does not have a primary key or an appropriate unique index , be InnoDB It will automatically generate a rowid As a hidden clustered index
Thinking questions
select * from user where id = 10;
select * from user where name = 'Arm';
-- remarks :id Primary key ,name Fields are indexed
n * 8 + (n + 1) * 6 = 16 * 10241171 * 16 = 187361171 * 1171 * 16 = 21939856grammar
CREATE [ UNIQUE | FULLTEXT ] INDEX index_name ON table_name (index_col_name, ...);SHOW INDEX FROM table_name;DROP INDEX index_name ON table_name;-- name The field is the name field , The value of this field may be repeated , Create an index for this field
create index idx_user_name on tb_user(name);
-- phone The value of the mobile phone number field is not empty , And only , Create a unique index for this field
create unique index idx_user_phone on tb_user (phone);
-- by profession, age, status Create a federated index
create index idx_user_pro_age_stat on tb_user(profession, age, status);
-- by email Establish an appropriate index to improve query efficiency
create index idx_user_email on tb_user(email);
-- Delete index
drop index idx_user_email on tb_user;
Usage rule
The leftmost prefix rule
Index failure
- Perform operations on index columns , Index will fail . Such as :
explain select * from tb_user where substring(phone, 10, 2) = '15';
- When using string type fields , Without quotes , Index will fail . Such as :
explain select * from tb_user where phone = 17799990015;, here phone The value of is not quoted
- Fuzzy query , If it's just tail blur matching , The index will not be invalidated ; If it's a fuzzy head match , Index failure . Such as :
explain select * from tb_user where profession like '% engineering ';, Before and after % It's going to fail .
- use or The conditions of separation , If or The column of one of the conditions has no index , Then the indexes involved will not be used .
- If MySQL Evaluation uses indexes more slowly than full tables , Index is not used .
SQL Tips
explain select * from tb_user use index(idx_user_pro) where profession=" Software Engineering ";explain select * from tb_user ignore index(idx_user_pro) where profession=" Software Engineering ";explain select * from tb_user force index(idx_user_pro) where profession=" Software Engineering ";Overlay index & Return to the table for query
using index conditionusing where; using index;select id, name from xxx where name='xxx';select id, name, gender from xxx where name='xxx';select *select id, username, password from tb_user where username='itcast';Prefix index
create index idx_xxxx on table_name(columnn(n));select count(distinct email) / count(*) from tb_user;
select count(distinct substring(email, 1, 5)) / count(*) from tb_user;
Single index & Joint index
explain select id, phone, name from tb_user where phone = '17799990010' and name = ' Han xin ';matters needing attention
- When multi condition joint query ,MySQL The optimizer evaluates which fields are more efficient to index , The index will be selected to complete this query
Design principles
- For large amount of data , The tables that are frequently queried are indexed
- For often used as query criteria (where)、 Sort (order by)、 grouping (group by) Index the fields of the operation
- Try to select highly differentiated columns as indexes , Try to build a unique index , The more distinguishable , The more efficient the index is
- If it is a string type field , Long field length , You can focus on the characteristics of the field , Building prefix index
- Try to use a federated index , Reduce single column index , When inquiring , Joint indexes can often overwrite indexes , Save storage space , Avoid returning to your watch , Improve query efficiency
- To control the number of indexes , The index is not that more is better , More indexes , The more expensive it is to maintain the index structure , It will affect the efficiency of addition, deletion and modification
- If the index column cannot store NULL value , Please use... When creating the table NOT NULL Constrain it . When the optimizer knows whether each column contains NULL When the value of , It can better determine which index is most effectively used for queries
边栏推荐
- H5时代leaflet中还在用DivIcon?
- 测试平台系列(97) 完善执行case部分
- SAP UI5 如何通过 manifest.json 文件定义第三方库依赖关系
- 进程间通信-共享内存shmat
- Deep feature synthesis and genetic feature generation, comparison of two automatic feature generation strategies
- Do you have to read for PMP?
- Start of u-boot_ Armboot analysis (II)
- Is the revised PMP worth testing?
- Buuctf-[ciscn 2019 preliminary]love math
- 2022年R2移动式压力容器充装考试题及在线模拟考试
猜你喜欢

线上真实排队系统重构案例分享——实战篇

Introduction to message oriented middleware (message queue)

leaflet如何加载10万条数据

Tableau

Case sharing of online real queuing system reconfiguration -- practical part
![Software development tools [3] theoretical basis of software development tools](/img/24/6a3c593931523ceb17e323bc0367b4.jpg)
Software development tools [3] theoretical basis of software development tools

How to get Matplotlib figure size

dict和set的基本操作

CV—BaseLine总结(从AlexNet到SENet的发展历程)

【Matlab】二维曲线
随机推荐
如何利用华为云容灾解决方案替代灾备一体机
【Matlab】矩阵操作
How to gracefully solve the offset problem of Baidu and Gaode maps in leaflet
如何实现OSM地图本地发布并自定义配图
[matlab] two dimensional curve
leaflet如何加载10万条数据
VHDL编程实验练习题合集
Online examination questions for September examination of financial management
vs studio_ How to use scanf in 2022
March 11, 2022 diary: Mr. Wang's spring, strange template mode
Divicon est toujours utilisé dans le leaflet de l'ère H5?
【HCIE论述】STP-A
June 11, 2022 diary: Mr. Wang's spring, mixed in
How to use Huawei cloud disaster tolerance solution to replace disaster recovery all-in-one machine
Do you have to read for PMP?
2022年3月11日记:王老师的春天,奇异的模板模式
Day 3 of jsbom and DOM learning
QT actual combat case (38) -- using qpprocess class to realize the function of starting process
Xi'an Jiaotong 22nd autumn e-commerce technology online expansion resources (IV) [standard answer]
SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍