A small note on SQL optimization rules

Source: Internet
Author: User
Tags joins mathematical functions

SQL Optimization Tips

1. Select the most efficient table name order (valid only in the rule-based optimizer):

The Oracle parser processes the table names in the FROM clause in a right-to-left order, and the FROM clause is written in the final table (the underlying table, driving tables) will be processed first, and in the case where the FROM clause contains more than one table, you must select the table with the fewest number of record bars as the underlying table. If you have more than 3 tables connected to the query, you need to select the crosstab (intersection table) as the underlying table, which refers to the table that is referenced by the other table.

Connection order in the 2.where clause:

Oracle uses a bottom-up sequential parsing where clause, according to which the connection between tables must be written before other where conditions, and those that can filter out the maximum number of records must be written at the end of the WHERE clause.

Avoid using ' * ' in the 3.SELECT clause:

During the parsing process, Oracle translates ' * ' into all column names, which is done by querying the data dictionary, which means more time is spent.

4. Reduce the number of Access databases:

Oracle has done a lot of work internally: parsing SQL statements, estimating index utilization, binding variables, reading blocks, etc.

5. Reset the ArraySize parameter in Sql*plus, sql*forms and pro*c to increase the amount of data retrieved per database access, with a recommended value of 200

6. Use the Decode function to reduce processing time:

Use the Decode function to avoid duplicate scans of the same record or duplicate connections to the same table.

7. Delete duplicate records:

The most efficient method of deleting duplicate records (because of the use of rowID) Example:

Delete from emp e where

E.rowid > (select min (x.rowid)

From EMP x where x.emp_no=e.emp_no);

8. Integrated simple, no associated database access:

If you have a few simple database query statements, you can integrate them into a single query (even if they are not related)

9. Replace Delete with truncate:

When you delete a record in a table, in general, the rollback segment (rollback segments) is used to hold information that can be recovered.

If you do not have a COMMIT transaction, Oracle restores the data to the state it was before it was deleted (exactly before the delete command was executed) and when the truncate is applied, the rollback segment no longer holds any recoverable information. When the command runs, The data cannot be restored. So very few resources are invoked and execution times are short. (Translator Press: Truncate only in the Delete full table applies, truncate is DDL is not DML).

10. Use commit as much as possible:

Whenever possible, commit is used as much as possible in the program, so that the performance of the program is improved,

Requirements are also reduced by the resources freed by commit:

Resources Freed by Commit:

A. Information for recovering data on a rollback segment.

B. Locks acquired by program statements

C. Redo space in the log buffer

D. Oracle manages internal spending on 3 of these resources

11. Replace the HAVING clause with a WHERE clause: to avoid having a HAVING clause, the having will only filter the result set after retrieving all records. This processing requires sorting, totals, and so on. If you can limit the number of records through the WHERE clause, you can reduce this overhead. (Non-Oracle) on, where, have the three clauses that can be added conditionally, on is the first execution, where the second, having the last, because on is the non-qualifying records filtered before the statistics, it can reduce the intermediate operation to process the data, It should be said that the speed is the fastest, where should also be faster than having to, because it filters the data before the sum, in two table joins only use on, so in a table, the left where and have compared. In the case of this single-table query statistics, if the conditions to be filtered do not involve the fields to be calculated, then they will be the same result, but where you can use the Rushmore technology, and have not, at the speed of the latter slow if you want to relate to the calculated field, it means that before the calculation, The value of this field is indeterminate, according to the workflow of the previous write, where the action time is done before the calculation, and having is calculated after the function, so in this case, the results will be different. On a multi-table join query, on has an earlier effect than where. The system first synthesizes a temporary table based on the conditions of the joins between the tables, then the where is filtered, then calculated, and then filtered by having. Thus, to filter the conditions to play the right role, first of all to understand when this condition should play a role, and then decide, put there.

12. Reduce the query on the table:

In the SQL statement that contains the subquery, pay particular attention to reducing the query on the table. Example:

Select TableName from tables

WHERE (Tab_name,db_ver) = (select Tab_name,db_ver from Tab_colimns

where version =604)

13. Improve SQL efficiency with intrinsic functions.:

Complex SQL often sacrifices execution efficiency. The ability to master the above application function to solve the problem is very meaningful in practical work.

14. Alias using the table:

When you concatenate multiple tables in an SQL statement, use the alias of the table and prefix the alias to each column. This reduces the time to parse and reduces the syntax errors caused by column ambiguity.

15. Replace in with exists instead of not exists instead of in:

In many base-table-based queries, it is often necessary to join another table in order to satisfy one condition, in which case the use of exists (or not exists) will usually improve the efficiency of the query. In a subquery, the NOT IN clause performs an internal sort and merge. In either case, not in is the least effective (because it performs a full table traversal of the table in the subquery). To avoid using not, we can change it to an outer join (Outer Joins) or not exists.

