mysql slow query optimization

First, what is the slow queries 

MySQL slow query, full name is slow query log is a log provided by MySQL, the response time exceeds the threshold used to record statements in MySQL. Specific environment, running for more than long_query_timethe value of the SQL statement, it will be logged to the slow query log. Simply put, is running for a long time sql statement .

Second, how to optimize

  1, the situation did not work index of
        1.1 LIKE keyword query in the query using the LIKE keyword query, if the first character matched string is "%", the index will not work. Only "%" not in the first position index will work.

        1.2 multi-column index query MySQL can create indexes on multiple fields. An index can comprise up to 16 fields. For multi-column index, only the query using these fields in the first field, the index will be used.

 

   2, optimize the structure of the database
            reasonable database structure not only allows databases take up less disk space, and can make queries faster. Design of the database structure, you need to consider the data redundancy, speed query and update the data type of the field is reasonable and many other content.

    2.1 The table number field into a plurality of tables 

            For more of the fields of the table, frequency of use is low some fields, these fields can be separated out to form a new table. Because when the data table is large, due to the presence of low frequency of use field slowed.

    2.2 Increase intermediate table

            For tables require frequent union query, you can create an intermediate table to improve query performance. By establishing middle of the table, inserting data requires frequent joint inquiry into the middle of the table, and then the original query to a query on the joint middle of the table, in order to improve query performance.

   3, associated with the query decomposition

      A large query into multiple small queries are necessary.

      Many high performance applications will decompose on the relational query is a query may be a single table, and associating the query result in the application, many scenes would be more efficient for each table.

 

 

  4, inner join the Join alternate left / right the Join
       Inner the Join the two tables, such as: a inner join b, but the order is actually executed with no half-dime order of writing, the final implementation may also be connected to b a, the order is not fixed dead. If the conditions on the indexed field, the index can also be used on. Let mysql judge for yourself (query optimizer). Join orders and specific situations using the index table, mysql query optimizer cost assessment will be made in each case, the final choice as the best of the implementation plan.

Reference: https: //blog.csdn.net/qq_35571554/article/details/82800463



 

Guess you like

Origin www.cnblogs.com/51python/p/11483039.html