从PL/SQL FAQ中摘抄出来几篇比较有用的文章

来源:互联网 发布:苹果手机屏幕检测软件 编辑:程序博客网 时间:2024/06/10 10:10

从PL/SQL FAQ中摘抄出来几篇比较有用的文章


全文阅读见PL/SQL FAQ

How can one see if somebody modified any code?

The source code for stored procedures, functions and packages are stored in the Oracle Data Dictionary. One can detect code changes by looking at the TIMESTAMP and LAST_DDL_TIME column in the USER_OBJECTS dictionary view. Example:

  1. SELECT OBJECT_NAME,  
  2.        TO_CHAR(CREATED,       'DD-Mon-RR HH24:MI') CREATE_TIME,  
  3.        TO_CHAR(LAST_DDL_TIME, 'DD-Mon-RR HH24:MI') MOD_TIME,  
  4.        STATUS  
  5. FROM   USER_OBJECTS  
  6. WHERE  LAST_DDL_TIME > '&CHECK_FROM_DATE';  

Note: If you recompile an object, the LAST_DDL_TIME column is updated, but the TIMESTAMP column is not updated. If you modified the code, both the TIMESTAMP and LAST_DDL_TIME columns are updated.


How can one search PL/SQL code for a string/ key value?

The following query is handy if you want to know where certain tables, columns and expressions are referenced in your PL/SQL source code.

  1. SELECT type, name, line  
  2.   FROM   user_source  
  3.  WHERE  UPPER(text) LIKE UPPER('%&KEYWORD%');  

If you run the above query from SQL*Plus, enter the string you are searching for when prompted for KEYWORD. If not, replace &KEYWORD with the string you are searching for.

What is the difference between %TYPE and %ROWTYPE?

Both %TYPE and %ROWTYPE are used to define variables in PL/SQL as it is defined within the database. If the datatype or precision of a column changes, the program automatically picks up the new definition from the database without having to make any code changes.

The %TYPE and %ROWTYPE constructs provide data independence, reduces maintenance costs, and allows programs to adapt as the database changes to meet new business needs.

%TYPE

%TYPE is used to declare a field with the same type as that of a specified table's column. Example:

  1. DECLARE  
  2.    v_EmpName  emp.ename%TYPE;  
  3. BEGIN  
  4.    SELECT ename INTO v_EmpName FROM emp WHERE ROWNUM = 1;  
  5.    DBMS_OUTPUT.PUT_LINE('Name = ' || v_EmpName);  
  6. END;  
  7. /  

%ROWTYPE

%ROWTYPE is used to declare a record with the same types as found in the specified database table, view or cursor. Examples:

  1. DECLARE  
  2.   v_emp emp%ROWTYPE;  
  3. BEGIN  
  4.   v_emp.empno := 10;  
  5.   v_emp.ename := 'XXXXXXX';  
  6. END;  
  7. /  

How does one loop through tables in PL/SQL?

One can make use of cursors to loop through data within tables. Look at the followingnested loops code example.

  1. DECLARE  
  2.    CURSOR dept_cur IS  
  3.    SELECT deptno  
  4.      FROM dept  
  5.     ORDER BY deptno;  
  6.   
  7.    -- Employee cursor all employees for a dept number  
  8.    CURSOR emp_cur (v_dept_no DEPT.DEPTNO%TYPE) IS  
  9.    SELECT ename  
  10.      FROM emp  
  11.     WHERE deptno = v_dept_no;  
  12. BEGIN  
  13.    FOR dept_rec IN dept_cur LOOP  
  14.       dbms_output.put_line('Employees in Department '||TO_CHAR(dept_rec.deptno));  
  15.   
  16.       FOR emp_rec in emp_cur(dept_rec.deptno) LOOP  
  17.          dbms_output.put_line('...Employee is '||emp_rec.ename);  
  18.       END LOOP;  
  19.   
  20.   END LOOP;  
  21. END;  
  22. /  

How often should one COMMIT in a PL/SQL loop? / What is the best commit strategy?

Contrary to popular belief, one should COMMIT less frequently within a PL/SQL loop to prevent ORA-1555 (Snapshot too old) errors. The higher the frequency of commit, the sooner the extents in the undo/ rollback segments will be cleared for new transactions, causing ORA-1555 errors.

