Oracle performance optimization notes

Source: Internet
Author: User
Tags mathematical functions

(1) select the most efficient table name sequence (only valid in the rule-based Optimizer): The Oracle parser processes the table names in the from clause in the order from right to left, the base table driving table written in the from clause will be processed first. When the from clause contains multiple tables, you must select a table with the least number of records as the base table. If more than three tables are connected for query, You need to select an intersection table as the base table, which is the table referenced by other tables.

(2) connection sequence in the WHERE clause: Oracle uses the bottom-up sequence to parse the WHERE clause. According to this principle, the connection between tables must be written before other where conditions, the conditions that can filter out the maximum number of records must be written at the end of the WHERE clause.

(3) Avoid using '*' in the select clause: during Oracle parsing, '*' is converted to all column names in sequence, this task is done by querying the data dictionary, which means it takes more time.

(4) Reduce the number of visits to the database: Oracle has performed a lot of work internally: parsing SQL statements, estimating the index utilization, binding variables, and reading data blocks.

(5) re-set the arraysize parameter in SQL * Plus, SQL * forms, and Pro * C to increase the retrieval data volume for each database access. The recommended value is 200.

(6) use the decode function to reduce processing time: Use the decode function to avoid repeated scanning of the same record or joining the same table.

(7) simple integration and non-associated database access: If you have several simple database query statements, you can integrate them into a single query (even if there is no relationship between them ).

(8) delete duplicate records: the most efficient method for deleting duplicate records (because rowid is used) Example: delete from EMP e where E. rowid> (select Min (X. rowid) from emp x where X. emp_no = E. emp_no ).

(9) replace Delete with truncate: When deleting records in a table, a rollback segment is usually used to store recoverable information. if you do not have a commit transaction, Oracle will recover the data to the State before the deletion (which is precisely the State before the deletion command is executed). When truncate is used, rollback segments no longer store any recoverable information. after the command is run, the data cannot be restored. therefore, few resources are called and the execution time is short. (The translator Press: truncate only applies when deleting the entire table, Truncate is DDL rather than DML ).

(10) use commit as much as possible: Use commit as much as possible in Program to improve program performance, the demand will also be reduced because of the resources released by commit:
. information used to restore data on the rollback segment.
B. locks obtained by Program Statements
C. space in redo log buffer
D. oracle manages the internal costs of the preceding three types of resources

(11) replace having clause with the WHERE clause: avoid having clause. Having filters the result set only after all records are retrieved. this process requires sorting, total, and other operations. if the WHERE clause can be used to limit the number of records, this overhead can be reduced. (in non-Oracle) Where on, where, and having can be added, on is the first statement to execute, where is the second clause, and having is the last clause, because on filters out records that do not meet the conditions before making statistics, it can reduce the data to be processed by the Intermediate operation. The speed should be the fastest, and the where should be faster than having, because it only performs sum after filtering data, and on is used only when two tables are joined, so in a table, then we can compare where with having. In the case of single-Table query statistics, if the filter condition does not involve fields to be calculated, the results will be the same, but the where technology can be used, having cannot be used. In terms of speed, the latter must be slow. If a calculated field is involved, the value of this field is uncertain before calculation, according to the workflow written in the previous article, the where function time is completed before computing, and having works only after computing. In this case, the results are different. In multi-table join queries, on takes effect earlier than where. The system first combines multiple tables into a temporary table based on the join conditions between tables, then filters them by where, then computes them, and then filters them by having after calculation. It can be seen that to filter a condition to play a correct role, you must first understand when the condition should take effect and then decide to put it there.

(12) Reduce table queries: in SQL statements containing subqueries, pay special attention to reducing table queries. example: Select tab_name from tables where (tab_name, db_ver) = (selecttab_name, db_ver from tab_columns where version = 604 ).

(13) Improve SQL efficiency through internal functions. complex SQL statements tend to sacrifice execution efficiency, so it is very meaningful to master the above method for solving problems using functions.

(14) use the table alias (alias): when connecting multiple tables in an SQL statement, use the table alias and prefix the alias on each column. in this way, the parsing time can be reduced and the syntax errors caused by column ambiguity can be reduced.

