当前位置:网站首页>[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 .
边栏推荐
- 美因基因港交所上市:市值43亿港元 IPO被市场忽略
- Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder
- 加油站(贪心)
- Regular matching numbers, English and English symbols
- 量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
- Foreign trade SEO Webmaster Tools
- Leetcode (406) - rebuild the queue based on height
- Recommendation letter brain correspondent: if love is just a chemical reaction, can you still believe in love?
- 【中移芯昇】5. spi接口测试tf卡
- 字节跳动埋点数据流建设与治理实践
猜你喜欢
叮!Techo Day 腾讯技术开放日如约而至!
基于MATLAB的混沌数字图像加密技术研究与仿真实现
荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?
Recommended practice sharing of Zhilian recruitment based on Nebula graph
快手投资电商服务商易心优选
openGauss内核:SQL解析过程分析
Thread life cycle and its methods
Ding! Techo day Tencent technology open day arrived as scheduled!
安杰思医学冲刺科创板:年营收3亿 拟募资7.7亿
线程的生命周期以及其中的方法
随机推荐
Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
How to count dimensions of foreign trade E-mail Promotion
美因基因港交所上市:市值43亿港元 IPO被市场忽略
2022 operation of simulation test platform for 100 simulated questions of main principals of metal and nonmetal mines (underground mines)
Clipping of raster vector data
由两个栈组成的队列
Only four breakthrough Lenovo smart Summer Palace in mainland China won the "IDC Asia Pacific Smart City Award in 2022"
Single responsibility principle
[Zhongyi Xinsheng] 5 SPI interface test TF Card
从小小线虫谈起——溯源神经系统进化,开启生命模拟
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
2022金属非金属矿山安全检查(地下矿山)复训题库及在线模拟考试
Design a stack with getmin function
Thread life cycle and its methods
Tencent cloud international ECS has no network after logging in. How to troubleshoot?
Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder
开闭原则
Regular matching numbers, English and English symbols
dolphinscheduler2.X的安装(亲测有效)