Some uncommon but important tips for using Oracle databases

Source: Internet
Author: User

Oracle DatabaseThere are some skills that need to be understood and mastered during use. To master these skills, we can avoid many detours and improve query efficiency in the future database use process. This article mainly introduces the seven-point skills and hopes to help you. Next we will introduce them one by one.

TIPS:

1. Do not include variables after select in procedure and function; otherwise, it will greatly affectSQL Efficiency.

2. TRUNCATE cannot be used in procedure. It can be written:

 
 
  1. EXECUTE IMMEDIATE 'truncate table smic_rtd_bullet_lot_tmp'; 

3. when you run the DML data monopoly language, add, delete, modify, and query statements, PL/SQL opens a built-in cursor and processes the results. The cursor is a region in the memory that maintains the query results, the cursor is open when running the DML statement. Implicit cursors only use SQL % FOUND, SQL % NOTFOUND, and SQL % ROWCOUNT attributes. SQL % FOUND, SQL % NOTFOUND is a Boolean value, and SQL % ROWCOUNT is an integer. This can be used as an inference criterion to exit the loop when the loop ends.

4. In order not to repeatedly parse the same SQL statement, after the sequential parsing, ORACLE will store the SQL statement in the memory. The memory in the shared buffer pool of the SGA system global area can be shared by all database users. Therefore, when you execute an SQL statement (sometimes called a cursor), in case it is exactly the same as the previous statement, ORACLE will be able to quickly obtain the statements that have been parsed and good paths. The shared statement definitely has three conditions:

A. Character-level comparison: currently, the statements in the statement and sharing pool must be completely the same. Including all spaces and uppercase/lowercase letters.

B. The objects referred to by the two statements must be completely identical. For example, if two users have a unified table, one is the table owner and the other is a reference synonym, they cannot share SQL statements.

C. The two SQL statements certainly use the same name to bind variables. All variables have the same name. Even at runtime, the same value can be assigned to different binding variables.

Unified SQL is used in multiple sessions. In case of multiple changes, the SQL statement is not shared due to inconsistent spaces and uppercase/lowercase letters. Use WITH a AS (SELECT * FROM ......) The query name is a, and then a can be used as an SQL statement, which can ensure that multiple times of sequential parsing can improve the efficiency.

5. Whenever possible, use COMMIT as much as possible during the process, so that the features in this process can grow and the demand will also shrink due to the resources released by COMMIT:

Resources released by COMMIT:

A. The message on the rollback segment is used to repeat the data.

B. The lock obtained by the process statement.

C. Space in redo log buffer.

D. ORACLE manages the internal costs of these three types of resources.

When using COMMIT, you must always be aware of the comprehensiveness of the transaction. In practice, efficiency and comprehensiveness of the transaction are often the same as that of the fish and the bear's paw.

6. Improve SQL efficiency through internal functions.

 
 
  1. SELECT H.EMPNO,E.ENAME,H.HIST_TYPE,T.TYPE_DESC,COUNT(*)  
  2.  
  3. FROM HISTORY_TYPE T,EMP E,EMP_HISTORY H  
  4.  
  5. WHERE H.EMPNO = E.EMPNO  
  6.  
  7. AND H.HIST_TYPE = T.HIST_TYPE  
  8.  
  9. GROUP BY H.EMPNO,E.ENAME,H.HIST_TYPE,T.TYPE_DESC; 

The following functions can be called to improve efficiency.

 
 
  1. FUNCTION LOOKUP_HIST_TYPE (typ in number) RETURN VARCHAR2
  2.  
  3. AS
  4.  
  5. TDESC VARCHAR2 (30 );
  6.  
  7. CURSOR C1 IS
  8.  
  9. SELECT TYPE_DESC
  10.  
  11. FROM HISTORY_TYPE
  12.  
  13. WHERE HIST_TYPE = TYP;
  14.  
  15. BEGIN
  16.  
  17. OPEN C1;
  18.  
  19. FETCH C1 into tdesc;
  20.  
  21. CLOSE C1;
  22.  
  23. RETURN (NVL (TDESC, Nikon lens '? '));
  24.  
  25. END;
  26.  
  27. FUNCTION LOOKUP_EMP (emp in number) RETURN VARCHAR2
  28.  
  29. AS
  30.  
  31. ENAME VARCHAR2 (30 );
  32.  
  33. CURSOR C1 IS
  34.  
  35. SELECT ENAME
  36.  
  37. FROM EMP
  38.  
  39. Where empempno = EMP;
  40.  
  41. BEGIN
  42.  
  43. OPEN C1;
  44.  
  45. FETCH C1 into ename;
  46.  
  47. CLOSE C1;
  48.  
  49. RETURN (NVL (ENAME ,'? '));
  50.  
  51. END;
  52.  
  53. Select h. EMPNO, baby milk rankings LOOKUP_EMP (H. EMPNO ),
  54.  
  55. H. HIST_TYPE, LOOKUP_HIST_TYPE (H. HIST_TYPE), COUNT (*)
  56.  
  57. FROM EMP_HISTORY H
  58.  
  59. Group by h. EMPNO, H. HIST_TYPE;

Many people want to use an SQL statement to write the necessary data. However, they do not know that hybrid SQL statements often devote themselves to strict efficiency. Being able to master the above measures to solve problems using functions is of extreme significance in practical work.

7. In general, using UNION to rotate OR in the WHERE clause will achieve better results. Using OR to index columns will cause a full table scan. Note: The above rules are only applicable to multiple index columns. If a column is not indexed, the query efficiency may decrease because you have no choice OR choice.

This section describes how to use Oracle databases.

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.