Mysql data query optimization

  1. How to Check Statement Execution Speed

  • View Mysql settings about profile

  • open profiling


  • View the records of profiles


  • Execute SQL to check its execution; then check the profiles record, you can see the time-consuming execution of the SQL


      2. How to view the statement execution plan

use explain



In the Extra column, it lists whether the SQL uses Index during execution. You can judge whether the SQL Query is efficient based on this column.

       3. Increase the index to improve the query efficiency

  • To optimize the query, you should try to avoid full table scan. First, you should consider creating indexes on the columns involved in where and order by.

Example: 0.04s when not indexing on timestamp


After indexing on timestamp, the query time is significantly shortened


  • You should try to avoid the null value judgment of the field in the where clause, otherwise the engine will give up the use of the index and perform a full table scan, such as: 
    select id from t where num is null 
    You can set the default value of 0 on num to ensure that the table num column has no null value, then query like this: 
    select id from t where num=0
  • In and not in should also be used with caution, otherwise it will result in a full table scan, such as: 
    select id from t where num in(1,2,3) 
    For consecutive values, if you can use between, do not use in: 
    select id from t where num between 1 and 3
  • Using parameters in the where clause also results in a full table scan. Because SQL resolves local variables only at runtime, the optimizer cannot defer the choice of an access plan to runtime; it must choose it at compile time. However, if the access plan is built at compile time, the value of the variable is unknown and cannot be used as an input for index selection. For example, the following statement will perform a full table scan: 
    select id from t where num=@num 
    can be changed to force the query to use the index: 
    select id from t with(index(index name)) where num=@num
  • The more indexes the better, the index can certainly improve the efficiency of the corresponding select, but it also reduces the efficiency of insert and update, because the index may be rebuilt during insert or update, so how to build an index needs to be carefully considered, depending on the specific Depends. The number of indexes in a table should not exceed 6. If there are too many indexes, you should consider whether it is necessary to build indexes on some infrequently used columns.
  • Try to use numeric fields as much as possible, and try not to design character fields for fields that only contain numeric information, which will reduce query and connection performance and increase storage overhead. This is because the engine compares each character of the string one by one when processing queries and joins, whereas only one comparison is required for numbers.
  • Do not use select * from t anywhere, replace "*" with a list of specific fields, and do not return any fields that are not used.
  • The following query will also result in a full table scan: (the percent sign cannot be prepended) 
    select id from t where name like '%c%' 
    To improve efficiency, consider full-text search.

4. Add index statement

/*Add PRIMARY KEY (primary key index) */

mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )

/*Add UNIQUE (unique index) */

mysql>ALTER TABLE `table_name` ADD UNIQUE ( `column` )

/* Add INDEX (normal index) */

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` )

/*Add FULLTEXT (full text index) */

mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)

/* add multi-column index */

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895273&siteId=291194637