儘量用簡單的SQL替代PL/SQL邏輯

LuiseDalian發表於2014-01-17
可以將很多複雜的結果集先用WITH的別名定義起來,方便後續呼叫。

點選(此處)摺疊或開啟

  1. drop table t;
  2. drop table t1;
  3. drop table t2;
  4. drop table t3;
  5. CREATE TABLE t AS SELECT object_id, object_name FROM dba_objects;
  6. CREATE TABLE t1 AS SELECT object_id, object_name FROM dba_objects WHERE 1 = 2;
  7. CREATE TABLE t2 AS SELECT object_id FROM t1;
  8. CREATE TABLE t3 AS SELECT object_name FROM t1;

  9. EXECUTE runstats_pkg.rs_start;
  10. INSERT ALL
  11.   WHEN object_id > 5 THEN INTO t1 VALUES(object_id, object_name)
  12.   WHEN object_id <> 2 THEN INTO t2 VALUES(object_id)
  13.   ELSE INTO t3 VALUES(object_name)
  14.   SELECT object_id, object_name FROM t;
  15.   
  16. EXECUTE runstats_pkg.rs_middle;

  17. --用自己的邏輯實現插入
  18. BEGIN
  19.   INSERT INTO t1 SELECT object_id, object_name FROM t WHERE object_id > 5;
  20.   INSERT INTO t2 SELECT object_id FROM t WHERE object_id <> 2;
  21.   INSERT INTO t3 SELECT object_name FROM t WHERE object_id <= 5 AND object_id = 2;
  22.   COMMIT;
  23. END;
  24. /

  25. EXECUTE runstats_pkg.rs_stop(1000);

  26. Run1 ran in 2265hsec
  27. Run2 ran in 2270hsec
  28. run 1 ran in 99.78% of the time

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17013648/viewspace-1072074/,如需轉載,請註明出處,否則將追究法律責任。

相關文章