当前位置:网站首页>[database basic dry goods] MySQL foundation and slow query optimization practice
[database basic dry goods] MySQL foundation and slow query optimization practice
2022-07-22 20:03:00 【Delphi on the road】
Knowledge reserve
In-memory database : Fast , The high cost , Indexes :Map、 Balance tree 、T Trees , application Redis、Memchached
Disk database : Faster , The cost is moderate ( virtual machine , The physical machine ), Index hash ,B+ , application :MySQL、Oracle
Distributed database : Slower , Large capacity , Storage : Vertical table 、HDFS
SQL Execution phase
- The client sends a query to the server , arrive The connector .
- The server checks the cache first , If the cache is hit , Return results immediately .
- arrive analyzer On the server side SQL Parsing , Generate parse tree .
- Again by Optimizer Generate the corresponding execution plan .
- According to the execution plan , Call the specific storage engine (InnoDB) Returns the result to the client .
InnoDB Why should the engine be used B+ data
1、 Every node ( Data pages ) by 16K, Leaf nodes store data .
2、 Multiforked tree , Reduce floor height , Reduce IO, Generally, it only needs 3~4 Time IO that will do .
3、 There are order pointers between leaf nodes .
4、B+ The main index stores all data ,B The secondary index of the tree only stores the primary key ID.
What are the benefits of using indexes ?
1、 Reduce the amount of data scanned by the server , Improve query efficiency
2、 Servers avoid sorting and temporary tables
Analyze the causes and solutions
Explain usage
- table : Which table to query
- rows : Estimate the number of lines to scan
- ref : Which field and key Use it together
- key : Index used
- possible_keys : Available indexes , More than one
- key_len : The number of bytes used in the index , The shorter the length, the better
- type : ALL( Scan the whole table )、index( Scan full index )、range( Range index )、ref( Indexes , Match single value )、const( Primary key or unique key , Match only one line )
- filtered : Percentage of data filtered by this query criteria
- extra: Additional information eg: Using filesort( The external index sort is not read according to the index order in the table )、Using index( Use overlay index )、Using index condition ( Prerequisite filtering , Check the data again )、Using where( Use where Statement to process the result ) 、Using Temporary( A temporary table , Common in order by and group by)
Slow query management ideas
Index level
1、 There is no index
2、 Index failure
select * from ShopTable where shopid > 10000000 VS where shopid > 50000000
Although give to shopid Added general index , But enforcement where shopid > 10000000 Indexes are not used , Back to the table ( That is, find out which line it is from the index , Then go back to the table, take out all the fields and return ) This leads to a full table scan when the amount of data that needs to be returned to the table is greater than 20%,MySQL I think the full table scan will be faster , So don't go to the index .
Suggest :
- Reduce use select * , The index overrides the fields that need to be returned , That is, the index column is consistent with the query column
- Reduce the amount of data , Consider paging when the amount of data is too large
3 、 The index is not comprehensive
where shopid > 234512 and update_days > -1 and buy_time > 300
Only for shopid The index of , Inefficient indexing , After the query, you need to go back to the table and filter , Resulting in a serious decline in efficiency
Suggest :
increase update_days and buy_time Index of field
4、Order By /Group By /Distinct Avoid temporary tables
5、 It is recommended to use the auto increment column as the primary key create table test(
idint(11) NOT NULL PRIMARY KEY (
id))
6、 The primary key is set to int unsigned or bigInt unsigned
7、 There are too many indexes in the table
8、 Unique index: check the uniqueness of data , If the data is not unique, it is possible to automatically delete duplicate columns when adjusting the online table structure , This may lead to data loss
9、
SQL level
Avoid incorrect or inefficient writing 、 Avoid complex SQL
1、limit 6000000 , 10
limit Too big ,limit A,B Can scan X+Y Data , When X When it is too large , A large number of scanning lines leads to slow queries
Suggest :
Use as much as possible id > xx LIMIT 10, It is recommended to use ID
2、
General index KEY shopid
General index KEY poiid
Composite index KEY combined(shopid,poiid)
Only the index is triggered when using merge i2 && KEY combined
Suggest :
Delete Key shopid Indexes , Reduce index space
3、 A complex SQL Or more simple SQL
4、 It is not recommended to use the preceding wildcard to find select c1,c2,c3 from tbl where name like '%foo';
5、 There are no wildcards LIKE Inquire about select c1,c2,c3 from tbl where name like 'foo';
6、 Parameter comparison contains implicit conversions , Index not available SELECT * FROM sakila.film WHERE length >= '60';
7、IN Use with caution , Too many elements will cause a full table scan , It is recommended to use between X and Y Instead of
8、 To avoid the WHERE Use functions or other operators in conditions
Table library layer
1、 Check the status of the table show table status like ‘table_shop’
Rows : 180000000
Data_length : 30G
The amount of data in a single table is too large , It is suggested that the single table should not exceed 1 Ten million lines
2、 Whether it is necessary to go to the main warehouse
Master-Slave framework , Main warehouse single node , It is easy to become a resource bottleneck
3、 normal form : Make sure that each column is atomic ( It can't be broken down )、 Uniqueness
Anti paradigm : Through redundant fields , Improve query efficiency
application layer
Change the storage engine 、 cache
Business logic optimization
The specification
- Use InnoDB Storage engine : Support transactions 、 Row-level locks 、 Better concurrency
- Data sheet 、 Data fields must be annotated in Chinese , Over time, you will forget the exact meaning of the field
- Stored procedures are not allowed , View , trigger ,Event, The purpose of the database is to store and index , Computing logic should be put into the service layer
- It is forbidden to store large files and photos , Stored in the file system , Database storage URI that will do
Build a table to regulate
- The field is designed as NOT NULL,NULL The column of / The index statistics / Value comparisons are more complex , Large storage space , You need to use is null 、is not null
- Must have primary key , Increasing the primary key improves the efficiency of data row writing , And the primary key selects a shorter data type , It can reduce the index disk space , Suggest adding xxx_create ,xxx_update Field
- Do not use foreign keys , Foreign keys cause coupling between tables , to update 、 The deletion operation will involve the associated table , It not only affects the performance, but also may cause deadlock
- Use varchar(20) Store phone number , Because there will be symbols when it comes to countries (±),varchar Support fuzzy query ‘like 173’
- The size of a single table should not exceed 1 Ten million lines
The index specification
- Single table index quantity control , Generally in 5 Within a
- Joint index and overlay index are two aspects , A federated index is an index type , Coverage index is an optimal case , You don't have to go back to the table
- A union index requires a leading column , Overlay index is to index both data , The index may only use one field
Joint index
Yes (a,b,c) Conduct joint indexing , Determine whether to go to the index
a = 1 //true
a = 1 and b = 2 //true
b = 2 and a = 1 //true,Mysql There is an optimizer that will automatically adjust a,b The order of is consistent with the index order , Pay attention to put the fields with high discrimination in front
b = 1 //false,
a = 1 and b = 2 and c > 3 and d = 4 //a,b,c Three fields can be indexed , and d It doesn't match , The leftmost matching principle stops matching when it encounters a range query
How to build a joint search problem
SELECT * FROM table WHERE a = 1 and b = 2 and c = 3; // (a,b,c) (b,a,c) (c,a,b) Fine , Put the fields with high discrimination in the front
SELECT * FROM table WHERE a > 1 and b = 2; // (b,a) Index , The optimizer will help us adjust where after a,b The order of
SELECT * FROM `table` WHERE a > 1 and b = 2 and c > 3; // (b,a) or (b,c) Can the
SELECT * FROM `table` WHERE a = 1 ORDER BY b; // Yes (a,b) Index building
SELECT * FROM `table` WHERE a IN (1,2,3) and b > 1; // Yes (a,b) Index , because IN It can be regarded as equivalent reference here , Index matching is not aborted
Which situations are not suitable for indexing
- Prohibit frequent updates 、 Build an index on the attribute field with low discrimination , Updates will change B+ Tree structure
- “ Gender ” It is meaningless to index such attributes with low discrimination
- Where Fields that cannot be used by the condition are not indexed
- There are too few records
- Frequently added and deleted tables
SQL matters needing attention
- Do not use where Use the function on the column name of the condition 、 Calculation 、 Type conversion
- It is forbidden to use left fuzzy or full fuzzy queries , Such as like ‘%abc’ ,like ‘%abc%’
- To use less or
- is null ,is not null You can't use the index
- It is forbidden to use large offset limit Pagination
- prohibit 3 Above the table join
Recommend learning :
https://ipu.sankuai.com/app/course/detail/1927
High performance SQL and MySQL Official documents (https://www.mysql.com)
边栏推荐
- 优博讯助力深圳打造全球“鸿蒙欧拉之城”
- 人和产品的五个层次
- Spark RDD算子:RDD分区,HashPartitioner、RangePartitioner、自定义分区
- * flutter 问题记录
- Oracle 11g installs and starts EM based on centos7
- NC26 括号生成
- 日常工作规范
- Common performance tools: if you want to be good at something, you must first sharpen its tools
- 小鸟平台隐私政策
- C regular expression extracts the index position where the specified word appears
猜你喜欢
Development and construction of NFT card chain game system DAPP
开幕在即 | “万物互联,使能千行百业”2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛
Opening soon | openatom openharmony sub forum of 2022 open atom global open source summit "interconnection of all things, enabling thousands of industries"
shell script “<< EOF”我的用途和遇到的问题
Youboxun helps Shenzhen build a global "city of Hongmeng Oula"
NewSQL數據庫數據模型設計
数据架构与数据库建模
Spark SQL 内置函数和自定义函数UDF
JVM jhat (virtual machine heap dump snapshot analysis tool)
优博讯助力深圳打造全球“鸿蒙欧拉之城”
随机推荐
低代码和无代码有什么区别?
xcode 编译pod 第三方库报错
Simple use of JVM's JPS
通过shell脚本进行数据库操作
shell中小数运算(bc)
【读书笔记】《MySQL体系结构和存储引擎》
ES6 new features sharing (III)
Pyinstaller packaging scene
NFT挖矿分红系统开发模式定制
mysql 主从同步出问题,重新修复从库(转)
C WinForm DataGridView column full width
leetcode 32. 最长有效括号
leetcode 394. 字符串解码
WPF textbox limits two ways to enter numbers only
Flutter 2进阶(八):EventBus、轮播图与沉浸式状态栏
LogBack & MDC & a simple use
shell 脚本编写提示
数据架构与数据库建模
stat函数详解
Customization of development mode of NFT mining Dividend System