Oracle Database Optimization

 

 

 

 

 

Oracle Database Optimization

 

 

 

 

 

 

Information Management Department

 

 

content

1. Avoid using '*' in the SELECT query statement................................. ............. 2

2. Reduce the number of database accesses: ................................................ .................................. 2

3. Querying a single record ................................................ ...................................................... 2

4. Select the optimal table name order: ................................................ ........................................ 2

5. Connections in the WHERE clause ................................................ ...................................... 3

6. Use the DECODE function to avoid repeatedly scanning the same records or repeatedly connecting the same table 3

7. It is recommended to use TRUNCATE but not DELETE for delete full table operations.......... 3

8. Use COMMIT as much as possible: ................................................ ................................. 4

Nine, reduce the query on the table: ................................................ ...................................... 5

10. Improve SQL efficiency through internal functions.:................................................ .................5

Eleven, using the alias of the table (Alias): ................................................ ................................. 5

12. Set indexes for common query conditions...................................................... ............................... 5

13. Use uppercase for sql statements; because oracle always parses sql statements first, converts lowercase letters to uppercase and then executes...................... ...................................................... ............................................... 6

14. Use the connector "+" to connect strings as little as possible in java code! ................ 6

15. Avoid using NOT on indexed columns ................................................ ............................... 6

16. Avoid using calculations on indexes ................................................ ............................... 6

17. Use >= instead of >................................................ ...................................................... ... 6

18. Replacing OR with UNION (for indexed columns)................................ ...........6

19. Replacing OR with IN in ordinary queries ................................................ ............................... 7

20. Avoid using IS NULL and IS NOT NULL on indexed columns.......... 7

21. Always use the first column of the index: ................................ ................................ 7

22. Replace UNION with UNION-ALL (if possible): ......... 7

23. Avoid changing the type of index columns.:................................................ .................................7

24. Some WHERE clauses do not use indexes ................................................ ................. 7

25. Avoid resource-intensive operations:................................................ ....................... 8

Twenty-six, optimize GROUP BY................................................ ....................................... 8

Twenty-seven, .............................................. ...................................................... .................. 8

 

 

 

1. Avoid using '*' in the SELECT query statement

Second, reduce the number of database access:

      Reduce database IO operation pressure

3. Query a single record

Fourth, select the optimal table name order:

       The parsing rules of the Oracle parser process the table names of the From clause from right to left. In this case, the table or cross-tab (the table referenced by other) with the least number of records should be used as the base table (the last table written in the From clause)

Five, the connection in the WHERE clause

The Oracle parser parses the WHERE clause in a bottom-to-top order. The filter conditions for filtering a large amount of data are recommended to be written at the end of the WHERE clause.

6. Use the DECODE function to avoid repeated scanning of the same records or repeated connections to the same table

7. It is recommended to use TRUNCATE for delete full table operations, but DELETE is not recommended

TRUNCATE will completely delete the data irrecoverably, and consume less resources than DELETE.

① Functionally, truncate clears the contents of a table, which is equivalent to delete from table_name

② delete is a dml operation, and truncate is a ddl operation; therefore, when delete is used to delete the data of the entire table, a large number of rollbacks (rollbacks) will be generated, occupying a lot of rollback segments (rollback segments), and truncate will not

③ In the memory, use delete to delete data, the space occupied by the table whose data is deleted in the table space is still there, which is convenient for future use. In addition, it is a "fake" deletion, which is equivalent to deleting data with delete in windows. Put it in the recycle bin, and it can be restored. Of course, if the system (OS or RDBMS) is restarted at this time, it cannot be restored!

When using truncate to clear data, the space occupied by the table whose data has been deleted in the in-memory table space will be released immediately, which is equivalent to using shift+delete to delete data in Windows, which cannot be recovered!

④ truncate adjusts the high water mark but delete does not; after truncate, the HWM of TABLE returns to the position of INITIAL and NEXT (default), delete is not allowed.

⑤ truncate can only be for TABLE, delete can be table, view, synonym

⑥ The object of TRUNCATE TABLE must be in this mode, or have the permission to drop any table and DELETE is that the object must be in this mode, or be granted the permission of DELETE ON SCHEMA.TABLE or DELETE ANY TABLE

⑦In the outer layer, after truncate or delete, the space occupied by it will be released

⑧ truncate and delete only delete data, while drop deletes the entire table (structure and data)

Tip: When deleting a large amount of data (most of the data in a table),

