[20180531]函式呼叫與遞迴.txt
[20180531]函式呼叫與遞迴.txt
--//前幾天我發現開發把一個計算公式建立為函式,我本想這樣不好,因為執行select f(x)...,的每行都會呼叫自定義的函式.
--//我本以為這樣會產生大量的遞規呼叫.但是還是有點吃驚,如果函式里面沒有一些select語句,這樣的呼叫自定義函式,不會
--//產生遞規,當然我個人這樣還是不提倡這樣的方式.
1.環境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
grant execute on sys.dbms_lock to scott;
CREATE OR REPLACE FUNCTION sleep (seconds IN NUMBER)
RETURN NUMBER AS
BEGIN
--// sys.dbms_lock.sleep(seconds);
RETURN seconds;
END;
/
2.測試1:
set autot traceonly
select rowid,dept.*,sleep(1) from dept ;
set autot off
SCOTT@test01p> select rowid,dept.*,sleep(1) from dept ;
Execution Plan
---------------------------
Plan hash value: 3383998547
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1021 bytes sent via SQL*Net to client
544 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
--//可以發現沒有遞迴,如果第一次存在遞迴,多執行計劃在檢視.(以下測試採用相似的方式).
SCOTT@test01p> set autot off
3.測試2:
--//如果修改如下:
CREATE OR REPLACE FUNCTION sleep (seconds IN NUMBER)
RETURN NUMBER AS
BEGIN
sys.dbms_lock.sleep(seconds);
RETURN seconds;
END;
/
set autot traceonly
select rowid,dept.*,sleep(1) from dept ;
set autot off
SCOTT@test01p> select rowid,dept.*,sleep(1) from dept ;
Execution Plan
----------------------------------------------------------
Plan hash value: 3383998547
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1021 bytes sent via SQL*Net to client
544 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
--//可以發現呼叫sys.dbms_lock.sleep也沒有出現遞迴.
4.測試3:
--//如果修改如下:
CREATE OR REPLACE FUNCTION sleep (seconds IN NUMBER)
RETURN NUMBER
is
d_date date;
BEGIN
select sysdate into d_date from dual;
RETURN seconds;
END;
/
set autot traceonly
select rowid,dept.*,sleep(1) from dept ;
set autot off
SCOTT@test01p> select rowid,dept.*,sleep(1) from dept ;
Execution Plan
----------------------------------------------------------
Plan hash value: 3383998547
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1021 bytes sent via SQL*Net to client
544 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
--//可以發現只有在函式里面執行sql語句,才存在遞迴呼叫.
--//前幾天我發現開發把一個計算公式建立為函式,我本想這樣不好,因為執行select f(x)...,的每行都會呼叫自定義的函式.
--//我本以為這樣會產生大量的遞規呼叫.但是還是有點吃驚,如果函式里面沒有一些select語句,這樣的呼叫自定義函式,不會
--//產生遞規,當然我個人這樣還是不提倡這樣的方式.
1.環境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
grant execute on sys.dbms_lock to scott;
CREATE OR REPLACE FUNCTION sleep (seconds IN NUMBER)
RETURN NUMBER AS
BEGIN
--// sys.dbms_lock.sleep(seconds);
RETURN seconds;
END;
/
2.測試1:
set autot traceonly
select rowid,dept.*,sleep(1) from dept ;
set autot off
SCOTT@test01p> select rowid,dept.*,sleep(1) from dept ;
Execution Plan
---------------------------
Plan hash value: 3383998547
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1021 bytes sent via SQL*Net to client
544 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
--//可以發現沒有遞迴,如果第一次存在遞迴,多執行計劃在檢視.(以下測試採用相似的方式).
SCOTT@test01p> set autot off
3.測試2:
--//如果修改如下:
CREATE OR REPLACE FUNCTION sleep (seconds IN NUMBER)
RETURN NUMBER AS
BEGIN
sys.dbms_lock.sleep(seconds);
RETURN seconds;
END;
/
set autot traceonly
select rowid,dept.*,sleep(1) from dept ;
set autot off
SCOTT@test01p> select rowid,dept.*,sleep(1) from dept ;
Execution Plan
----------------------------------------------------------
Plan hash value: 3383998547
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1021 bytes sent via SQL*Net to client
544 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
--//可以發現呼叫sys.dbms_lock.sleep也沒有出現遞迴.
4.測試3:
--//如果修改如下:
CREATE OR REPLACE FUNCTION sleep (seconds IN NUMBER)
RETURN NUMBER
is
d_date date;
BEGIN
select sysdate into d_date from dual;
RETURN seconds;
END;
/
set autot traceonly
select rowid,dept.*,sleep(1) from dept ;
set autot off
SCOTT@test01p> select rowid,dept.*,sleep(1) from dept ;
Execution Plan
----------------------------------------------------------
Plan hash value: 3383998547
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1021 bytes sent via SQL*Net to client
544 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
--//可以發現只有在函式里面執行sql語句,才存在遞迴呼叫.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2155374/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 函式遞迴與生成式函式遞迴
- 好程式設計師Python教程系列遞迴函式與匿名函式呼叫程式設計師Python遞迴函式
- [20231123]函式與bash shell呼叫.txt函式
- 遞迴函式遞迴函式
- 好程式設計師Python培訓分享Python的遞迴函式與匿名函式呼叫程式設計師Python遞迴函式
- 函式表示式–遞迴函式遞迴
- 函式之遞迴函式遞迴
- 函式的遞迴函式遞迴
- Python 函式進階-遞迴函式Python函式遞迴
- 20201023_081.遞迴函式_函式呼叫記憶體分析_棧幀的建立遞迴函式記憶體
- 遞迴函式的理解遞迴函式
- 1.5.6 python遞迴函式Python遞迴函式
- 13.0、python遞迴函式Python遞迴函式
- 第 8 節:函式-匿名函式、遞迴函式函式遞迴
- JS函式表示式——函式遞迴、閉包JS函式遞迴
- 遞迴呼叫 VS 迴圈呼叫遞迴
- 遞迴函式-樹形列表遞迴函式
- 遞迴函式例項大全遞迴函式
- day 17 – 1 遞迴函式遞迴函式
- [20190401]關於semtimedop函式呼叫.txt函式
- bilibiliclass10and11_函式遞迴函式遞迴
- 初學 PHP 函式的遞迴PHP函式遞迴
- GO語言————6.6 遞迴函式Go遞迴函式
- C#語言函式遞迴C#函式遞迴
- 遞迴函式,可變引數列表遞迴函式
- 測開之函式進階· 第1篇《遞迴函式》函式遞迴
- [20190402]關於semtimedop函式呼叫2.txt函式
- 普通函式與函式模板呼叫規則函式
- 編寫函式:遞迴求逆序 (Append Code) ★函式遞迴APP
- 假如我們把函式都改成遞迴...函式遞迴
- 課時22:函式:遞迴是神馬函式遞迴
- (011)我們一起學Python;匿名函式,遞迴函式Python函式遞迴
- 什麼是遞迴呼叫遞迴
- 遞迴尾呼叫優化遞迴優化
- 尾呼叫和尾遞迴遞迴
- 【C語言】函式的概念和函式的呼叫(引數傳遞)C語言函式
- 普通函式與函式模板呼叫規則2函式
- 函式呼叫與空間分配函式