Excerpt sql optimization

Sql query optimization statement? DB index usage scenarios?

1, in the index table, priority where, group by using the field.

2, try to avoid using select *, return unwanted field will reduce the query efficiency. As follows:
the SELECT * the FROM T
optimization mode: * Instead of using specific field, only to return to the field.

3, try to avoid using in and not in, can cause the database engine to give the index a full table scan. As follows:
the SELECT * the FROM (2,3) the WHERE T ID the IN
the SELECT * the FROM T1 the IN the WHERE username (username the SELECT the FROM T2)
Optimization: If the continuous values may be replaced with between. As follows:
the SELECT * the FROM T 2 the WHERE the BETWEEN the AND ID. 3
If a subquery, can be used instead exists. As follows:
the SELECT * the FROM T1 the WHERE EXISTS (the WHERE t1.username the SELECT * T2 = t2.username the FROM)

4, try to avoid using or, cause the database engine to give the index a full table scan. As follows:
the SELECT * the FROM T = the WHERE ID ID = OR. 3. 1
Optimization: Can be replaced with the union or. As follows:
the SELECT * the FROM T = the WHERE ID. 1
the UNION
the SELECT * the FROM T = the WHERE ID. 3
(the PS:. If the sides or the field is the same as example of such seemingly similar efficiency in two ways, even if the union is an index scan, or scan the whole table)

5, try to avoid fuzzy query at the beginning of the field, will lead to index the database engine to give a full table scan. As follows:
the SELECT * the WHERE username the LIKE the FROM T '%% Li'
Optimization mode: to make use of fuzzy search field later. As follows:
the SELECT * the WHERE username the LIKE the FROM T '% Li'

. 6, to avoid the determination of a null value, the database will cause the engine to give a full table scan index. As follows:
the SELECT * Score the FROM T the WHERE the IS NULL
optimization mode: default value of 0 may be added to the field, a value of 0 is determined. As follows:
the SELECT * Score the FROM T = 0 the WHERE

7, try to avoid conditions where the expression on the left side of the equal sign, the function operation, will lead to index the database engine to give a full table scan. As follows:
the SELECT * Score the FROM T2 the WHERE / = 10. 9
the SELECT * the FROM T2 the WHERE the SUBSTR (username, 1,2) = 'Li'
Optimization mode: expressions, functions, operations may be moved to the right of the equal sign. As follows:
the SELECT * Score the WHERE the FROM T2 = 10. 9 *
the SELECT * the FROM T2 the WHERE username the LIKE '% Li'

8, when the amount of data, to avoid the use of conditions where 1 = 1. Usually in order to facilitate the assembly query, we will use the default condition, the index database engine will give up a full table scan. As follows:
the SELECT * the FROM T = the WHERE. 1. 1
Optimization mode: when the determination in code assembly sql, where no added where, where there is added and.

Guess you like

Origin www.cnblogs.com/yanghuiping/p/11976473.html