First copy the data that does not need to be deleted to a temporary table;

trunc table table;

Copy back the data that does not need to be deleted.

8. Use COMMIT as much as possible:

Reduce as much as possible using the resources released by the COMMIT request:

Resources released by COMMIT:

a. Information on the rollback segment used to restore data.

b. Locks acquired by program statements

c. Space in redo log buffer

d. ORACLE manages the internal costs of the above three resources

Nine, reduce the query on the table:

In SQL statements containing subqueries, special attention should be paid to reducing the number of queries to the table.

10. Improve SQL efficiency through internal functions.:

Complex SQL often sacrifices execution efficiency. It is more inclined to use functions to solve problems

11. Use the alias of the table (Alias):

When joining multiple tables in an SQL statement, use table aliases and prefix each column with the alias. This reduces parsing time and reduces syntax errors caused by column ambiguity.

12. Set indexes for common query conditions

An index is a conceptual part of a table that is used to improve the efficiency of retrieving data. 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 out, execute the query and Update The ORACLE optimizer will use an index when determining the best path for a statement. Also, using an index can improve efficiency when joining multiple tables. Another benefit of using an index is that it provides primary key uniqueness verification. For those LONG or LONG RAW data types, you can index almost any column. In general, using an index is especially effective on large tables. Of course, you will also find that when scanning small tables, using an index can also improve efficiency. Although using an index The query efficiency can be improved, but we must also pay attention to its cost. The index needs space for storage and regular maintenance. Whenever a record is added or deleted in the table or the index column is modified, the index itself will also be modified. . This means that INSERT, DELETE, UPDATE of each record will cost 4 or 5 more disk I/Os for this. Because indexes require additional storage space and processing, those unnecessary indexes will make query response time worse. slow.. Periodically rebuilding the index is necessary:

ALTER INDEX <INDEXNAME> REBUILD <TABLESPACENAME>

13. Use uppercase for sql statements; because oracle always parses sql statements first, converts lowercase letters to uppercase and then executes them

14. Use the connector "+" to connect strings as little as possible in java code!

15. Avoid using NOT on indexed columns  

NOT has the same effect as using a function on an indexed column. When ORACLE "encounters" NOT, it stops using the index and performs a full table scan instead.

16. Avoid using calculations on indexes

The index column is part of the function. The optimizer will use full table scans instead of indexes.

17. Use >= instead of >

Problems with data record processing mechanism

18. Replace OR with UNION (for indexed columns)

Replacing OR in the WHERE clause with UNION will have better results. Using OR on indexed columns will cause a full table scan. Note that the above rules are only valid for multiple indexed columns. If there are columns that are not indexed, the query efficiency will be reduced. may be lower because you didn't choose OR

19. Use IN to replace OR in ordinary queries

20. Avoid using IS NULL and IS NOT NULL on indexed columns

Try to avoid null values ​​in the field information. A null value field in the index will cause ORACLE to fail to use the index.

21. Always use the first column of the index:

If an index is built on multiple columns, the optimizer will choose to use the index only if its first column (leading column) is referenced by the where clause. This is also a simple but important rule, when only referencing The optimizer used a full table scan while ignoring the index on the second column of the index

22. Replace UNION with UNION-ALL (if possible):

When the SQL statement needs to UNION two query result sets, the two result sets will be combined in a UNION-ALL manner, and then sorted before outputting the final result. If UNION ALL is used instead of UNION, then sorting is not necessary

UNION ALL will repeatedly output the same records in the two result sets, so please use it according to your specific needs.

23. Avoid changing the type of index columns.:

When comparing data of different data types, ORACLE automatically performs simple type conversions on columns. .

24. Some WHERE clauses do not use indexes

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

(2)'||' is a character concatenation function. Like other functions, indexing is disabled.

(3)'+' is a math function. Like other math functions, indexing is disabled.

(4) The same index columns cannot be compared with each other, which will enable a full table scan.

25. Avoid resource-intensive operations:

SQL statements with DISTINCT, UNION, MINUS, INTERSECT, ORDER BY will start the SQL engine

Performs a resource-intensive sorting (SORT) function. DISTINCT requires one sorting operation, while others require at least two sorting operations. In general, SQL statements with UNION, MINUS, INTERSECT can be rewritten in other ways.

26. Optimize GROUP BY

Improve the efficiency of GROUP BY statements by filtering out unwanted records before GROUP BY

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028344&siteId=291194637