当前位置:网站首页>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 * 1024
1171 * 16 = 18736
1171 * 1171 * 16 = 21939856
grammar
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 condition
using 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
边栏推荐
- Running of NCF dapr application instance
- Novice must see! How rust beginners write gear smart contracts (1)
- [leetcode] understanding and usage of map[key]+
- Introduction to business rules service on SAP Business Technology Platform (BTP)
- PMP training organization
- Video tracker error troubleshooting
- How does idea switch the interface to Chinese
- Sequence maximum return
- 【Matlab】三维曲线与三维曲面
- 2022-06-13日报: 图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?
猜你喜欢
随机推荐
2022-06-13日报: 图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?
支持Canvas的Leaflet.Path.DashFlow动态流向线
leaflet如何优雅的展示重叠点位的气泡窗口
How to get Matplotlib figure size
Actual combat | UI automation test framework design and pageobject transformation
Xi'an Jiaotong 22nd autumn e-commerce technology online expansion resources (IV) [standard answer]
【HCIE论述】RR-A
【Matlab】多项式计算
How does idea switch the interface to Chinese
Test platform series (97) perfect the case part
M_ 8: Design a MySQL table for message queue to store message data
How to gracefully solve the offset problem of Baidu and Gaode maps in leaflet
Why study PMP?
Vscode实现PHP在浏览器实时预览
Matlab【路径规划】—— 无人机药品配送路线最优化
1111111111111111111111111111111111111111111111111111111
设计消息队列存储信息数据的MySQL表结构
2022年3月11日记:王老师的春天,奇异的模板模式
Summary of individual NLP internship experience
Ast, really fragrant