当前位置:网站首页>MySQL-01. Summary of database optimization and explain field knowledge in work

MySQL-01. Summary of database optimization and explain field knowledge in work

2022-06-23 10:24:00 cfcoolya

Database optimization - topic of conversation

Why optimize ?

  • The throughput bottleneck of the system often appears in the access speed of the database
  • There are more and more data in the database , Processing time will slow down accordingly
  • Optimization principle : Reduce resource use , Increase the reaction speed of the system

0.Redis cache

  • For this kind of information that does not change frequently and has a large amount of data , Add it to the cache , Improve the access efficiency of the system
  • Reducing database connections is an optimization tool , Some queries do not need to access the database , You can cache the server redis,elasticsearch Add cache , Reduce database connections .

1. Master slave copy , Read / write separation

  • The program uses more databases and updates less , Consider using when there are many queries , Reduce database pressure , Improve performance .
  • adopt MySQL Master slave copy ,curd go master The server , Inquiry slaver From the server , So it's reduced to just one MySQL Server pressure .

2. Database parameter configuration optimization

  • For example, the default maximum number of connections is 100, Even if SQL No matter how well the statement is optimized , No matter how high the hardware configuration is , Request exceeds 100 Have to wait , This is the result of unreasonable configuration .
  • https://cloud.tencent.com/developer/article/1582406

3. Optimize the design of database table structure

  • Select a reasonable field data type ( The database will eventually be written to disk , The length of the field also affects the size of the disk I/O operation ). such as : The age of a person is indicated by an unsigned unsigned tinyint that will do , It is not necessary to use integer Length of data type ; User's mobile number 11 Bit length , It is not necessary to 255 Length .
  • SQL Optimize , Use index
3.1 Indexes
3.1.1. In which case the index is used
  • Frequent fields as query criteria
  • Fields sorted in the query , Access speeds up
  • The fields counted or grouped in the query
  • The data column with foreign key must be indexed
3.1.2. Which case does not require indexing
  • Frequently updated fields
  • where Fields not used in conditions
  • There are too few records
  • Fields with duplicate data

4.MySQL Performance optimization artifact -Explain

4.1 brief introduction

MySQL Provides a Explain command , It's right select Statement analysis , And the output select Details of execution , For developers to optimize .

4.2explain Introduction to main formats

4.2.1 id

select Identifier , Query serial number , by sql The order in which statements are executed .

4.2.2 select_type
select_typemeaning
simple ordinary SELECT sentence ( barring UNION Operation or subquery operation )
primary Indicates that this query is the outermost query
unionUNION In operation , Inside the query SELECT
subquery First in subquery select
dependent unionUNION In operation , Inside the query SELECT( The inner layer of the SELECT Sentence and outer SELECT Statement has dependencies )
subquery First in subquery select
union resultUNION Results of operation ,id The value is usually NULL
4.2.3 type

Best to worst

type valuemeaning
const It's found by index once , At most one matching row in a single table ,primary key perhaps unique index Search for
eq_ref Uniqueness index , In a multi table connection, the connection column of the driven table has primary key perhaps unique index Search for
ref And eq_ref similar , But not using primary key perhaps unique index, It's a normal index . It can also be on a single table non-unique Index search
range Range query in single table index , Use the index to query out some row data in a single table .ref The column will become null.
index Than all Better
all Full table scan
4.2.4 key
  • possible_key Index list , Not necessarily used in practice
  • key Actual index used
  • key_len Maximum possible length of index field
4.2.5 rows
  • According to table statistics and index selection , Roughly estimate the number of rows to read to find the required record
4.2.6 extra

Contains additional information that is not suitable for display in other columns but is important

  • using filesort Access is not in the order of indexes in the table
  • using temporary Increased the burden of the database , Temporary tables are used to store intermediate result sets , Apply to group by,distinct, or order by Columns for different tables .
  • using index well , Override index used
原网站

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