MySQL (three) - sql optimization

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.

Analyzing avoid 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 where NUM IS null T
may be provided on a default value of 0 num, to ensure that the table num column value is not null, then this query:
SELECT ID WHERE num from T = 0


Avoid the equivalent of judgment

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

Avoid using or logic

Should be avoided or used to join condition 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 = NUM = 10 or 20 is
  could this query:
  SELECT ID from where T 10 = NUM
  Union All
  SELECT from ID = 20 is T WHERE NUM

Caution in and not in logic

should be used with caution in and not in, otherwise it will lead to a full table scan, such as:
  the SELECT NUM in the above mentioned id from the WHERE T1 (T2 the WHERE from the SELECT the above mentioned id the above mentioned id> 10)
  In this case the outer query will be a full table scan, do not use indexes. You may be modified:
  SELECT ID from T1, (T1 WHERE ID SELECT ID from> 10) = T2 WHERE t1.id t2.id
  case index is used, can significantly improve the efficiency of the query.

Note fuzzy query

The following query will result in a full table scan:
  SELECT ID from T WHERE name like '%% ABC'
  fuzzy inquiry is necessary if conditions, can select id from t where name like ' abc%' to fuzzy inquiry, this time the index will be used. If the first match logic is necessary, it is recommended to use full-text search engine (Elastic search, Lucene, Solr, etc.).

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:
  SELECT ID WHERE NUM from T / 2 = 100
  should read:
  SELECT ID from T WHERE NUM = 100 * 2

A function of field operating conditions to avoid the query

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 WHERE T from the substring (name, l, 3) = 'ABC' - to name id abc beginning
  should read:
  SELECT ID from T WHERE name like '% ABC'

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.

Use 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.

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:
  the SELECT col1, col2 INTO #t from the WHERE t 1 = 0
  This code does not return any result sets, but consumes system resources and should be replaced by this :
  the Create the Table #t (...)

exists

Instead of use exists in many cases is a good choice:
  SELECT NUM NUM from A in WHERE (SELECT NUM from B)
  was replaced with the following statement:
  SELECT NUM exists from A WHERE (SELECT. 1 from B WHERE NUM = a.num)

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.

Select the type of form fields

Make use of numeric fields, numeric fields containing information if only so as not to design for the character, which 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.
  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.

The 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.

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.
  * From Table limit 1000000 SELECT, 10
  SELECT * from Table ID in WHERE (SELECT from Table limit 100000 PK, 10)

Guess you like

Origin www.cnblogs.com/RobertLionLin/p/11411627.html