当前位置:网站首页>[MySQL performance optimization] - optimize query

[MySQL performance optimization] - optimize query

2022-06-21 08:12:00 _ Superdream

Preface

         Optimize MySQL Database is a necessary skill for database administrator , Through different Optimization method To improve MySQL The purpose of database performance .

        MySQL Database when the number of users and data is very small , It is difficult for us to judge MySQL Database performance . Only when the number of users and the amount of data increase ,MySQL How can the performance of . Inquire about Is the most frequent database operation , Improve Query speed Can effectively improve MySQL Database performance .


Optimized query

SHOW STATUS Statement query MySQL Database performance

Analyze query statements

The impact of index on query speed

LIKE Optimize index queries

Use multi column index

Use keywords or


SHOW STATUS Statement query MySQL Database performance

We can use SHOW STATUS Statement query MySQL Database performance .

Grammar format :

SHOW STATUS LIKE 'VALUE'

VALUE The parameters are as follows

  1. Connection: Connect MySQL The number of servers .
  2. Uptime:MySQL The online time of the server .
  3. Slow_queries: Slow query times .
  4. Com_select: Number of query operations .
  5. Com_insert: The number of insert operations .
  6. Com_delete: The number of delete operations .

Through these parameters, we can analyze MySQL Database performance . Then according to the analysis results , Conduct Corresponding performance optimization .



Analyze query statements

have access to EXPLAIN Statement and DESCRIBE Statement to analyze the query statement .

Grammar format :

#EXPLAIN  Grammar format 
EXPLAIN  SELECT  sentence ;

#DESCRIBE  Grammar format 
DESCRIBE SELECT  sentence ;

  Next, we're going to talk about EXPLAIN and DESCRIBE To introduce

Use EXPLAIN Statement analysis query statement

  The meaning of each field :

id: Indicates that throughout the query select The location of .

table: Store the table name queried .

type: Connection type , Many values are stored in this column , Range from const To All.

possible_keys: Point out that in order to improve the search speed , stay MySQL Indexes that can be used in .

key: Indicate the keys actually used .

rows: Pointed out that MySQL You need to return the number of rows verified by the query result in the corresponding table , To get the total number of rows ,MySQL The entire query must be scanned , Multiply by the row value of each table .

Extra: Contains some additional information , Design MySQL How to handle queries .


Use DESCRIBE Statement analysis query statement

 DESCRIBE The use of statements is the same as EXPLAIN The grammar is the same , The results of these two analyses are also roughly the same .


The impact of index on query speed

We use the index in the query process , It can improve the efficiency of database query , Reduce the number of queries . Next, let's use an example to compare the difference in query speed between using an index and not using an index .


We're looking through student Table to analyze the use and non use of indexes .

 


Queries without index

explain select * from student where stuname = 'zhangsan';

  branch Analysis : Without using an index , We found that in the original table Three pieces of data They were all checked . And then someone will say , What is the impact of only three pieces of data ? Of course , When the amount of data is small , Query efficiency won't make much difference . If there is a large amount of data in the database , Like tens of millions of them , When we query, these tens of millions of data will be retrieved , Users traverse tens of millions of pieces of data in order to query one piece of data , It will consume a lot of time and resources .


Query using index

Let's give it first stuname Field plus index

CREATE INDEX index_stuname ON student(stuname);

  After adding the index, we are querying

explain select * from student where stuname = 'zhangsan';

  branch Analysis : It can be seen from the above results that , The number of rows accessed due to the index created is determined by 3 Reduced to 1, therefore , In the query operation , Using indexes will not only Optimize query efficiency , Will Reduce server overhead .


LIKE Optimize index queries

When the first character is “%” when , Index will not be used , If “%” The position in the matching string is not the first position , Then the index will be Normal use .

explain select * from student where stuname like 'zh%';

 


Use multi column index

Multi column index creates an index on multiple fields of a table , Only when one of these fields is used in the query criteria , The index will be used normally .

Create multi column indexes

CREATE INDEX index_student ON student(stuname,stuage);

Be careful : When we query, if we apply stuage Field , The index cannot be used normally , You have to use First field stuname when , The index takes effect normally . This must be remembered !



Use keywords or

        stay MySQL in , Query statements only contain keywords or when , It is required that the two fields of the query must Same as index , If the search criteria , One field is not indexed , The index will not be applied to the query , Indexes Will not enter into force , Query efficiency will not be improved .


If this article is helpful to my friends , I hope you can give me some praise and support ~      Thank you very much. ~


原网站

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