Slow query SQL optimization

Slow query: It is a query with slow query response. What is the standard of slow query? There is no fixed standard for the definition of a slow query. Generally, a query with a query time exceeding 1 second is called a slow query.

 

Two ways to optimize SQL:

  1. Optimize query SQL and table structure
  2. Perform sub-database and sub-table (DB intervention may be required when sub-database)

Both of the above two methods are levels that programmers can subjectively intervene in.

According to the above two situations, I will explain it separately (personal opinion)

1. Optimize query SQL and table structure

  • Reasonable use of indexes, adding indexes to SQL conditions with frequent queries in the code
  • It is best to determine the query fields in the query SQL, that is, use less *, and it is best to determine the fields that need to be queried
  • Check whether there are large field queries in the query field, and use other methods instead to reduce large field queries

2. Carry out sub-database sub-table

  • The sub-database represents the division of the database according to the business or applicable data range. For example, it is divided into different databases according to different business channels, and different micro-services are used for data processing; while the applicable data range may divide the original large database by transaction time. To divide (year), special division for the range that requires high query efficiency.
  • The sub-table represents predicting the amount of data to be accessed in later transactions when creating a business table, and judging whether it is necessary to perform partition table operations when building a table

The author believes that the core of sub-database and sub-table is to reduce the data volume of single database or single table, and reduce the pressure of data query of single database and single table. All will definitely overlap during usage and interviews.

Knowledge supplement (partition table statement)

MYSQL:
#查看数据库是否支持分区
show variables like '%partition%';

MYSQL official document , Document here

ORACLE official document

PS: If you have different opinions or feel that the author is wrong, please let me know

Guess you like

Origin blog.csdn.net/weixin_42505381/article/details/128385239