Example:

Efficient

Select # from EMP where

Empno>0 and exists

(SELECT ' x ' from dept where Dept.deptno =emp.deptno and loc= ' Melb ')

(Low efficiency)

SELECT * from emp where empno>0 and Deptno in

(select Deptno Dept where loc= ' Melb ')

16. Identify the SQL statement for ' inefficient execution ': although there are many graphical tools for SQL optimization, it is always a good idea to write your own SQL tools to solve the problem:

Select executions, Disk_reads, Buffer_gets,

Round ((buffer_gets-disk_reads)/buffer_gets,2) Hit_radio,

Round (disk_reads/executions,2) Reads_per_run,

Sql_text from V$sqlarea where

Executions>0 and buffer_gets > 0 and

(buffer_gets-disk_reads)/buffer_gets < 0.8

ORDER BY 4 Desc;

17. Improve efficiency with indexes:

An index is a conceptual part of a table used to improve the efficiency of retrieving data, and Oracle uses a complex self-balancing b-tree structure. In general, querying data through an index is faster than a full table scan. When Oracle finds the best path to execute queries and UPDATE statements, the Oracle Optimizer uses the index. Also, using indexes when joining multiple tables can improve efficiency. Another advantage of using an index is that it provides the uniqueness of the primary key (primary key) Validation: Those long or long raw data types, you can index almost all the columns. In general, using indexes in large tables is particularly effective. Of course, you will also find that using indexes can improve efficiency when scanning small tables. Although the use of indexes can improve query efficiency, we must also pay attention to its cost. Indexes require space to store, and they need to be maintained regularly, and the index itself is modified whenever a record is added to a table or the index column is modified. This means that the Insert Delete update for each record will pay 4, 5 more disk I/O. Because indexes require additional storage space and processing, those unnecessary indexes can slow query response time. Periodic refactoring of indexes is necessary: Alter INDEX <indexname> rebuild <tablespacename>

18. Replace distinct with exists: avoid using DISTINCT in the SELECT clause when submitting a query that contains one-to-many table information, such as a departmental table and an employee table. It is generally possible to consider replacing with EXIST, EXISTS makes the query faster because the RDBMS core module will return the results immediately after the conditions of the subquery have been met.

Example:

(inefficient):

SELECT DISTINCT Dept_no,dept_name from Dept D, EMP E where

D.dept_no = E.dept_no

(efficient):

Select Dept_no,dept_name

From Dept D

where

Exists (select ' x ' from emp e where e.dept_no = D.dept_no);

The 19.sql statement is capitalized; because Oracle always parses the SQL statements first, the lowercase letters are converted to uppercase and then executed.

20. Use the connector "+" connection string sparingly in Java code!

21. Avoid using not on indexed columns typically, we want to avoid using not on indexed columns, which does not produce the same effect as using functions on indexed columns. When Oracle "encounters" not, he stops using the index instead of performing a full-table scan.

22. Avoid using calculations on indexed columns:

Where clause, if the index column is part of a function.  The optimizer will use a full table scan without using an index. Example:

Low efficiency:

Select ... from dept where Sal * > 25000;

Efficient:

Select ... from dept where Sal > 25000/12;

23. Replacing > with >=

Efficient: SELECT * from emp where Deptno >=4

Inefficient: SELECT * from emp where Deptno >3

The difference between the two is that the former DBMS will jump directly to the first record that dept equals 4, and the latter will first locate the Deptno =3 record and scan forward to the first dept greater than 3.

24. Replace or with union (for indexed columns) in general, replacing or in the WHERE clause with union will have a good effect. Using or on an indexed column causes a full table scan. Note that the above rules are valid only for multiple indexed columns. If a column is not indexed, the query efficiency may be reduced because you did not select or. In the following example, indexes are built on both loc_id and region.

Efficient:

Select loc_id, Loc_desc, region from location

where loc_id = 10

Union

Select loc_id, Loc_desc, Region

From location

where region = "Melbourne"

Low efficiency:

Select loc_id, Loc_desc, region from location

where loc_id = ten or region = "Melbourne"

If you persist in using or, you need to return the least logged index column to the front.

25. Replacing or with in is a simple and easy-to-remember rule, but the actual execution has to be tested, and the execution path seems to be the same.

Low efficiency:

Select .... from location where loc_id = ten or loc_id = 30 or loc_id

Efficient

Select ... from location where loc_in in (10,20,30);

26. Avoid using is null on the index column and is not NULL to avoid using any nullable columns in the index, and Oracle will not be able to use that index. For single-column indexes, this record will not exist in the index if the column contains null values. For composite indexes, if each column is empty, the same record does not exist in the index. If at least one column is not empty, the record exists in the index. For example, if a uniqueness index is established on column A and column B of a table, and the table has a value of a, a and a record of (123,null), Oracle will not accept the next record (insert) with the same A, B value (123,null). However, if all the index columns are empty, Oracle will assume that the entire key value is empty and null is not equal to NULL. So you can insert 1000 records with the same key value, of course they are empty! Because null values do not exist in the index column, a null comparison of indexed columns in the WHERE clause causes Oracle to deactivate the index.

