当前位置:网站首页>[MySQL learning notes 23] index optimization
[MySQL learning notes 23] index optimization
2022-06-28 14:34:00 【yqs_ two hundred and eighty-one million eight hundred and seven】
Check the execution frequency of the database
show global status like ‘com_______’;
Slow query log
Introduce
The slow query log records all events that have exceeded the specified time ( Default 10s) Of SQL sentence .
Default mysql Do not enable slow query
To configure
my.cnf
# Turn on mysql Slow log switch
slow_query_log=1
# Set the time of slow log to 2s
long_query_time=2
Check the log
Open file localhost-slow.log You can see it.
profile
Introduce
profile yes mysql Functions provided to monitor the execution of query statements
View your mysql Does the version support profile function
select @@have_profiling;
If it shows YES, It means that 
Turn on profile
set [session|global] profiling = 1;
# If specified session Then it will only take effect for the current session , If specified global It is effective for the global
see profiles
show profiles;# Query each item sql Time consuming
show profile for query query_id;# View specified id Of sql Statement details take time
show profile cpu for query query_id;# View specified id Of sql sentence cpu usage
explain Implementation plan
Usage method
stay sql Add a... Before the statement explain perhaps desc Key words can be used
explain select * from emp where id=5;
Execution results

Partial column meaning
id: Primary key
select_type: Express select type , Common values are simple( A simple watch , That is, it does not apply to table joins or subqueries ),primary( Main query , That is, the outer query ),union(union The second or subsequent statement in ),subquery( Subquery )
type: Indicates the type of access , Indicates the connection type , The order of performance from good to bad is NULL、system、const、eq_ref、ref、range、index、all
possible_key: Indexes that may be referenced
key: Actual index
key_len: Number of bytes of index used
rows: Number of rows to execute the query , It's a valuation
filtered: Indicates the percentage of returned rows in the number of rows to be read ,filtered The greater the value, the better
Index optimization
The leftmost prefix rule
If you index multiple columns , Follow the leftmost prefix rule . The leftmost prefix rule means that the query starts from the leftmost part of the index , And do not skip the columns in the index . If you skip a column , Then this column and all subsequent indexes will be invalidated .
Range queries
If a range query appears in the union index , Column index failure on the right side of the range
Column operation
If the index column is involved in the operation , Then the index is invalid
String of characters
If you don't use quotation marks for character strings , You don't use indexes . Without single quotation marks, there will be implicit type conversion or direct error reporting , Don't go by index .
Fuzzy query
If the tail is fuzzy , The index works , If the header performs a fuzzy query , Index does not work .
or Inquire about
Use or When inquiring , As long as one of the conditions has no index , Then the whole will fail
Data distribution prediction
If mysql It is predicted that the efficiency of using indexes is not as good as that of full table scanning , The whole table will be scanned directly , For example, there is a field that is age,age The minimum value of is 10, So if one condition is age>=10,mysql Will directly scan the whole disk , because age>=10 You can see that the data volume covers the whole table , There's no need to use an index .
SQL Tips
use index
by sql Statement provides a reference index
select * from table use index(name,age);
ignore index
tell mysql Do not use certain indexes
select * from table ignore index(name,age);
force index
mandatory mysql Use the specified index
select * from table_name force index(index_name) where xxx;
Overlay index
The columns of the query should be included in the index as much as possible , This is efficient , Otherwise, you need to query back to the table to get the value of the target column
Prefix index
If some fields are long strings , Using this field to create an index will consume a lot of space , Do more harm than good ,mysql Provides us with a prefix index , You can extract the first few characters of a string as an index , Reduce index space .
create index index_name on table_name(column(n));
Selection of single column index and joint index
Imagine this situation
info Table structure
id: Primary key
name: full name
phone: Telephone
And all three fields have indexes ,id Corresponding to the unique index ( Aggregate index ),name and phone It's ordinary. B+Tree Indexes
When we subquery like this :
select id,name,phone from info where name=‘xxx’ and phone=‘xxxx’;
because name and phone They're all single column indexes ,mysql Will choose one of them to use , Not both .
Suppose the choice is name Indexes , So according to name The only data found in the index is name and id Information , did not phone Information , Therefore, you must perform a back table query to find out the complete data .
And when we work for name,phone After building a union index ,mysql We only need to query the joint index once to return the information we need , One back to table query is missing , More efficient .
边栏推荐
- 3. Overall UI architecture of the project
- Four methods of thread termination
- Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
- Configuration file encryption (simple use of jasypt)
- Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index
- 腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
- Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship
- CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition
- 从小小线虫谈起——溯源神经系统进化,开启生命模拟
- 推荐四款可视化工具,解决 99% 的可视化大屏项目!
猜你喜欢

Mulan open work license 1.0 open to the public for comments

Based on asp Net based document retrieval system

Foreign trade SEO Webmaster Tools

Ding! Techo day Tencent technology open day arrived as scheduled!

基于MATLAB的混沌数字图像加密技术研究与仿真实现

IonQ联合GE Research证实:量子计算在风险聚合上有巨大潜力

Adding virtual environments to the Jupiter notebook

【中移芯昇】5. spi接口测试tf卡

Why can't Bert completely kill the BM25??

Only four breakthrough Lenovo smart Summer Palace in mainland China won the "IDC Asia Pacific Smart City Award in 2022"
随机推荐
Solving Hanoi Tower problem
open3d里pointcloud和numpy数组之间的转化
【数字IC精品文章收录】近500篇文章|学习路线|基础知识|接口|总线|脚本语言|芯片求职|安全|EDA|工具|低功耗设计|Verilog|低功耗|STA|设计|验证|FPGA|架构|AMBA|书籍|
同花顺上开户是安全的吗
A bug liver a week I can't help mentioning issue
3. Overall UI architecture of the project
配置文件加密(Jasypt的简单使用)
MySQL slave error: "you cannot 'alter' a log table“
2022 operation of simulation test platform for 100 simulated questions of main principals of metal and nonmetal mines (underground mines)
js 判断字符串为空或者不为空
基于ASP的勤工俭学管理系统
Kwai investment e-commerce service provider Yixin optimization
基于asp.net的文献检索系统
Unable to create process using 'd:\program file
2022金属非金属矿山安全检查(地下矿山)复训题库及在线模拟考试
名创优品通过上市聆讯:寻求双重主要上市 年营收91亿
字节跳动埋点数据流建设与治理实践
Who is the main body of the waiting insurance record? Record in the local network security, right?
解决Unable to create process using ‘D:\Program File
Rslo: self supervised lidar odometer (real time + high precision, icra2022)