Common SQL optimization interview questions

 

1. indexed in the table, to give priority to where.group by the use of the field.

2. query, be sure not to use select *, will return too many useless because the field will reduce the query efficiency. Should be used instead of specific fields *, only to return to the field.

3. Do not use the left and right sides in where conditions are like fuzzy% of queries, such as:

SELECT * FROM t_order WHERE customer LIKE '%zhang%'

This will lead to index the database engine to give a full table scan.

Optimization: make use of fuzzy query in the back field. as follows:

SELECT * FROM t_order WHERE customer LIKE 'zhang%'

4. Do not use in and not in, will result in a full table scan. as follows:

SELECT * FROM t_order WHERE id IN (2,3)

SELECT * FROM t_order1 WHERE customer IN (SELECT customer FROM t_order2)

optimization:

For continuous values between can not use in, as follows: the SELECT * the FROM t_order the WHERE the AND ID. 3 the BETWEEN 2

For sub-queries, can be used instead exists. As follows: the SELECT * the FROM t_order1 the WHERE EXISTS (the FROM t_order2 the WHERE t1.customer the SELECT * = t2.customer)

5. Try not to use or, will result in a full table scan. as follows:

SELECT * FROM t_order WHERE id = 1 OR id = 3

Optimization: You can use union instead of or. as follows:

SELECT * FROM t_order WHERE id = 1

UNION

SELECT * FROM t_order WHERE id = 3

6. Try not to carry out field operations in the where clause expression, it will also result in a full table scan. Such as:

select id FROM t_order where num/2=100

Should read:

select id FROM t_order where num=100*2

Conditions in judgment 7.where try not null value, null judgment will result in a full table scan. as follows:

SELECT * FROM t_order WHERE score IS NULL

optimization:

To add a default value to the field, to be the default value determination. Such as:

SELECT * FROM t_order WHERE score = 0

8. Do not try expression in conditions where the left side of the equal sign in. Operation function, leads to a full table scan. as follows:

SELECT * FROM t_order2 WHERE score/10 = 10

SELECT * FROM t_order2 WHERE SUBSTR(customer,1,5) = 'zhang'

optimization:

The expressions. Function operation to the right of the equal sign. as follows:

SELECT * FROM t_order2 WHERE score = 10*10

SELECT * FROM t_order2 WHERE customer LIKE 'zhang%'

9. Do not use the condition where 1 = 1

Sometimes, in the development process, in order to facilitate assembly query, we will add this condition, this will cause a full table scan. as follows:

SELECT * FROM t_order WHERE 1=1

optimization:

If the code is assembled with the sql, determined by the code, where no applied where, and there was added where

If mybatis, please use where the syntax mybatis.

10. The program should try to avoid large transaction operations, improve system concurrency.

11. The index number of a table is best not more than 6, if the index is too much, then you need to consider the construction of the columns that do not use the index if necessary.

Guess you like

Origin www.cnblogs.com/Nina-piaoye/p/11269431.html