16 method to improve the efficiency of SQL

The method of optimizing efficiency sql statement project:
1) to select a smaller column
2) indexed more frequently used in the field WHERE
. 3) SELECT clause avoid the use '*'
4) to avoid the use of the index columns calculating, not in and <> etc. operation
5) only when needed. 1 line of data using the limit
. 6) to ensure a single table does not exceed 200W, timely partition table.
For queries slower statement can be analyzed specific implementation of the statement using explain.

------------------------------------------------------------------------------

1. Try not contain sub-queries where in;

Query with respect to time, so as not to be written: where to_char (dif_date, 'yyyy-mm-dd') = to_char ( '2007-07-01', 'yyyy-mm-dd');

2. in the filter can filter out the maximum number of conditions must be recorded at the end of the where clause;

FROM clause written in the final table (base table, driving table) will be processed first, the case includes a plurality of tables in the FROM clause, you must select the least number of records in the table as a base table. If more than three join queries, it would need to select the cross-table (intersection table) as a base table, cross table refers to the table that is referenced by other tables;

3. Using bind variables

4. Do not use in the WHERE OR

5. Alternatively IN with EXISTS, NOT IN alternatively by NOT EXISTS;

6. Avoid the use of columns in the index calculation: WHERE SAL * 12> 25000;

7.用IN来替代OR: WHERE LOC_ID=10 OR LOC_ID=15 OR LOC_ID=20

8. avoid the use of IS NULL and IS NOT NULL index columns;

9. always uses the first column of the index;

10. Alternatively UNION by UNION-ALL;

11. The index of the column to avoid changing the type: SELECT ... FROM EMP WHERE EMPNO = '123', since the implicit data type conversion, to_char (EMPNO) = '123', and therefore, will not use the index, is generally employed in a dynamic string patchwork SQL statements appear;

. '! =' 12 will not use the index;

13. Optimization GROUP BY;

14. Avoid LIKE with wildcard parameter, LIKE '4YE%' use of the index, but LIKE '% YE' does not use the index

15. A regular expression is difficult to avoid using, for example, select * from customer where zipcode like "98___", even if the zipcode have an index, in this case, still by way of sequential scanning. If the statement into select * from customer where zipcode> "98000", when performing a query will use the index to look up, obviously will greatly enhance the speed;

16. Try to clear the complete SQL statements, as little as possible so that the database work. For example, when writing a SELECT statement, you need to field queries made clear that the table name. Do not use SELECT * statement. When tissue SQL statement, as far as possible be organized in accordance with the habits of the database.

Guess you like

Origin www.cnblogs.com/Mr-Echo/p/12148047.html