SQL statement optimization policy

Source: Internet
Author: User

In general, the SQL statement optimization policy has the following aspects:
● When creating a table. Try to create a primary key to query data based on the primary key. ● delete a big data table and replace delete with truncate table. ● Use indexes properly. do not have too many indexes for a table in OLTP applications. The column sequence of the composite index should be the same as that of the query condition column. For tables with frequent data operations, the index should be rebuilt regularly to reduce invalid indexes and fragments. ● Use a definite column name as much as possible for query, and use less * numbers. Use as few nested subqueries as possible. Such queries consume a large amount of CPU resources. For queries with more or operations, we recommend that you divide them into multiple queries and join them with union all; select the most efficient table name order in the Multi-Table query statement (effective in the rule-based Optimizer ). The Oracle parser parses tables from right to left, so the tables with fewer records are placed on the right. ● Use the commit statement whenever possible to submit transactions, which can release resources, unlock, release log space, and reduce management costs in a timely manner. Avoid remote access when performing frequent data operations with high performance requirements, for example, database links. frequently accessed tables can be stored in the memory: alter table... cache;
Detailed score:

1. Use the IN operator as few as possible. Basically, all IN operators can be replaced by EXISTS.
2. Do NOT use the not in operator. Instead, use not exists or outer join + (Outer Join + is null. 3. No "<>" or "! = "Operator. Processing Non-equals operators will cause full table scanning, which can be replaced by "<" or ">. For example, change a <> 0 to a> 0 or a <0, a <> ''To a>''
4. If the Where clause is null or is not null, Oracle will stop using the index and perform a full table scan. When designing a table, you can set the index column to not null. In this way, other operations can be used to replace NULL operations.
5. When the wildcard "%" or "_" is the first character of the query string, the index is not used. Therefore, it is generally not used as the first character.
6. For the connected column "|", the index of the last connected column is invalid. Avoid connections whenever possible. Separate connections or use functions that do not work on columns.
7. If the index is not function-based, the index will no longer function when you use the function for the index column in The Where clause.
8. Avoid using computation in the index column in The Where clause. Otherwise, the entire table is scanned because the index fails.
9. If you compare columns of different data types, the index will become invalid.
10. Replace "> =" with "> = ".
11. The UNION operator filters the results to eliminate duplicates. If the data volume is large, disk sorting may occur. If you do not need to delete duplicate records, use union all.
12. Oracle processes multiple query conditions in the Where clause from bottom to top. Therefore, the table join statement should be written before other Where conditions. The conditions for filtering the maximum number of records must be written at the end of the Where clause.
13. Oracle processes the table names in the From clause From right to left. Therefore, when the From clause contains multiple tables, the table with the least records is placed at the end.
14. Non-index columns in the Order By statement will reduce performance and can be processed By adding an index. Strictly control the use of expressions in Order By statements.
15. For the same SQL statements in different regions, make sure that the query characters are identical, and use the SGA shared pool to prevent the same SQL statements from being analyzed multiple times.
16. Use more internal functions to improve SQL efficiency.
17. When connecting multiple tables in an SQL statement, use the table alias and use it as the prefix of each column. This reduces the parsing time.
18. SELECT different optimization policies based on different SQL optimization modes, and SELECT/* + ALL + _ ROWS */......; . Available HINT includes/* + ALL_ROWS */,/* + FIRST_ROWS */,/* + CHOOSE */,/* + RULE */and so on. Generally, the first_rows policy is added before SQL, the choose policy is used in special cases. (This rule architecture package is supported)
19. Do not convert columns in large table queries, such as To_char, to_date, and to_number.
20. Try to use an index if there is an index, and write the index conditions in front of it.
21. Create some indexes if possible and necessary
22. Avoid full table scan as much as possible, with as many restrictions as possible, so that you can quickly search for the data to be queried.


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.