[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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 函式遞迴與生成式函式遞迴
- JavaScript 中匿名函式的遞迴呼叫JavaScript函式遞迴
- 好程式設計師Python教程系列遞迴函式與匿名函式呼叫程式設計師Python遞迴函式
- 遞迴函式遞迴函式
- 函式表示式–遞迴函式遞迴
- 函式的遞迴函式遞迴
- JavaScript 函式遞迴JavaScript函式遞迴
- php遞迴函式PHP遞迴函式
- 函式之遞迴函式遞迴
- 好程式設計師Python培訓分享Python的遞迴函式與匿名函式呼叫程式設計師Python遞迴函式
- 遞迴函式的理解遞迴函式
- 第 8 節:函式-匿名函式、遞迴函式函式遞迴
- Python 函式進階-遞迴函式Python函式遞迴
- JS函式表示式——函式遞迴、閉包JS函式遞迴
- 1.5.6 python遞迴函式Python遞迴函式
- 13.0、python遞迴函式Python遞迴函式
- day 17 – 1 遞迴函式遞迴函式
- 遞迴函式例項大全遞迴函式
- 遞迴函式-樹形列表遞迴函式
- 初學 PHP 函式的遞迴PHP函式遞迴
- C#語言函式遞迴C#函式遞迴
- GO語言————6.6 遞迴函式Go遞迴函式
- 直觀理解(尾)遞迴函式遞迴函式
- [20120414]儲存過程遞迴呼叫與V$SQL.txt儲存過程遞迴SQL
- 遞迴函式,可變引數列表遞迴函式
- 測開之函式進階· 第1篇《遞迴函式》函式遞迴
- 遞迴尾呼叫優化遞迴優化
- SQL with as 的用法 以及遞迴函式的寫法 遞迴層次查詢SQL遞迴函式
- 課時22:函式:遞迴是神馬函式遞迴
- bilibiliclass10and11_函式遞迴函式遞迴
- 假如我們把函式都改成遞迴...函式遞迴
- sql 函式實現三種父子遞迴SQL函式遞迴
- Javascript的函式呼叫與thisJavaScript函式
- 父類與子類之間靜態函式的呼叫以及函式引數的傳遞函式
- 遞迴、三元表示式、生成式(列表,字典)、匿名函式遞迴函式
- 【C語言】函式的概念和函式的呼叫(引數傳遞)C語言函式
- [20231123]函式與bash shell呼叫.txt函式
- 從漢諾塔遊戲理解python遞迴函式遊戲Python遞迴函式