MySQL SQL statements in common optimization strategy

1. Avoid full table scan
query optimization, should try to avoid full table scan, you should first consider indexing by the column involved in where and order.

2. Analyzing avoid null values
should be avoided to a null value is determined fields in the where clause, will cause the engine to give up using the index and a full table scan, such as:

select id from t where num is null

You can set the default value of 0 on the num, num column in the table to ensure that there is no null value, then this query:

select id from t where num=0

3. Analyzing avoid equivalent
should be avoided in the where clause! = Or <> operator, otherwise the engine will give up using the index and a full table scan.

4. Avoid using logic or
should be avoided in the where clause to connect or condition will cause the engine to give up using the index and a full table scan, such as:

select id from t where num=10 or num=20

Queries can do:

select id from t where num=10

union all

select id from t where num=20

5. caution in and not in logic
in should be used with caution and not in, otherwise it will lead to a full table scan, such as:

select id from t1 where num in(select id from t2 where id > 10)

In this case the outer query will be a full table scan, do not use indexes. Be amended as follows:

select id from t1,(select id from t1 where id > 10)t2 where t1.id = t2.id

At this index is used, it can significantly improve query efficiency.

6. Note fuzzy query
following query will also lead to a full table scan:

select id from t where name like '%abc%'

Fuzzy queries if the necessary conditions are, may be used select id from t where name like 'abc%' to fuzzy query, the index will be used at this time. If the first match logic is necessary, it is recommended to use full-text search engine (Elastic search, Lucene, Solr, etc.).

7. Avoid calculated fields in the query
should be avoided for operating fields in the where clause expression, which will cause the engine to give up using the index and full table scan. Such as:

select id from t where num/2=100

Should read:

select id from t where num=100*2

8. avoid the query function of the operation fields
should be avoided to a function operation in the fields where clause that will cause the engine to give up using the index and full table scan. Such as:

select id from t where substring (name, 1,3) = 'abc' - name beginning with the id abc

Should read:

select id from t where name like 'abc%'

9.WHERE clause "=" left Precautions
Do not functions, arithmetic operations, or other expressions in the where clause "=" left, or the system may not work properly indexed.

10. The combination index used
in the field as a condition of using the index, if the index is a composite index, you must use the index as a first field of the condition to ensure the system uses the index, otherwise the index will not be used and should as far as possible so that field order is consistent with the index order.

11. Do not define queries without objection,
do not write the query does not make sense, such as the need to create an empty table structure:

select col1,col2 into #t from t where 1=0

This code does not return any result sets, but consumes system resources and should be replaced by this:

create table #t(...)

12.exists
a lot of time instead of in use exists is a good choice:

select num from a where num in(select num from b)

Replace with the following statement:

select num from a where exists(select 1 from b where num=a.num)

13. The index may fail
and not all indexes are valid query, SQL query optimization is performed according to data in the table, when the index lists a large number of duplicate data, SQL queries may not go to use the index, such as a table has a field sex, male, female almost each half, even if the index built on sex have no effect on the query efficiency.

Table 14. Select field type
make use of numeric fields, if only the fields containing numerical information is not possible for the character design, which reduces the performance of the connections and queries, and increases storage costs. This is because the engine when processing queries and connections one by comparing each character in the string, and for numeric comparison purposes only once is enough. Possible use varchar instead of char, because first of all variable-length fields small storage space, you can save storage space, followed by the query, in a relatively small field of search efficiency is clearly higher.

15. The fields in the query syntax
anywhere Do not use the SELECT from t, with a specific list of fields instead of " ," Do not return any of the fields with less than.

16. The index is independent optimization
does not use *, try not to use union, union all keywords such, try not to use or keywords, try to use the equivalent of judgment. Table join recommends no more than five. If more than 5, the design of the table is considered. (Internet applications) using the connection table outreach than inline. An outer base connected to data exists. Such as: A left join B, the base data is A. A inner join B, no basic data, using the Cartesian product to complete the whole connection, the connection according to the connection conditions to obtain a result set.

When the order of large data tables do paging query, if the number of pages is too large, the use of sub-query with the completion of paging logic.

Select * from table limit 1000000, 10

Select * from table where id in (select pk from table limit100000, 10)

Guess you like

Origin blog.51cto.com/14551317/2440300