(15) replace in with exists and not exists instead of not in: In many basic table-based queries, to meet one condition, you often need to join another table. in this case, using exists (or not exists) usually improves the query efficiency. in a subquery, the not in Clause executes an internal sorting and merging. in either case, not in is the most inefficient (because it executes a full table traversal for the table in the subquery ). to avoid using not in, we can rewrite it into an outer join (Outer Join S) or not exists. example: (efficient) Select * from EMP (basic table) Where empno> 0 and exists (select 'x' from Dept where Dept. deptno = EMP. deptno and loc = 'melb') (inefficient) Select * from EMP (basic table) Where empno> 0 and deptno in (select deptno from Dept where loc = 'melb ').

(16) identifying 'inefficient execution' SQL statements: although a variety of graphical tools for SQL optimization are emerging, writing your own SQL tools is always the best way 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,
S Ql_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) using indexes to improve efficiency: indexes are a conceptual part of a table to improve data retrieval efficiency. Oracle uses a complex self-balancing B-tree structure. data Query by index is usually faster than full table scan. when Oracle finds the optimal path for executing the query and update statements, the Oracle optimizer uses the index. using indexes when joining multiple tables can also improve efficiency. another advantage of using an index is that it provides uniqueness verification for the primary key .. For those long or long raw data types, You Can index almost all columns. generally, using indexes in large tables is particularly effective. of course, you will also find that using indexes to scan small tables can also improve efficiency. although the index can improve the query efficiency, we must pay attention to its cost. the index requires space for storage and regular maintenance. The index itself is also modified whenever a record is increased or decreased in the table or the index column is modified. this means that the insert, delete, and update operations for each record will pay four or five more disk I/O. because indexes require additional storage space and processing, unnecessary indexes will slow the query response time .. Regular index reconstruction is necessary: Alter index rebuild .

(18) replace distinct with exists: when you submit a query that contains one-to-many table information (such as the Department table and employee table), avoid using distinct in the select clause. in general, you can consider replacing it with exist, and exists makes the query more rapid, because the RDBMS core module will return the result immediately after the subquery conditions are 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 ).

(19) SQL statements are written in uppercase, because Oracle always parses SQL statements first, converts lowercase letters to uppercase and then executes them.

(20) In JavaCodeTry to use the connector "+" to connect strings!

(21) Avoid using not in the index column. We should avoid using not in the index column, not will have the same impact as using the function in the index column. when Oracle Encounters "not", it stops using indexes and then performs full table scanning.

(22) avoid using the computing. Where clause in the index column. If the index column is part of the function, the optimizer will use full table scanning without using the index. Example: inefficient: select... From dept where Sal * 12> 25000; efficiency: select... From dept where SAL> 25000/12.

(23) Replace with> => efficiency: Select * from EMP where deptno> = 4 inefficiency: Select * from EMP where deptno> 3. The difference between the two is, the former DBMS will jump directly to the first record whose DEPT is equal to 4, while the latter will first locate the record whose deptno is = 3 and scan forward to the record whose first DEPT is greater than 3.

(24) replacing or with Union (applicable to index columns) usually results in better performance when replacing or with union in the WHERE clause. using or for index columns will scan the entire table. note that the preceding rules are only valid for multiple index columns. if a column is not indexed, the query efficiency may be reduced because you did not select or. in the following example, both loc_id and region have indexes. efficient: Select loc_id, loc_desc, region from location where l Oc_id = 10 Union select loc_id, loc_desc, region from location where region = "Melbourne" inefficiency: Select loc_id, loc_desc, region from location where loc_id = 10 or region = "Melbourne" if you insist on using or, you need to write at the beginning of the index column with the least record returned.

(25) replace or with in. This is a simple and easy-to-remember rule, but the actual execution results must be tested. in Oracle8i , the execution paths of the two seem to be the same. inefficient: select .... From location where loc_id = 10 or loc_id = 20 or loc_id = 30 efficient select... From location where loc_in in (10, 20, 30 ).

