[20210426]execute immediate.txt

lfree發表於2021-04-27

[20210426]execute immediate.txt

--//以前給別人講解繫結變數的好處,總是寫一個指令碼對比使用繫結變數與非繫結變數的好處,基本會使用execute immediate,
--//因為需要比較,總不能非繫結變數的使用execute immediate來拚接sql語句,另外一個不使用execute immediate.例子:
execute immediate 'select pad from t where id = ' || i into v_pad;
execute immediate 'select pad from t where id = :j ' into v_pad using  i  ;
--//這樣編寫測試至少公正一些.
--//今天比較兩種方式的區別,實際上測試的目的主要是我發現生產系統的一個函式,裡面全部sql語句都是execute immediate.
--//不知道開發為什麼這麼寫,從我某種意義講我覺得程式設計者可能第一次寫PL/SQL指令碼或者不經常寫.實際上我也屬於不會寫的那類^_^.
--//也測試看看效能是否存在差距.

1.環境:
SCOTT@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> create table job_times (sid number, time_ela number,method varchar2(20));
Table created.

SCOTT@book> create table t as select rownum id ,cast('test' as varchar2(10)) name from dual ;
Table created.

SCOTT@book> alter table t modify ( id  not null );
Table altered.
--//分析表略。
create unique index i_t_id on t(id);

2.建立測試指令碼:
$ cat m11.txt
set verify off
host sleep $(echo &&3/150 | bc -l )
variable vmethod varchar2(20);
exec :vmethod := '&&2';
insert into job_times values ( sys_context ('userenv', 'sid') ,dbms_utility.get_time ,:vmethod) ;
commit ;
declare
v_id number;
v_d date;
begin
    for i in 1 .. &&1 loop
        select /*+ index(t) &&3 */ count (name) into v_id from t where id=1;
        --execute immediate 'select /*+ Index(t) &&3 */ count (name) from t where id=1' into v_id;
        --execute immediate 'select /*+ INdex(t) &&3 */ count (name) from t where id='||i into v_id;
    end loop;
end ;
/
update job_times set time_ela = dbms_utility.get_time - time_ela where sid=sys_context ('userenv', 'sid') and method=:vmethod;
commit;
quit

3.單個程式測試:
--//分別測試3中情況。
$ sqlplus -s -l scott/book @m11.txt 1e6 p1=1 1 >/dev/null
$ sqlplus -s -l scott/book @m11.txt 1e6 p1exe=1 1 >/dev/null
$ sqlplus -s -l scott/book @m11.txt 1e5 p1exe_nobind=1 1 >/dev/null
--//注:我也順便測試使用非繫結變數的情況,不過減少了迴圈測試1e5.不然太浪費時間.

--//測試結果如下:
SCOTT@book> Select method,count(*),round(avg(TIME_ELA),0),sum(TIME_ELA) from scott.job_times group by method order by 3 ;
METHOD                 COUNT(*) ROUND(AVG(TIME_ELA),0) SUM(TIME_ELA)
-------------------- ---------- ---------------------- -------------
p1=1                          1                   2399          2399
p1exe=1                       1                   3130          3130
p1exe_nobind=1                1                   5532          5532
--//非繫結變數不再說明,本來就很慢,可以發現execute immediate的呼叫比直接select的要很慢。

4.多個程式測試:
$ seq 150 | xargs -I{} -P 150 sqlplus -s -l scott/book @m11.txt 2e5 p2=150 {} >/dev/null
$ seq 150 | xargs -I{} -P 150 sqlplus -s -l scott/book @m11.txt 2e5 p2exe=150 {} >/dev/null
--//測試迴圈測試設定2e5,不然時間有點長。

SCOTT@book> Select method,count(*),round(avg(TIME_ELA),0),sum(TIME_ELA) from scott.job_times group by method order by 3 ;
METHOD                 COUNT(*) ROUND(AVG(TIME_ELA),0) SUM(TIME_ELA)
-------------------- ---------- ---------------------- -------------
p1=1                          1                   2399          2399
p1exe=1                       1                   3130          3130
p2=150                      150                   4840        725953
p2exe=150                   150                   5387        808032
--//不管那種模式,execute immediate都要慢一點點。

5.繼續分析:
$ zzdate;seq 150 | xargs -I{} -P 150 sqlplus -s -l scott/book @m11.txt 2e5 p3=150 {} >/dev/null;zzdate
trunc(sysdate)+10/24+53/1440+15/86400 == 2021/04/26 10:53:15
trunc(sysdate)+10/24+54/1440+05/86400 == 2021/04/26 10:54:05

SCOTT@book> @ ashtop sql_id,event,p1,p2,p3 1=1 trunc(sysdate)+10/24+53/1440+15/86400 trunc(sysdate)+10/24+54/1440+05/86400
    Total
  Seconds     AAS %This   SQL_ID        EVENT                 P1         P2         P3 FIRST_SEEN          LAST_SEEN
