[20180607]函式與標量子查詢8.txt
[20180607]函式與標量子查詢8.txt
--//前面看http://www.cnblogs.com/kerrycode/p/9099507.html連結,裡面提到:
通俗來將,當使用標量子查詢的時候,ORACLE會將子查詢結果快取在雜湊表中, 如果後續的記錄出現同樣的值,最佳化器透過快取在雜湊
表中的值,判斷重複值不用重複呼叫函式,直接使用上次計算結果即可。從而減少呼叫函式次數,從而達到最佳化效能的效果。另外在
ORACLE 10和11中, 雜湊表只包含了255個Buckets,也就是說它能儲存255個不同值,如果超過這個範圍,就會出現雜湊衝突,那些出現
雜湊衝突的值就會重複呼叫函式,即便如此,依然能達到大幅改善效能的效果。
--//昨天我測試11.2.0.4 for linux下,雜湊表不止255個Buckets.
--//今天測試看看10g下到底有多少個Buckets.因為我感覺10g可能雜湊表的buckets可能不大.
1.環境:
SYS@test> @ &r/ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- ----------------------------------------------------------------
x86_64/Linux 2.4.xx 10.2.0.4.0 Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
grant execute on sys.dbms_lock to scott;
CREATE OR REPLACE FUNCTION sleep1 (seconds IN NUMBER)
RETURN NUMBER
is
d_date date;
BEGIN
select sysdate into d_date from dual;
sys.dbms_lock.sleep(seconds/10);
RETURN seconds;
END;
/
CREATE OR REPLACE FUNCTION sleep (seconds IN NUMBER)
RETURN NUMBER
is
d_date date;
BEGIN
select sysdate into d_date from dual;
--//sys.dbms_lock.sleep(0.01);
RETURN seconds;
END;
/
create table t as select rownum id1,mod(rownum-1,4000)+1 id2 from dual connect by level<=8000;
--//ALTER TABLE t MINIMIZE RECORDS_PER_BLOCK ;
--//注意插入資料的順序,我以前的插入有1點問題,導致id2顯示不按照1-4000,1-4000顯示(執行select * from t).
--//導致測試出現一些奇怪情況.
2.測試:
SELECT 'exec :x := '
|| LEVEL
|| ';'
|| CHR (10)
|| 'select rowid,t.*,(select sleep(id2) from dual) s from t where id2<=:x;'
|| CHR (10)
|| '@ &r/dpc '''' '''''
FROM DUAL
CONNECT BY LEVEL <= 4000;
--//建立指令碼ay.txt:
alter session set statistics_level=all;
set feed on
variable x number;
exec :x := 1;
select rowid,t.*,(select sleep(id2) from dual) s from t where id2<=:x;
@ http://192.168.100.40/sqllaji//dpc '' ''
...
exec :x := 4000;
select rowid,t.*,(select sleep(id2) from dual) s from t where id2<=:x;
@ http://192.168.100.40/sqllaji//dpc '' ''
--//多執行幾次,避免其它遞迴影響.
spool cz.txt
@ ay.txt
spool off
--//取出數字
$ egrep 'FAST DUAL|rows selected' cz.txt | sed '/^29 rows selected./d' > c1.txt
$ grep "rows selected." c1.txt | cut -f1 -d' '> c2.txt
$ grep "FAST DUAL" c1.txt | cut -f5 -d"|" > c3.txt
$ paste c2.txt c3.txt -d"," > c4.txt
SCOTT@book> create table t1 ( b number ,a number);
Table created.
--//注b在前,表示查詢記錄數量,a表示執行fast dual次數,也就是遞迴次數.
--//修改b4.txt ,改寫成inert插入表t1.執行如下:
:%s/^/insert into t1 values(/g
:%s/$/);/g
select max(id2) from (
SELECT id2, r, rp
FROM ( SELECT b / 2 id2, a r, LAG (a) OVER (ORDER BY b) rp
FROM t1
ORDER BY b/2)
WHERE r - rp = 1 order by id2);
MAX(ID2)
----------
3152
--//3152值還會進入backupset,後面的數字帶入都是出現hash 衝突的情況.
SELECT id2, r, rp
FROM ( SELECT b / 2 id2, a r, LAG (a) OVER (ORDER BY b) rp
FROM t1
ORDER BY b/2)
WHERE r - rp >= 2 and id2<=3152 ;
--//輸出太長,一共2640個值,略,這個結果就是在1-3152之間,出現hash衝突的值.
select rowid,t.*,(select sleep(id2) from dual) s from t where id2<=3152
and id2 not in
(
SELECT /*+ NL_AJ */ id2
FROM ( SELECT b / 2 id2, a r, LAG (a) OVER (ORDER BY b) rp
FROM t1
ORDER BY b/2)
WHERE r - rp >= 2 and id2<=3152
);
SCOTT@test> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 7r8dyxjmdwucp, child number 0
-------------------------------------
select rowid,t.*,(select sleep(id2) from dual) s from t where id2<=3152 and id2 not in ( SELECT /*+ NL_AJ */ id2 FROM ( SELECT b / 2
id2, a r, LAG (a) OVER (ORDER BY b) rp FROM t1 ORDER BY b/2) WHERE r - rp >= 2 and id2<=3152 )
Plan hash value: 4130365942
---------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
---------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | FAST DUAL | | 512 | 1 | | 2 (0)| 00:00:01 | 512 |00:00:00.01 | 0 | | | |
| 2 | NESTED LOOPS ANTI | | 1 | 6304 | 283K| 46284 (32)| 00:09:16 | 1024 |00:00:07.65 | 40 | | | |
|* 3 | TABLE ACCESS FULL | T | 1 | 6306 | 44142 | 6 (0)| 00:00:01 | 6304 |00:00:00.01 | 24 | | | |
|* 4 | VIEW | | 6304 | 1 | 39 | 7 (29)| 00:00:01 | 5280 |00:00:07.65 | 16 | | | |
| 5 | SORT ORDER BY | | 6304 | 4000 | 28000 | 7 (29)| 00:00:01 | 4096K|00:00:06.14 | 16 | 160K| 160K| 142K (0)|
| 6 | WINDOW SORT | | 1024 | 4000 | 28000 | 7 (29)| 00:00:01 | 4096K|00:00:04.10 | 16 | 196K| 196K| 174K (0)|
| 7 | TABLE ACCESS FULL| T1 | 1 | 4000 | 28000 | 5 (0)| 00:00:01 | 4000 |00:00:00.01 | 16 | | | |
---------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$2 / DUAL@SEL$2
2 - SEL$8771BF6C
3 - SEL$8771BF6C / T@SEL$1
4 - SEL$4 / from$_subquery$_003@SEL$3
5 - SEL$4
7 - SEL$4 / T1@SEL$4
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("ID2"<=3152)
4 - filter(("ID2"="ID2" AND "R"-"RP">=2 AND "ID2"<=3152))
35 rows selected.
--//正好512,說明10.2.0.4,雜湊表只包含了512個Buckets,也就是說它能儲存512個不同值,
--//刪除衝突的記錄看看.
delete from t where id2 in (SELECT id2
FROM ( SELECT b / 2 id2, a r, LAG (a) OVER (ORDER BY b) rp
FROM t1
ORDER BY b/2)
WHERE r - rp >= 2 and id2<=3152 );
commit;
select rowid,t.*,(select sleep(id2) from dual) s from t where id2<=3152;
SCOTT@test> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 9q5bnk36nnq68, child number 0
-------------------------------------
select rowid,t.*,(select sleep(id2) from dual) s from t where id2<=3152
Plan hash value: 1032660217
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------
| 1 | FAST DUAL | | 512 | 1 | | 2 (0)| 00:00:01 | 512 |00:00:00.01 | 0 |
|* 2 | TABLE ACCESS FULL| T | 1 | 6306 | 44142 | 6 (0)| 00:00:01 | 1024 |00:00:00.01 | 24 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$2 / DUAL@SEL$2
2 - SEL$1 / T@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("ID2"<=3152)
24 rows selected.
--//這樣如果
select rowid,t.*,(select sleep(id2) from dual) s from t where (id2<=3152 and id2<>1693) or id2=:x;
--//:x 選擇 3153-4000 任何一個 ,fast dual 的starts都是514,也就是存在衝突.大家可以自行驗證.
--//dpc指令碼如下:
set verify off
select * from table(dbms_xplan.display_cursor(NVL('&1',NULL),NULL,'ALL ALLSTATS LAST PEEKED_BINDS cost partition -projection -outline &2'));
prompt
prompt argment : typical all advanced partition predicate remote note parallel projection alias peeked_binds outline adaptive
prompt
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2155755/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20180626]函式與標量子查詢14.txt函式
- [20180625]函式與標量子查詢13(補充)函式
- [20180612]函式與標量子查詢10.txt函式
- [20180611]函式與標量子查詢9.txt函式
- [20180602]函式與標量子查詢4.txt函式
- [20180602]函式與標量子查詢3.txt函式
- 20180601]函式與標量子查詢2.txt函式
- [20200325]慎用標量子查詢.txt
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- MYSQL count標量子查詢改left joinMySql
- [20211220]關於標量子查詢問題.txt
- Oracle常用抓取SQL-標量子查詢等OracleSQL
- pytest標記:查詢測試策略、標記測試函式函式
- mysql 標量子查詢和現金盤程式製作非法子查詢MySql
- [20211214]18c標量子查詢unnest.txt
- Solr複雜查詢一:函式查詢Solr函式
- 影響Oracle標量子查詢效能的三個因素Oracle
- PostgreSQL 查詢替換函式SQL函式
- 【TUNE_ORACLE】列出有標量子查詢的SQL參考OracleSQL
- SQL查詢中用到的函式SQL函式
- Java-MySql-函式、多表查詢JavaMySql函式
- GreatSQL 最佳化技巧:將 MINUS 改寫為標量子查詢SQL
- [20210202]計算標量子查詢快取數量2.txt快取
- 二分查詢 - 手寫模板與自帶函式函式
- Vlookup大叔與一對多查詢(Excel函式集團)Excel函式
- webgl內建函式--向量函式與紋理查詢函式Web函式
- [20210201]19c計算標量子查詢快取數量.txt快取
- C++中函式指標與函式物件C++函式指標物件
- c++字串查詢函式實現C++字串函式
- 二分查詢(函式實現)函式
- 二分查詢函式的使用函式
- 【從零開始學習Oracle資料庫】(3)函式與子查詢和連線查詢Oracle資料庫函式
- 【從零開始學習 MySql 資料庫】(3) 函式與子查詢和連線查詢MySql資料庫函式
- PHP 查詢、擷取字串函式詳解PHP字串函式
- PostgreSQL函式:返回表查詢結果集SQL函式
- C++ 逆向之 main 函式的查詢C++AI函式
- 指標函式 和 函式指標指標函式
- MYSQL滿足條件函式里放查詢最大函式的方法MySql函式