Mysql SQL performance optimization

Mysql optimization is to find out the bottleneck of the system and improve the overall performance of the MySQL database. Can provide a larger load of services.
The principle is to reduce system bottlenecks, reduce resource occupation, and increase system response speed. For example, improve the read and write speed of I/O, improve the load capacity of mysql under high load conditions by optimizing the operating system scheduling strategy, optimize the table structure, indexes, query statements, etc. to make the query response faster.
MySQL has some parameters for querying database performance, show status
show connections //The number of connections to the server,
show update //The online time of the mysql server
show slow_queries //The number of slow queries
show com_select //The number of query operations
show com_insert //Insert The number of operations
show com_update //The number of update operations show com_delete //The number of
delete operations
There is an optimizer module in mysql that is specially responsible for optimizing the select statement. The optimizer module is the Query Optimizer.
I am used to using Explan and profiling
insert statement optimization:
1. Insert many rows of data into mysql. If it is multiple values ​​at a time, you can use the following format, which will greatly shorten the connection and closure between the client and the database server.
insert into table values (id,name),(id,name2);

When loading a table from a file, loading data using load data infile is often at least 20 times more efficient than using many insert statements
. 2. For MyISAM-type tables, if many rows are inserted by different clients, the speed can be improved by using the insert delayed statement , the server will return OK to the client after the operation submits the data to the server. This is not to insert the data into the list immediately, but to store it in the memory and wait for the queue until the mysql server is free to insert it. At this time, the data is not actually written to the disk. The advantage of this is to improve the speed of inserting data, not The good thing is that if the system crashes you have no way of finding lost data.
3. Tables can usually be locked to speed up inserting data,
lock tables test write
insert into test values(1,'22',2,'22' );
unlock tables;

If the table is not locked, the index buffer area will be written to the disk after each insert statement is executed, and the index buffer area will only be written to the disk once after adding the lock.
4. The main factors that affect the insertion speed are indexes, uniqueness verification, and the number of records inserted at one time. We can optimize them separately.
a. Disable the index When inserting records into
a non-empty table, mysql will index the inserted records according to the index of the table. If a large amount of data is inserted, the indexing will reduce the speed of inserting records, so disable the index before inserting, and enable the index after inserting.
alter table test distable keys;
//insert data
alter table test enable keys;

b. Disabling the uniqueness check
Because some verifications will slow down the insertion speed, in order to reduce the query speed in this case, you can disable the uniqueness check before inserting the record and wait until the record is inserted.
set unique_checks=0;
set unique_checks=1;
load data infile '/home/mysql/data.txt' into table test;
set unique_checks=0;//Do not verify uniqueness
load data infile '/home/mysql/data.txt' into table test;

c. Use batch insertion
, just like the above one insert multiple data
d. Use the load command to import in batches
alert table test disable keys;
load data infile ‘/home/mysql/data.txt’ into table test;
alert table test enable keys;

The above is the method of using the MyISAM engine, which
corresponds to use of Innodb
a, prohibit unique checks set unique_checks=0
b, prohibit foreign key checks set foreign_key_checks=0
c, prohibit automatic submission set autocommit=0


To optimize the ORDER BY statement
1, an index can usually be used to optimize the order by statement. order by+limit; where +order by+limit
select id,name from test order by name limit 0,10
select id,name from test where name='Janle' order by name limit 0,10;

Usually do not use expressions or functions for where and order by, e.g.
select * from test order by year(birthday) limit 0,20;

Indexes should not be used in the following situations:
1. The fields of order by use a mixture of asc and desc, such as
select * from test order by a asc,b desc ;

2. The fields used in the where clause are inconsistent with the order by, such as:
select * from test where b=1 order by a;

3" Use order by to sort different keywords, such as:
select * from test order by a;

2. When using the group by statement, mysql will automatically sort the matching results. By scanning the temporary table and creating a temporary table, all the behaviors of each group in the table are continuous. Then use a temp table to find the group and apply the cumulative row count. In some cases, mysql can be accessed through indexes instead of temporary tables. Sorting can be disabled by specifying group by null, saving losses.
select count(*) from test group by id order by null;

Optimizing nested queries
1. Although sub-queries are convenient and flexible, mysql needs to create a temporary table for the inner query statement, and then the outer query statement queries records from the temporary table. After the query is complete, these temporary tables are revoked. Therefore, the speed will have an impact. If the amount of data queried is relatively large, this impact will increase accordingly. A join query can be used instead of a subquery. The join query does not need to create a temporary table, and is faster than a subquery. If an index is used, the performance will be better. It can be seen from the execution plan that the join query scan record has been greatly improved after join, and the performance has been improved to a certain extent.
Optimizing OR Conditions
For subqueries using OR conditional statements, if you want to use an index, each conditional column between ORs must use an index. If not, you can consider adding an index.
explan select * from test where col=1 and col1=2; //union is not used
create index index_col on(test.col);
create index index_col1 on(test.col1);
explan select * from test where col=1 and col1=2;//You can see that the union operation is done

After adding the index, the union operation is performed after querying each field to the result.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676277&siteId=291194637