Database optimization strategy (draft)

Database query performance optimization strategy

Query Optimization

Explain analyze query using Statement

Explain to analyze SELECT query, developers can optimize query by analyzing Explain the results.

By analyzing the query, you can understand the implementation of the query, the query performed to identify bottlenecks to optimize queries.

Use index query

MySql improve performance in a most effective way is to design a reasonable data table index.

Index provides a method for efficient access to data and speed up queries.

If the query does not use the index, then the query will scan all the records in the table. When the large amount of data, so that the query can be slow.

Use the index query, the query can be indexed according to quickly locate the record to be queried, thus reducing the number of records query, the query as to improve the speed.

Some special cases (when using the indexed field query, the index does not work)

  • Like using a keyword, if the first character of the match string is "%", the index does not work, if the first keyword is not "%", then the index will work.
  • MySQL can create multiple fields index, an index may include 16 fields, only the query used in these fields of the first field, the index will be used.
  • When the query keywords only OR, OR and the left and right columns are the index, to work.

Optimization Subqueries

Although the sub-queries can make queries more flexible, but the efficiency is not high, because MySQL needs to create a temporary table to the inner query results statement.

You can use join queries instead of sub-queries, join queries do not need to create a temporary table, its speed is faster than the subquery.

Optimizing Data Access

1. Reduce the amount of data requested

  • Returns only the necessary columns, select * best not to use this syntax;
  • Returns only the necessary rows using limit the number of statements restricting access to data;
  • Data caching repetitive queries: using the cache can be avoided in a database query, especially when the data is often repeated queries, caching query performance improvements brought about, it will be very obvious.

2. The number of scan lines reduces the server

The most effective way is: use an index to cover the query;


Reconstruction of query

1. Cut a large sub-query

A large one-time query if implemented, could once locked a lot of data, occupies the entire transaction log, run out of system resources, blocked a lot of small but important inquiry.

2. break down large join queries

A large query into connection for a single-table query for each table, and associates the application.

Benefits of doing so are:

  • Let caching more efficient. For join queries, if one table is changed, then the entire query cache can not be used while the decomposed multiple queries, even if the query in which a table is changed, then the query cache for other tables still can use.
  • Decomposing the query into a single table, these single-table queries cached results more likely to be used to query another, thereby reducing the redundancy record query.
  • Reducing lock contention.
  • In the application layer connection, it can more easily split the database, making it easier to achieve high performance and scalability.

Optimized database structure

Table 1. The field number into a plurality of tables

For more table fields, some fields if the frequency is low, these fields can be separated out to form a new table.

When the data table is large, due to the presence of low frequency of use field slowed.

2. Increase the middle of the table

For tables often need a union query, you can create an intermediate table to improve query performance.

3. Insert the optimal speed recorded

When recording is inserted, the insertion speed mainly affect the index, the only check, insert a number of records, etc. under these circumstances may be optimized separately.

Optimizing the MySQL Server

1. Optimize server hardware

For performance bottlenecks, improve the hardware configuration, you can increase the speed of query and update the database.

  • Larger memory configuration.
  • With the high speed disk system to reduce the waiting time to read the disk.
  • Allocate disk IO.
  • Multi-processor configuration, MySQL database is multi-threaded, multi-processor can execute multiple threads simultaneously.

2. Optimize MySQL parameters

MySQL optimization parameters can improve resource utilization, thereby improving server performance.

Guess you like

Origin www.cnblogs.com/CherishTheYouth/p/CherishTheYouth_20190921.html