To fix this problem one can easily rewrite code like this:

  1. FOR records IN my_cursor LOOP  
  2.    ...do some stuff...  
  3.    COMMIT;  
  4. END LOOP;  
  5. COMMIT;  

... to ...

  1. FOR records IN my_cursor LOOP  
  2.    ...do some stuff...  
  3.    i := i+1;  
  4.    IF mod(i, 10000) = 0 THEN    -- Commit every 10000 records  
  5.       COMMIT;  
  6.    END IF;  
  7. END LOOP;  
  8. COMMIT;  

If you still get ORA-1555 errors, contact your DBA to increase the undo/ rollback segments.

NOTE: Although fetching across COMMITs work with Oracle, is not supported by the ANSI standard.


Issuing frequent commits is bad, bad, BAD! It’s the WORST thing you can do… just don’t do it! In the following example I will create around 7 million rows and then attempt to update a portion of them serially. In addition, I will issue a commit every thousandth row.

Example 1.1: Creating a somewhat large table

  1. SQL> create table big_employee_table  as  select  rownum as eid  ,  e.*  from  hr.employees e  ,  dba_objects do;  
  2. Table created.  
  3. Elapsed: 00:00:12.23  
  4. SQL>   select  count(*)   from  big_employee_table; 
  5.   COUNT(*)  
  6. ----------  
  7.    7838713  
  8.   
  9. Elapsed: 00:00:08.11  

Before I go on, notice that Oracle’s “Create Table As” (CTAS) method blazed thru table creation. That’s 7.84 Million rows in 12.23 seconds. Sometimes, this is the very best method of updating large data sets. The following block updates 100,000 rows, serially, committing every 1000 rows:

Example 1.2: Updating serially

 
  1. SQL> declare  
  2. cursor c is  select  *   from  big_employee_table  where  rownum <= 100000;  
  3. begin  
  4. for r in c loop  
  5. update  big_employee_table  set  salary = salary * 1.03  where  eid = r.eid;  
  6. if mod ( r.eid, 1000 ) = 0 then  
  7. commit;  
  8. end if;  
  9. end loop;  
  10.  end;  
  11. /  

Observe that the update took more time than I have patience for ;). At 20 minutes I killed the session. It is painfully slow and should never be done. Moreover, it chewed up an entire CPU core for the duration. If you’re only updating a few rows, why do it in PL/SQL at all? I like Tom Kyte’s approach (paraphrasing):

  1. Do it in SQL.  
  2. If SQL can’t do it, do it in PL/SQL.  
  3. If PL/SQL can’t do it, do it in Java.  
  4. If Java can’t do it ask yourself if it needs to be done.  

The following block does the same work in bulk:

Example 1.3: Updating in bulk and committing at the end

 
  1. SQL> declare  
  2. type obj_rowid is table of rowid  
  3. index by pls_integer;  
  4. lr_rowid    obj_rowid;  
  5. lr_salary   dbms_sql.number_table;  
  6. cursor c is  
  7. select  rowid rid ,  salary  from  big_employee_table  where  rownum <= 100000;  
  8. begin  
  9. open c;  
  10.  loop  
  11. fetch c bulk collect  
  12. into lr_rowid , lr_salary limit 500;  
  13. for a in 1 .. lr_rowid.count loop  
  14. lr_salary ( a ) := lr_salary ( a ) * 1.03;  
  15. end loop;  
  16. for all b in 1 .. lr_rowid.count  
  17. update  big_employee_table  
  18. set  salary = lr_salary ( b )  where  rowid in ( lr_rowid ( b ));  
  19. xit when c%notfound;  
  20. end loop;  
  21. close c;  
  22. commit-- there! not in the loop  
  23. exception  
  24. when others then  
  25. rollback;  
  26. dbms_output.put_line ( sqlerrm );  
  27. end;  
  28.  /  
  29.    
  30.  PL/SQL procedure successfully completed.  
  31.    
  32.  Elapsed: 00:00:02.11  
  33.  SQL>  

Notice that the update completed in 2 seconds! I’ve seen faster but my two-gerbil sandbox machine doesn’t have the power that our newer servers do. The point is that the update was incredibly fast and chewed up only 10% of one core. So, in answer to the question of “how often should I commit?” I say don’t until you absolutely have to


0 0
原创粉丝点击