当前位置:网站首页>How to optimize MySQL?
How to optimize MySQL?
2022-06-13 10:24:00 【CodeMartain】
Mysql The optimization of the
Mysql The optimization of the
The performance of a database depends on a number of factors at the database level , For example, the design of tables 、 Query statements and database configuration settings . These software architectures lead to hardware level CPU and I/O operation , We must reduce CPU and IO To improve efficiency as much as possible ;
Optimize at the database level ----
1, Consider whether the table structure is reasonable ?---- Three principles of paradigm
Whether the data type is appropriate
2, Whether the correct index is set ?---- The index duplicate field should not have
3, For each table Storage engine Whether it is used properly ?( especially , Select transactional storage engine ( Such as innodb) Or non transactional storage engine ( Such as mysiam) It can be very important for performance and scalability ),
4, Whether the appropriate locking strategy is used ---- Table locks / Row lock so that the database can operate concurrently ;
5, Whether the size of all memory areas used for caching is appropriate ?
This can be adjusted mysql Size of the buffer in (bufferpoolmoren 64M), But not too big ;
Hardware level optimization ---->>
1, disk , It takes a while for the disk to find data , Usually in 10ms about , So theoretically every second 100 Search , Optimizing seek time is to distribute data to multiple disks ;
Or maybe Replacing high-performance disks can reduce disk seek time For example, using a solid-state drive ;
2,cpu Speed , Replace with high performance cpu It can also speed up processing ;
3, Memory bandwidth , When CPU The data required exceeds CPU When caching the data it can hold , Main memory bandwidth will become a bottleneck ;
Hardware level is not in the scope of this discussion
Optimize sql sentence
1, Query statements are often used sql sentence , Key considerations for optimizing queries :
- To make slow queries faster ----->> Check if the index can be added , Set the index for the column in the clause ;
The more indexes, the better , Consider that indexes also take up disk space ; - For queries that reference different tables using joins, foreign keys, and so on , have access to explain Statement to determine which indexes are used ;
- Isolate and optimize any part of the query
- Minimize the number of full table scans , Especially for large tables
- The statistical information of the table system can be kept up to date by regularly analyzing the table , Optimize sql Implementation plan of (select etc. )
- If a performance problem cannot be easily solved by a basic rule , You can use EXPLAIN analysis sql sentence And adjust the index 、 Clause 、 Join clauses, etc. to investigate the internal details of a particular query .
- adjustment MySQL be used for Size and properties of the cached memory area . Through effective use Buffer pool 、 Key cache and MySQL The query cache , Repeated queries run faster , Because the results will be retrieved from memory at the second and subsequent times .
Mysql Optimize the details
Usually, the frequency of reading data is higher than that of writing data , So the first one should consider optimization select sentence ;
Data sheet design :
Data type optimization ---->>>
1, Data types that take up less are usually better ( Because more data can be read at a time , This means that it usually takes less to read the same amount of data CPU Processing cycle ;
For example, use mysql Instead of using strings to store dates ;
Store in integers ip Instead of strings ,(mysql There is a function in that can put IP Convert to integer INET_ATON())
2, Simple is good , Avoid using null
3, Avoid using null, stay mysql in nul=null The result is null, However, sometimes the user fails to fill in the information , At this point, we can specify the data as ’’ Empty string instead of null
4, Set the primary key properly
5, Rational use of paradigms and paradigms ( According to the actual situation , Data redundancy can be properly provided )
6, Character set selection
7, Choice of storage engine
8
Optimize select sentence
1, Solve the problem of slow query
First, check the slow query log , Find out the cause of slow query ;
Generally, slow query optimization needs to start from Start with the index ------ Try adding an index to the field you want ;
For the use of Join and foreign key And other functions refer to queries of different tables , Indexing is especially important .
2, Cache commonly used data ;
adjustment MySQL The size and properties of the memory area used for caching . By using buffer pools effectively 、 Key cache and MySQL The query cache , Repeated queries run faster ,
3, Reduce the number of full table scans ;
Try to use triggers cont ,index And so on , Avoid back table queries ;
4, Use regularly analyze table Statement keep statistics up to date ;
where Clause optimization
Remove unnecessary brackets :
((a AND b) AND c OR (((a AND b) AND (c AND d))))
-> (a AND b AND c) OR (a AND b AND c AND d)
1, Index merge optimization
The index merge access method retrieves rows with multiple range scans , And combine the results into one . This access method only merges index scans from a single table , Instead of merging scans across multiple tables . Merge can generate union of its underlying scans 、 Intersection or intersection Union .
Let's look at a table first ----->>
mysql> select * from test ;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | Zhang San 2 | NULL |
| 4 | Zhang San 4 | NULL |
| 6 | Li Si | NULL |
| 7 | Xiao Qi | NULL |
| 8 | Wang Wu | NULL |
| 10 | Li Si 10 | NULL |
| 13 | Zhao 13 | NULL |
| 18 | Wang Wu 18 | NULL |
+----+----------+------+
8 rows in set (0.00 sec)
# The first table , There are primary keys and indexes name;
mysql> show create table test \G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` int NOT NULL,
`name` varchar(8) COLLATE utf8mb4_general_ci DEFAULT NULL,
`age` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `nameindex` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
1 row in set (0.00 sec)
# The other table Only primary keys
mysql> show create table test3 \G
*************************** 1. row ***************************
Table: test3
Create Table: CREATE TABLE `test3` (
`id` int NOT NULL,
`name` varchar(8) COLLATE utf8mb4_general_ci DEFAULT NULL,
`age` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
1 row in set (0.01 sec)
Next, let's query id Less than 10 and name Is Xiaoqi The data of , How to improve query efficiency ??
The first table ----->>
mysql> explain select * from test where id <10 and name=" Xiao Qi " \
G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
partitions: NULL
type: index_merge
possible_keys: PRIMARY,nameindex
key: nameindex,PRIMARY
key_len: 39,4
ref: NULL
rows: 1
filtered: 100.00
Extra: Using intersect(nameindex,PRIMARY); Using where
1 row in set, 1 warning (0.00 sec)
The cases given on the official website —>>
SELECT * FROM tbl_name WHERE key1 = 10 OR key2 = 20;
SELECT * FROM tbl_name
WHERE (key1 = 10 OR key2 = 20) AND non_key = 30;
SELECT * FROM t1, t2
WHERE (t1.key1 IN (1,2) OR t1.key2 LIKE 'value%')
AND t2.key1 = t1.some_col;
SELECT * FROM t1, t2
WHERE t1.key1 = 1
AND (t2.key1 = t1.some_col OR t2.key2 = t1.some_col2);
边栏推荐
- Software testing often asks, do you really build a testing environment?
- June training (day 13) - two way linked list
- C# Oracle 多表查询
- Node-RED系列(二五):集成高德地图并实现3d地图和轨迹回放
- 多线程 从UE4的无锁队列开始 (线程安全)
- Node-RED系列(二七):扩展节点之s7节点的使用说明
- spark源码(一)spark-submit如何将jar以及配置参数提交给spark服务器
- ThingsBoard教程(二一):使用消息类型和数据处理节点对数据处理后保存
- Programming principles
- Smart210 uses SD card to burn uboot
猜你喜欢
The rise of cloud computing enterprises and the shaking of Oracle database market dominance
Classical convolutional neural network model
低代码开发一个基础模块
Write pytoch model in five minutes
计算循环冗余码--摘录
Smart210 uses SD card to burn uboot
检验冗余码是否出错题型解法-摘录
Learning makefile with me
Docker部署Mysql
Node red series (27): instructions for S7 node of the extension node
随机推荐
实战模拟│企业微信机器人实时报错预警
Oracle自定义数据类型Type疑问
【ELM分类】基于粒子群优化卷积神经网络CNN结合极限学习机ELM实现数据分类附matlab代码
GCC compilation process
软件测试常问,你真的会搭建测试环境吗?
Node red series (25): integrate Gaode map and realize 3D map and track playback
Pxxx local socket communication
UNIX Environment advanced programming --3-file io---3.10 file sharing
Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
GPIO of hardware schematic diagram
简述请求过程
C# 11 更加实用的 nameof
架构师必备:系统容量现状checklist
Learning makefile with me
Apple zoom! It's done so well
Test cases that testers must master
Win7 cannot be remote desktop problem
Programming principles
index查list 注入的是mysql 执行的是oracle
全栈开发实战|SSM框架整合开发