What is the difference between explicit and implicit cursors in Oracle?

前端 未结 16 1929
温柔的废话
温柔的废话 2020-12-08 07:41

I am a bit rusty on my cursor lingo in PL/SQL. Anyone know this?

16条回答
  •  春和景丽
    2020-12-08 08:15

    From a performance point of view, Implicit cursors are faster.

    Let's compare the performance between an explicit and implicit cursor:

    SQL> DECLARE
      2    l_loops  NUMBER := 100000;
      3    l_dummy  dual.dummy%TYPE;
      4    l_start  NUMBER;
      5    -- explicit cursor declaration
      6    CURSOR c_dual IS
      7      SELECT dummy
      8      FROM   dual;
      9  BEGIN
     10    l_start := DBMS_UTILITY.get_time;
     11    -- explicitly open, fetch and close the cursor
     12    FOR i IN 1 .. l_loops LOOP
     13      OPEN  c_dual;
     14      FETCH c_dual
     15      INTO  l_dummy;
     16      CLOSE c_dual;
     17    END LOOP;
     18
     19    DBMS_OUTPUT.put_line('Explicit: ' ||
     20                         (DBMS_UTILITY.get_time - l_start) || ' hsecs');
     21
     22    l_start := DBMS_UTILITY.get_time;
     23    -- implicit cursor for loop
     24    FOR i IN 1 .. l_loops LOOP
     25      SELECT dummy
     26      INTO   l_dummy
     27      FROM   dual;
     28    END LOOP;
     29
     30    DBMS_OUTPUT.put_line('Implicit: ' ||
     31                         (DBMS_UTILITY.get_time - l_start) || ' hsecs');
     32  END;
     33  /
    Explicit: 332 hsecs
    Implicit: 176 hsecs
    
    PL/SQL procedure successfully completed.
    

    So, a significant difference is clearly visible. Implicit cursor is much faster than an explicit cursor.

    More examples here.

提交回复
热议问题