(26) avoid using is null and is not null in the index column to avoid using any columns that can be empty in the index. Oracle will not be able to use this index. this record does not exist in the index if the column contains a null value. for a composite index, if each column is empty, this record does not exist in the index. if at least one column is not empty, the record is stored in the index. for example, if the unique index is created in column A and column B of the table, and the and B values of a record exist in the table are (123, null ), oracle will not accept the next entry with the same A and B values (123, NUL L) records (insert ). however, if all index columns are empty, Oracle considers the entire key value to be null, but null is not equal to null. therefore, you can insert 1000 records with the same key value. Of course, they are empty! Because the null value does not exist in the index column, the null value of the index column in The WHERE clause will make Oracle disable the index. Inefficiency: (index failure) select... From department where dept_code is not null; efficient: (index valid) select... From department where dept_code> = 0.

(27) always use the first column of the index: If the index is created on multiple columns, only when its first column (Leading column) is referenced by the WHERE clause, the optimizer selects to use this index. this is also a simple and important rule. When only the second column of the index is referenced, the optimizer uses a full table scan and ignores the index.

(28) replace union with Union-all (if possible): When an SQL statement needs to union two query result sets, these two result sets are merged in the form of union-all, then sort the final result. if Union all is used to replace union, sorting is unnecessary. the efficiency will be improved accordingly. note that Union all will repeatedly output the same records in the two result sets. therefore, you still need to analyze the feasibility of using Union all from the business needs. union sorts the result set This operation will use the sort_area_size memory. this memory optimization is also very important. the following SQL statement can be used to query the consumption efficiency of sorting: Select acct_num, balance_amt from debit_transactions where tran_date = '31-DEC-95 'Union select acct_num, balance_amt from nation where tran_date = '31-DEC-95 'efficiency: Select acct_num, balance_amt from debit_transactions
where tran_date = '31-DEC-95' Union all select acct_num, balance_amt from nation where Tran_date = '31-DEC-95 '.

(29) replace the order by: Order by clause with where to use the index only under two strict conditions.
all columns of order by must be included in the same index and sorted in the index.
all columns in 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 together. for example, the dept table contains the following columns:
dept_code PK not null
dept_desc not null
dept_type null is inefficient: (indexes are not used) select dept_code from Dept order by dept_type efficiency: (using indexes) select dept_code from Dept where dept_type> 0

(30) avoid changing the index column type.: When comparing data of different data types, Oracle automatically performs simple type conversion on columns. assume that empno is a numeric index column. select... From EMP where empno = '000000' in fact, after Oracle type conversion, the statement is converted to: select... From EMP where empno = to_number ('123') Fortunately, the type conversion does not occur on the index column, and the purpose of the index is not changed. assume that emp_type is a character-type index column. select... From EMP where emp_type = 123 this statement is converted to: select... From EMP whereto_number (emp_type) = 123 because of internal type conversion, this index will not be used! To avoid implicit type conversion for your SQL statements, it is best to explicitly display the type conversion. note that when comparing characters and values, Oracle converts the value type to the character type first.

(31) WHERE clause to be careful: The WHERE clause in some select statements does not use indexes. Here are some examples. In the following example, (1 )'! = 'No index is used. remember, indexes only tell you what exists in the table, but not what does not exist in the table. (2) '|' is a character concatenation function. as with other functions, indexes are disabled. (3) '+' is a mathematical function. as with other mathematical functions, indexes are disabled. (4) The same index Columns cannot be compared with each other, which enables full table scan.

(32) A. If the retrieved data volume exceeds 30% of the number of records in the table, using indexes will not significantly improve the efficiency.
B. in certain cases, using indexes may be slower than full table scanning, but this is an order of magnitude difference. in general, using an index is several times or even several thousand times more than a full table scan!

(33) Avoid resource-consuming operations: SQL statements with distinct, union, minus, intersect, and order by enable the SQL engine to execute resource-consuming sorting (SORT. distinct requires a sorting operation, while other operations require at least two sorting operations. generally, SQL statements with union, minus, and Intersect can be rewritten in other ways. if your database's sort_area_size is well configured, you can also consider using Union, minus, and intersect. After all, they are highly readable.

(34) Optimize group by: Improve the efficiency of group by statements. You can filter out unnecessary records before group. the following two queries return the same results, but the second one is much faster. inefficiency:
Select job, AVG (SAL)
From EMP group job having job = 'President 'or job = 'manager' efficiency:
Select job, AVG (SAL)
From EMP
Where job = 'President'
Or job = 'manager' group job

Related Article

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.