sql optimization points

1 to avoid full table scan

Query optimization, should try to avoid full table scan, should first consider indexing by the column involved in where and order.
Commercial development, mandatory, not a full table scan. Try to type a query raised above ref level must be above the index level.

const> eq_ref> ref> the Range> index> ALL
MySQL EXPLAIN Type Description of the type

2 is determined to avoid a null value

Fields should be avoided to a null value is determined 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
. 1
may be provided on a default value of 0 num, ensuring table the num column has no null value, then this query:

T WHERE ID from NUM SELECT = 0
. 1
. 3 equivalent avoid Analyzing

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

4 or avoid the use of logic

Should avoid or use to connect to the conditions in the where clause, will cause the engine to give up using the index and full table scan, such as:

T WHERE ID from NUM SELECT = NUM = 10 or 20 is
. 1
may query this way:

--union all not deduplication
--union deduplication
SELECT ID NUM WHERE T = 10 from
Union All
SELECT WHERE T from ID = 20 is NUM
. 1
2
. 3
. 4
. 5
Note that the calculation result of two sets, the amount of data is preferably not over a thousand units.

union - and eliminate duplicate set
union all - and set without eliminating duplicates
. 1
2
. 5 and not in the logic used with caution in

should be used with caution in and not in, otherwise it will lead to a full table scan, such as:
query t1 to t2 in the table num id id more than 10 people

NUM ID from T1 WHERE SELECT in (SELECT ID from T2 WHERE ID> 10)
. 1
At this full table scan the outer query, the index is not used. Be amended as follows:
The query is split into multiple sub-table query

ID from T1 SELECT, (SELECT ID from T2 WHERE ID> 10) = T2 WHERE t1.num t2.id
. 1
At this time the index is used, can significantly improve the efficiency of the query.

6 Note fuzzy query

The following query will also lead to a full table scan:

from T WHERE name ID SELECT like '%% ABC'
. 1
fuzzy inquiry is necessary if the conditions can 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, Solr, Lucene (both underlying implementation), etc.).

7 Avoid calculated fields in the query

Should be avoided fields operations expression in the where clause, which would cause the engine to give up using the index and full table scan. Such as:

T WHERE ID from NUM SELECT / 2 = 100 `
. 1
should read:

T WHERE ID from NUM SELECT = 100 * 2
. 1
. 8 to avoid fields in query operation function

Should be avoided fields operations function in the where clause, which would 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 id abc beginning in
1
should read:

from T WHERE name ID SELECT like 'ABC%'
. 1
. 9 the 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 using a composite index

In the field as a condition of using the index, if the index is a composite index, you must use the index to the first field as the conditions to ensure the system uses the index, otherwise the index will not be used, and should as far as possible let the field order is consistent with the order index.

11 Do not define meaningless query

Do not write the query does not make sense, such as the need to create an empty table structure:

col1 SELECT, WHERE col2 INTO #t from T = 0. 1
. 1
such code does not return any result sets, but they consume system resources, this should be replaced:

create table #t(...)
1
12 exists

Not all cases can be used: a good choice in many cases instead of using exists.

A WHERE NUM NUM from SELECT in (B SELECT from NUM)
. 1
is replaced with the following statement:

A NUM from WHERE EXISTS SELECT (SELECT. 1 from B WHERE NUM = a.num)
. 1
13 is the index may be invalid

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 built on sex query efficiency index also no effect.

14 table field type selection

Make use of numeric fields, numeric fields containing information if only so as not to design for the character, because it will reduce the performance of queries and connections, and will increase the storage overhead. 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.

Try to 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 fields in the query syntax

Do not use anywhere select * from t, with a specific list of fields instead of "*", do not return any of the fields with less than.

16 Index-independent optimization

* Do 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)

Table connection than the use of inline outreach.

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 limit 100000, 10)

Guess you like

Origin www.cnblogs.com/yachao1120/p/12152410.html