PHP MySQL query optimization interview

MySQL query optimization

    A face questions

      Method for optimizing the efficiency of the SQL statement Please describe briefly the project, from those aspects, how to analyze the performance of SQL statements?

      Optimizing data access query process, optimize long difficult query, a particular type of query optimization

      SQL statement analysis method

      1, explain or analyze a single SQL statement desc 

      The Column: explain select * from news;

      2, slow query log record

      Slow query log analysis, do not directly open the slow query log for analysis, this is more a waste of time and effort, you can use pt-query-disgest analysis tool.

      3, use show profile

      For example: set profiling = 1; turn, are detected time consumed all statements executed on the server, stored in the temporary table

         show profiles; temporary display table

         show profile for query id statement singled to view specific execution time

      4、show  status

      show status will return to some of the counters, show global status view of all counts at the server level.

      Sometimes these counts, can guess a higher number of time consuming or cost of those operations.

      5、show processlist

      Observe whether the right number of threads in an abnormal state or features

      Query optimization process

        Too many queries to access data lead to performance degradation

        Determining whether the application data requires a lot more than in the search, too many rows or columns when possible

        Confirm whether the MySQL server analyze large amounts of unnecessary data lines

      Avoid using the following SQL statement

      Query unwanted records, limit use to solve

      Multi-table associated with the return of all columns, specify the column returns needed

      Always remove all of the columns, select * optimizer will not complete coverage optimization index scan

      Repeat the same data query, the query result cache can do next to get data from the cache

      Whether additional scanning records?

      Use explain the analysis, if the discovery query needs to scan large amounts of data, but returns only a few rows, you can go through the following optimization tips:

      Using an index to cover the scan, all the columns are placed with the index, so that the storage engine does not need to acquire a corresponding table rows back can return results

      And changing the structure of the database tables, database modification paradigm

      Rewrite the SQL statement, the optimizer can execute a query in a more optimal way

      Optimize long hard query

      A more complex queries or simple queries,

      MySQL internal memory can be scanned per second in millions of rows of data, by contrast, the response data to the client will slow more

      Use the query as little as possible is good, but sometimes a large query into multiple smaller queries are necessary

      Segmentation query

      A large number of small queries into the same query

      Than a one-time 10 million deletion of data deletion 10,000, suspended for a loss programs more server overhead

      Decomposition related inquiries

      A related statement can be broken down into multiple SQL to perform

      Make more efficient cache

      Perform a single query can reduce lock competition

      Correlative application layer may be more easily split the database

      Certain types of queries

      Optimization count () query

      count (*) * will ignore all of the columns, all direct statistical series, so do not use count (column name)

      MyISAM, there is no count where conditions (*) very fast, when there are conditions where, MyISAM count of statistics is not necessarily faster than other table engines

      Optimization

      You can use the query explain approximation with approximation alternative count (*)

      Increasing the summary table per query query summary tables, summary tables using the cache

      Optimized Association statement

      Column on the index to determine the ON or USING clause

      Ensure GROUP BY and ORDER BY columns in a table only, so will it be possible to use the index MYSQL

      Optimization Subqueries

      Use relational query alternative

      Optimization group by and distinct

      Both queries can be used to optimize the index, is the most effective optimization methods, related queries, efficient use of identity columns are grouped will be higher

      If the order does not require the use order by, performed group by by null, MySQL will not be sorted file

      with rollup super polymerization, application processing can be moved to

      Page Optimization limit

      limit the time offset greater low query efficiency

      Id can record a maximum of the last query, the next time the query directly from the id query

      Query optimization union

      Higher than the efficiency of the union all union

Guess you like

Origin www.cnblogs.com/dcrq/p/11081875.html