--------- ------- ------- ------------- ------------- ---------- ---------- ---------- ------------------- -------------------
      888    17.8   12% |                             1650815232          1          0 2021-04-26 10:53:15 2021-04-26 10:54:04
       39      .8    1% | bxzr72xbttp9p               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       39      .8    1% | g5jk5qhqz3jt6               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:03
       38      .8    1% | 5drwax8fqfpyu               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       38      .8    1% | ac9zqscdmf9jb               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       38      .8    1% | b1vwf4qz54n07               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       38      .8    1% | btbnvhwc5wf5v               1650815232          1          0 2021-04-26 10:53:17 2021-04-26 10:54:01
       38      .8    1% | c0pa8271ypyk7               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       38      .8    1% | cqd6a708y6sqv               1650815232          1          0 2021-04-26 10:53:17 2021-04-26 10:54:04
       38      .8    1% | gm6j6ms4v2y2t               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:03
       37      .7    1% | 84ysnr3yh220n               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       37      .7    1% | f4dvfms42fts4               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       36      .7    1% | 1k67m3kp08mwa               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       36      .7    1% | 1typfc22ntsfs               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       36      .7    1% | 1zqghmqsd3dt2               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:03
       36      .7    1% | 5534dccb81upv               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       36      .7    1% | 7f3xr6z4x6x9a               1650815232          1          0 2021-04-26 10:53:17 2021-04-26 10:54:03
       36      .7    1% | 9n8jt2as50a3n               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:03
       36      .7    1% | audf2w5zdrm21               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:02
       36      .7    1% | dfwp1nznbwjth               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:03
       36      .7    1% | dvrxyf0qgdnta               1650815232          1          0 2021-04-26 10:53:17 2021-04-26 10:54:04
       36      .7    1% | fa041482wj3hn               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:00
       36      .7    1% | fgap543ywdvs1               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       36      .7    1% | fhb1kwqmas68w               1650815232          1          0 2021-04-26 10:53:15 2021-04-26 10:54:04
       35      .7    0% | 0hvydr4qq635t               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       35      .7    0% | 12kfrtvckuwzc               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:03
       35      .7    0% | 40nt0nbr0ngw4               1650815232          1          0 2021-04-26 10:53:16 2021-04-26 10:54:04
       35      .7    0% | 4wgdy2gkg9bvn               1650815232          1          0 2021-04-26 10:53:15 2021-04-26 10:54:02
       35      .7    0% | 63b297t1a7xyz               1650815232          1          0 2021-04-26 10:53:17 2021-04-26 10:54:04
       35      .7    0% | 862bh2t0r6rx3               1650815232          1          0 2021-04-26 10:53:17 2021-04-26 10:54:04
30 rows selected.

$ zzdate;seq 150 | xargs -I{} -P 150 sqlplus -s -l scott/book @m11.txt 2e5 p3exe=150 {} >/dev/null;zzdate
trunc(sysdate)+10/24+56/1440+08/86400 == 2021/04/26 10:56:08
trunc(sysdate)+10/24+57/1440+02/86400 == 2021/04/26 10:57:02

SCOTT@book> @ ashtop sql_id,event,p1,p2,p3 1=1 trunc(sysdate)+10/24+56/1440+08/86400 trunc(sysdate)+10/24+57/1440+02/86400
    Total
  Seconds     AAS %This   SQL_ID        EVENT                 P1         P2         P3 FIRST_SEEN          LAST_SEEN
--------- ------- ------- ------------- ------------- ---------- ---------- ---------- ------------------- -------------------
      960    17.8   12% |                             1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       40      .7    1% | 1u8ay9kvzfgcj               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:01
       40      .7    1% | 4y11rcjuavm0r               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:00
       39      .7    1% | 0hraatbh1j21j               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       39      .7    1% | 254r509f0a2w1               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:01
       39      .7    1% | 3ad7rqaftzprw               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:00
       39      .7    1% | 3tmzqan6b7972               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       38      .7    0% | 2frc5m06qgc91               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       38      .7    0% | 2n8yvbqcmcq9r               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:01
       38      .7    0% | 2qpm3g8fnapw3               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:56:58
       38      .7    0% | 38a7p5ttb8ysv               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:00
       38      .7    0% | 3j2t4ghjj62bv               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:01
       38      .7    0% | 50mawfgzdcykd               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:00
       38      .7    0% | 51n9xh10zsr0z               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       38      .7    0% | 5d98bgycmfgvm               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:00
       38      .7    0% | 5wsqzr3d5rr68               1650815232          1          0 2021-04-26 10:56:12 2021-04-26 10:57:00
       38      .7    0% | 6954azkwsfgm3               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:56:56
       38      .7    0% | 72mjmhhhyark4               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       38      .7    0% | 7qa3pvn803j9v               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:00
       38      .7    0% | bsprts61usm05               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       38      .7    0% | cafptwgpk94xn               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       38      .7    0% | d2b0kfdp4qwhy               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:00
       38      .7    0% | fgffh4fnfg8w1               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       38      .7    0% | fzaxunnkyq30p               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       37      .7    0% | 0sg8q2fgms7dn               1650815232          1          0 2021-04-26 10:56:11 2021-04-26 10:57:01
       37      .7    0% | 5768r83v60uvm               1650815232          1          0 2021-04-26 10:56:12 2021-04-26 10:56:58
       37      .7    0% | 5by0swaxv6jrx               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       37      .7    0% | 6x232fpy1ppcq               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:56:59
       37      .7    0% | 77vcnx24zs61w               1650815232          1          0 2021-04-26 10:56:10 2021-04-26 10:57:01
       37      .7    0% | ab5bms77mxkpn               1650815232          1          0 2021-04-26 10:56:12 2021-04-26 10:56:59
30 rows selected.

SCOTT@book> Select method,count(*),round(avg(TIME_ELA),0),sum(TIME_ELA) from scott.job_times group by method order by 3 ;
METHOD                 COUNT(*) ROUND(AVG(TIME_ELA),0) SUM(TIME_ELA)
-------------------- ---------- ---------------------- -------------
p1=1                          1                   2399          2399
p1exe=1                       1                   3130          3130
p3=150                      150                   4831        724649
p2=150                      150                   4840        725953
p3exe=150                   150                   5150        772438
p2exe=150                   150                   5387        808032
6 rows selected.
--//在CPU的使用上增加一點點時間。很奇怪event顯示是null.P1表示什麼。

6.總結:
--//應該該儘量避免使用execute immediate執行sql語句,execute immediate主要用於動態執行的sql語句。


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

相關文章