熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

臨時表更適合做插入和查詢操作

2022-06-13   來源: Oracle 

  ORACLE數據庫除了可以保存永久表外還可以建立臨時表temporary tables這些臨時表用來保存一個會話SESSION的數據或者保存在一個事務中需要的數據當會話退出或者用戶提交commit和回滾rollback事務的時候臨時表的數據自動清空(truncate)但是臨時表的結構以及元數據還存儲在用戶的數據字典中
  
  簡介
  
  ORACLE數據庫除了可以保存永久表外還可以建立臨時表temporary tables這些臨時表用來保存一個會話SESSION的數據或者保存在一個事務中需要的數據當會話退出或者用戶提交commit和回滾rollback事務的時候臨時表的數據自動清空(truncate)但是臨時表的結構以及元數據還存儲在用戶的數據字典中
  
  In addition to permanent tables Oracle can create temporary tables to hold sessionprivate data that exists only for the duration of a transaction or session
  
  Temporary tables are supported by Oraclei and Oraclei
  
  詳細介紹
  
  Oracle臨時表分為 會話級臨時表 和 事務級臨時表
  
  會話級臨時表是指臨時表中的數據只在會話生命周期之中存在當用戶退出會話結束的時候Oracle自動清除臨時表中數據
  
  事務級臨時表是指臨時表中的數據只在事務生命周期中存在當一個事務結束(commit or rollback)Oracle自動清除臨時表中數據
  
  臨時表中的數據只對當前Session有效每個Session都有自己的臨時數據並且不能訪問其它Session的臨時表中的數據因此臨時表不需要DML鎖
  
  The CREATE GLOBAL TEMPORARY TABLE statement creates a temporary table that can be transactionspecific or sessionspecific For transactionspecific temporary tables data exists for the duration of the transaction For sessionspecific temporary tables data exists for the duration of the session Data in a temporary table is private to the session Each session can only see and modify its own data DML locks are not acquired on the data of the temporary tables The LOCK statement has no effect on a temporary table because each session has its own private data
  
  DML操作的臨時表不產生redo log重作日志但會產生回滾日志Undo logUndo的產生(rollback segment)會產生Redo log
  
  DML statements on temporary tables do not generate redo logs for the data changes However undo logs for the data and redo logs for the undo logs are generated
  
  當一個會話結束(用戶正常退出 用戶不正常退出 ORACLE實例崩潰)或者一個事務結束的時候Oracle對這個會話的表執行 TRUNCATE 語句清空臨時表數據但不會清空其它會話臨時表中的數據
  
  A TRUNCATE statement issued on a sessionspecific temporary table truncates data in its own session It does not truncate the data of other sessions that are using the same table
  
  DML statements on temporary tables do not generate redo logs for the data changes However undo logs for the data and redo logs for the undo logs are generated Data from the temporary table is automatically dropped in the case of session termination either when the user logs off or when the session terminates abnormally such as during a session or instance failure
  
  你可以索引臨時表和在臨時表基礎上建立視圖同樣建立在臨時表上的索引也是臨時的也是只對當前會話或者事務有效  臨時表可以擁有觸發器
  
  You can create indexes for temporary tables using the CREATE INDEX statement Indexes created on temporary tables are also temporary and the data in the index has the same session or transaction scope as the data in the temporary table
  
  You can create views that access both temporary and permanent tables You can also create triggers on temporary tables
  
  空間分配Segment Allocation(v$sort_usage)
  
  Temporary tables use temporary segments Unlike permanent tables temporary tables and their indexes do not automatically allocate a segment when they are created Instead segments are allocated when the first INSERT (or CREATE TABLE AS SELECT) is performed This means that if a SELECT UPDATE or DELETE is performed before the first INSERT then the table appears to be empty
  
  Temporary segments are deallocated at the end of the transaction for transactionspecific temporary tables and at the end of the session for sessionspecific temporary tables
  
  臨時表在一些版本中存在BUG可能產生過多的REDO LOGeygle 的站點
  
  建立臨時表
  
  臨時表的定義對所有會話SESSION都是可見的但是表中的數據只對當前的會話或者事務有效
  
  建立方法:
  
  ) ON COMMIT DELETE ROWS 定義了建立事務級臨時表的方法
  
  CREATE GLOBAL TEMPORARY TABLE admin_work_area
  
  (startdate DATE
  
  enddate DATE
  
  class CHAR())
  
  ON COMMIT DELETE ROWS;
  
  EXAMPLE:
  
  SQL> CREATE GLOBAL TEMPORARY TABLE admin_work_area
  
       (startdate DATE
  
        enddate DATE
  
        class CHAR())
  
      ON COMMIT DELETE ROWS;
  
  SQL> create table permernate( a number);
  
  SQL> insert into admin_work_area values(sysdatesysdatetemperary table);
  
  SQL> insert into permernate values();
  
  SQL> commit;
  
  SQL> select * from admin_work_area;
  
  SQL> select * from permernate;
  
  A
  
  
  
  )ON COMMIT PRESERVE ROWS 定義了創建會話級臨時表的方法
  
  CREATE GLOBAL TEMPORARY TABLE admin_work_area
  
  (startdate DATE
  
  enddate DATE
  
  class CHAR())
  
  ON COMMIT PRESERVE ROWS;
  
  EXAMPLE:
  
  會話:
  
  SQL> drop table admin_work_area;
  
  SQL> CREATE GLOBAL TEMPORARY TABLE admin_work_area
  
       (startdate DATE
  
        enddate DATE
  
        class CHAR())
  
      ON COMMIT PRESERVE ROWS;
  
  SQL> insert into permernate values();
  
  SQL> insert into admin_work_area values(sysdatesysdatesession temperary);
  
  SQL> commit;
  
  SQL> select * from permernate;
  
  A
  
  
  
  
  
  SQL> select * from admin_work_area;
  
  STARTDATE ENDDATE  CLASS
  
  ?? ?? session temperary
  
  會話:
  
  SQL> select * from permernate;
  
  A
  
  
  
  
  
  SQL> select * from admin_work_area;
  
  未選擇行
  
  會話看不見會話中臨時表的數據
  
  temporary tables but they have to support
  
  a) read consistency
  b) rollback
  c) rollback to savepoint_P_DISPLAYIDF_P_CRITERIA:
  
  they definitely totally definitely generate UNDO
  
  consider:
  
  ops$tkyte@ORAIUTF> create global temporary table t ( x int ) on commit preserve
  rows;
  Table created
  ops$tkyte@ORAIUTF> insert into t values ( );
   row created
  ops$tkyte@ORAIUTF> variable x refcursor
  ops$tkyte@ORAIUTF> exec open :x for select * from t;
  PL/SQL procedure successfully completed
  
  that is (as always) a read consitent result set it is preordained we
  havent fetched a single row of data yet NO IO HAS BEEN performed the result
  set is not copied somewhere just like any other query
  
  
  ops$tkyte@ORAIUTF> savepoint foo;
  Savepoint created
  ops$tkyte@ORAIUTF> insert into t values ( );
   row created
  ops$tkyte@ORAIUTF> select * from t;
  X
  
  
  
  
  that shows we can see two rows but
  ops$tkyte@ORAIUTF> print x
  X
  
  
  
  that query used UNDO to rol
From:http://tw.wingwit.com/Article/program/Oracle/201311/18639.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.