Local Temporary table in Oracle 10 (for the scope of Stored Procedure)

前端 未结 4 429
小鲜肉
小鲜肉 2020-11-30 15:24

I am new to oracle. I need to process large amount of data in stored proc. I am considering using Temporary tables. I am using connection pooling and the application is mult

4条回答
  •  情书的邮戳
    2020-11-30 16:29

    In Oracle, it's almost never necessary to create objects at runtime.

    Global Temporary Tables are quite possibly the best solution for your problem, however since you haven't said exactly why you need a temp table, I'd suggest you first check whether a temp table is necessary; half the time you can do with one SQL what you might have thought would require multiple queries.

    That said, I have used global temp tables in the past quite successfully in applications that needed to maintain a separate "space" in the table for multiple contexts within the same session; this is done by adding an additional ID column (e.g. "CALL_ID") that is initially set to 1, and subsequent calls to the procedure would increment this ID. The ID would necessarily be remembered using a global variable somewhere, e.g. a package global variable declared in the package body. E.G.:

    PACKAGE BODY gtt_ex IS
       last_call_id integer;
       PROCEDURE myproc IS
          l_call_id integer;
       BEGIN
          last_call_id := NVL(last_call_id, 0) + 1;
          l_call_id      := last_call_id;
          INSERT INTO my_gtt VALUES (l_call_id, ...);
          ...
          SELECT ... FROM my_gtt WHERE call_id = l_call_id;
       END;
    END;
    

    You'll find GTTs perform very well even with high concurrency, certainly better than using ordinary tables. Best practice is to design your application so that it never needs to delete the rows from the temp table - since the GTT is automatically cleared when the session ends.

提交回复
热议问题