Inefficient: (Index invalidation)

Select ... from department where Dept_code are not null;

Efficient: (Index valid)

Select ... from department where Dept_code >=0;

27. Always use the first column of an index:

If the index is built on more than one column, the optimizer chooses to use the index only if its first column (leading column) is referenced by a WHERE clause. This is also a simple and important rule that when referencing only the second column of an index, the optimizer uses a full table scan and ignores the index.

28. Replace the union with Union-all (if possible): When the SQL statement requires a union of two query result sets, the two result sets are merged in a union-all manner and then sorted before the final result is output. If you replace union with Union-all, this sort is not necessary. Efficiency will therefore be improved. It is important to note that Union-all will output the same record in the two result set repeatedly. Therefore, you still need to analyze the feasibility of using union-all from the business requirements. The Union will sort the result set, which will use the memory of the sort_area_size. The optimization of this memory is also very important. The following SQL can be used to query the consumption of sorts.

Low efficiency:

Select Acct_num, Balance_amt

From Debit_transactions

where tran_date = ' 31-dec-95 '

Union

Select Acct_num, Balance_amt

From Debit_transactions

where tran_date = ' 31-dec-95 '

Efficient:

Select Acct_num, Balance_amt

From Debit_transactions

where tran_date = ' 31-dec-95 '

UNION ALL

Select Acct_num, Balance_amt

From Debit_transactions

where tran_date = ' 31-dec-95 '

29. Replace order by with where:

The ORDER BY clause uses the index only under two strict conditions.

All columns in an order by must be in the same index and remain in the order in which they are arranged in the index.

All columns in the order by must be defined as non-empty.

The index used in the WHERE clause and the index used in the ORDER BY clause cannot be tied.

For example:

Table Dept contains the following:

Dept_code PK NOT NULL DEPT_DESC NOT NULL DEPT_TYPE NULL

Low efficiency:

(index not being used)

Select Dept_code from Dept ORDER by Dept_type

Efficient:

(Use index)

Select Dept_code from dept where dept_type > 0

30. Avoid changing the type of indexed columns:

ORACLE automatically makes simple type conversions to columns when comparing data of different data types. Suppose EMPNO is an indexed column of a numeric type.

Select ... from emp where empno = ' 123 '

In fact, after the ORACLE type conversion, the statement translates to:

Select ... from emp where empno = To_number (' 123 ')

Fortunately, the type conversion did not occur on the index column, and the purpose of the index was not changed.

Now, suppose Emp_type is an indexed column of a character type.

Select ... from emp where emp_type = 123

This statement is translated by Oracle to:

Select ... from emp whereto_number (emp_type) =123

This index will not be used because of the type conversions that occur internally!

To avoid the implicit type conversion of your SQL by Oracle, it is best to explicitly express the type conversions. Note When comparing characters to numbers, Oracle takes precedence over numeric types to character types

31. The WHERE clause to be careful: the WHERE clause in some SELECT statements does not use an index.

Here are some examples. In the following example,

(1) '! = ' will not use the index. Remember, the index can only tell you what exists in the table, not what does not exist in the table.

(2) ' | | ' is a character join function. As with other functions, the index is deactivated.

(3) ' + ' is a mathematical function. As with other mathematical functions, the index is deactivated.

(4) The same index columns cannot be compared to each other, which will enable full table scanning.

32.

A. If the number of records in a table that has more than 30% data is retrieved. Using indexes will have no significant efficiency gains.

B. In certain situations, using an index may be slower than a full table scan, but this is the same order of magnitude difference. In general, the use of indexes than the full table scan to block several times or even thousands of times!

33. Avoid resource-intensive operations: SQL statements with Distinct,union,minus,intersect,order by will start the SQL engine to perform a resource-intensive sort (sort) function. Distinct requires a sort operation, while the others need to perform at least two sorting. Typically, SQL statements with Union,minus,intersect can be overridden in other ways. If your database is sort_area_size well, using Union,minus,intersect is a good way to think about it. After all, they're very readable.

34. Optimize GROUP BY: increase the efficiency of the group BY statement by filtering out unwanted records before group by. The following two queries return the same result but the second one is significantly faster.

Low efficiency:

Select Job, AVG (SAL) from EMP

GROUP BY Job

Having job = ' president ' or job = ' manager '

Efficient:

Select Job, AVG (SAL) from

EMP WHERE job = ' President '

or job = ' manager '

GROUP BY Job

A small note on SQL optimization rules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.