当前位置:网站首页>How to optimize MySQL?

How to optimize MySQL?

2022-06-13 10:24:00 CodeMartain

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

 Insert picture description here
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)

 Insert picture description here
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);
原网站

版权声明
本文为[CodeMartain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131005446376.html