日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Oracle 变量绑定与变量窥视合集系列一

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Oracle 变量绑定与变量窥视合集系列一 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Oracle?變量綁定與變量窺視合集》

數據庫環境

LEO1@LEO1> select * from v$version;

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

PL/SQL Release 11.2.0.1.0 - Production

CORE? ? 11.2.0.1.0? ?? ?Production

TNS for Linux: Version 11.2.0.1.0 - Production

NLSRTL Version 11.2.0.1.0 - Production


?

?用示例說明綁定變量的應用領域是OLTP而不是OLAP

變量綁定:這是一個老生常談的話題,我所理解的綁定就是執行計劃的綁定,我所理解的變量就是謂詞替換的變量。

變量綁定機制:要說機制不得不說一下SQL執行的過程,三部曲:解析?–>?執行?->?取操作,而綁定變量就發生在解析這一步,而解析又分成硬解析和軟解析。

硬解析:當一條SQL語句第一次執行時,首先生成執行計劃,并把這個執行計劃存放到shared_pool的library cache中,這個過程叫做硬解析。

軟解析:如果SQL語句已經被硬解析過了,那么可以直接從library cache中抽取現成的執行計劃來重用,這個過程叫做軟解析,目的減少生成執行計劃這方面的資源消耗。為什么這么說呢,硬解析會消耗一些系統資源,尤其是CPU的資源,從而影響系統的效率,如果能把這方面的影響消除,那么對系統當然是多多益善了,哈?多侃了幾句。

SQL詳細執行過程:當oracle接收到一條sql語句時,首先會把這條sql語句字符做成哈希值,然后到library cache中尋找是否有和這個哈希值相匹配的sql存在,如果有就直接使用這個sql的執行計劃去執行當前的sql語句,最后將結果返回給用戶。如果沒有找到相同的哈希值,oracle會認為這是一條新的sql,將會重新生成執行計劃來執行(在這個過程中先要檢查語法分析和語義分析),最后將結果返回給用戶。

實驗

下面我們演示一下綁定變量和非綁定變量在資源消耗上的差異

LEO1@LEO1> drop table leo1 purge;? ?? ?? ?? ?? ??清理環境

Table dropped.

LEO1@LEO1> drop table leo2 purge;? ?? ?? ?? ?? ?

Table dropped.

LEO1@LEO1> create table leo1 as select * from dba_objects;? ??創建leo1

Table created.

LEO1@LEO1> create table leo2 as select * from dba_objects;? ??創建leo2

Table created.

LEO1@LEO1> alter session set tracefile_identifier='bind_variable';? ??設置trace文件標識

Session altered.

LEO1@LEO1> alter session set sql_trace=true;? ?? ?啟動trace功能,追蹤sql資源消耗情況

Session altered.

LEO1@LEO1> begin

for i in 1..100 loop

execute immediate 'select * from leo1 where object_id=:i' using i;

end loop;

end;

/

PL/SQL procedure successfully completed.

我們對一條sql執行了100次并采用了綁定變量技術,oracle對這條sql只有一次硬解析,沒有軟解析,反復執行100次。

LEO1@LEO1> alter session set sql_trace=false;? ?? ?? ???關閉trace功能

Session altered.

LEO1@LEO1> select sql_text,parse_calls,loads,executions from v$sql where sql_text like 'select * from leo1 where %';

SQL_TEXT? ?? ?? ?? ?? ?? ?? ?? ?? ???PARSE_CALLS??LOADS? ?EXECUTIONS

-------------------------------------------------- ----------- -------------------------- ----------- ---------- ------------------

select * from leo1 where object_id=:i? ?? ?? ?1? ?? ?? ? 1? ?? ???100

SQL_TEXT:我們跟蹤的sql語句

PARSE_CALLS:硬解析+軟解析次數? ?? ? 1次? ?只有硬解析沒有軟解析

LOADS:硬解析次數? ?? ?? ?? ?? ?? ???1次

EXECUTIONS:執行次數? ?? ?? ?? ?? ???100次

雖說值隱藏在變量中,但在解析環節oracle認為是一樣的

[oracle@leonarding1 trace]$ tkprof LEO1_ora_16433_bind_variable.trc bind_variable.txt sys=no

TKPROF: Release 11.2.0.1.0 - Development on Fri Feb 1 13:18:08 2013

Copyright (c) 1982, 2009, Oracle and/or its affiliates.??All rights reserved.

使用tkprof工具過濾和匯總trace文件的,sys=no?不輸出sys用戶遞歸語句,默認yes,實際上設置成no更具有可讀性

---------?下面是bind_variable.txt文件信息

********************************************************************************

SQL ID: 0b74y9utb0b6r? ?? ?? ?? ? #這就是SQL語句字符的哈希值

Plan Hash: 2716644435

select *

from

leo1 where object_id=:i

call? ???count? ?? ? cpu? ? elapsed? ?? ? disk? ?? ?query? ? current? ?? ???rows

------- ------??-------- ---------- ---------- ---------- ----------??----------

Parse? ?? ???1? ?? ?0.00? ?? ? 0.00? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ???0

Execute? ??100?? ???0.01? ?? ? 0.01? ?? ?? ? 0? ?? ?? ? 1? ?? ?? ? 0? ?? ?? ???0

Fetch? ?? ???0? ?? ?0.00? ?? ? 0.00? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ???0

------- ------??-------- ---------- ---------- ---------- ----------??----------

total? ?? ?101? ?? ?0.01? ?? ? 0.01? ?? ?? ? 0? ?? ?? ? 1? ?? ?? ? 0? ?? ?? ???0

Misses in library cache during parse: 1? ?? ???只有1次硬解析,反復執行100次

Optimizer mode: ALL_ROWS

Parsing user id: 85? ???(recursive depth: 1)

Rows? ???Row Source Operation

-------??---------------------------------------------------

? ?? ?0??TABLE ACCESS FULL LEO1 (cr=0 pr=0 pw=0 time=0 us cost=288 size=2484 card=12)

********************************************************************************

下面是一個非綁定變量的sql執行情況

LEO1@LEO1> alter session set sql_trace=true;? ?? ?? ?? ???啟動trace功能

Session altered.

LEO1@LEO1> begin

for i in 1..100 loop

execute immediate 'select * from leo2 where object_id='||i;

end loop;

end;

/

PL/SQL procedure successfully completed.

我們對一條sql執行了100次沒有采用綁定變量技術,oracle對這條sql要硬解析100次,執行100次,資源嚴重被sql解析所消耗,系統顯得緩慢不堪。

LEO1@LEO1> alter session set sql_trace=false;? ?? ?? ?? ??關閉trace功能

Session altered.

LEO1@LEO1> select sql_text,parse_calls,loads,executions from v$sql where sql_text like 'select * from leo2 where %' order by 1;

SQL_TEXT? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? PARSE_CALLS? ?? ?LOADS EXECUTIONS

-------------------------------------------------- ----------- ---------- ----------

select * from leo2 where object_id=1? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=10? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=100? ?? ?? ?? ?? ?? ?? ???1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=11? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=12? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=13? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=14? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=15? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=16? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=17? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=18? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=19? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=2? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=20? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=21? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=22? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=23? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=24? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=25? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=26? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=27? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=28? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=29? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=3? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=30? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=31? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=32? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=33? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=34? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=35? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=36? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=37? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=38? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=39? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=4? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=40? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=41? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=42? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=43? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=44? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=45? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=46? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=47? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=48? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=49? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=5? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=50? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=51? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=52? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=53? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=54? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=55? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=56? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=57? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=58? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=59? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=6? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=60? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=61? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=62? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=63? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=64? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=65? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=66? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=67? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=68? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=69? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=7? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=70? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=71? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=72? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=73? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=74? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=75? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=76? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=77? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=78? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=79? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=8? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=80? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=81? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=82? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=83? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=84? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=85? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=86? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=87? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=88? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=89? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=9? ?? ?? ?? ?? ?? ?? ?? ? 1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=90? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=91? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=92? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=93? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=94? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=95? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=96? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=97? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=98? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

select * from leo2 where object_id=99? ?? ?? ?? ?? ?? ?? ?? ?1? ?? ?? ? 1? ?? ?? ? 1

100 rows selected.

我們從動態性能視圖上可以看出oracle每執行一次sql,都要先硬解析1次之后在執行。這種沒有使用綁定變量技術在硬解析消耗上就比使用綁定變量技術多損耗100倍,如果執行的次數上萬?上億對系統性能的影響可想而知。

---------?我們來看看trace文件的內容

[oracle@leonarding1 trace]$ tkprof LEO1_ora_16433_bind_variable.trc bind_variable.txt sys=no

TKPROF: Release 11.2.0.1.0 - Development on Fri Feb 1 13:49:52 2013

Copyright (c) 1982, 2009, Oracle and/or its affiliates.??All rights reserved.

********************************************************************************

SQL ID: 22r47f3t6w0td

Plan Hash: 2258638698

select *

from

leo2 where object_id=1

call? ???count? ?? ? cpu? ? elapsed? ?? ? disk? ?? ?query? ? current? ?? ???rows

------- ------??-------- ---------- ---------- ---------- ----------??----------

Parse? ?? ???1? ?? ?0.00? ?? ? 0.00? ?? ?? ? 0? ?? ?? ? 1? ?? ?? ? 0? ?? ?? ???0

Execute? ?? ?1? ?? ?0.00? ?? ? 0.00? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ???0

Fetch? ?? ???0? ?? ?0.00? ?? ? 0.00? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ? 0? ?? ?? ???0

------- ------??-------- ---------- ---------- ---------- ----------??----------

total? ?? ???2? ?? ?0.00? ?? ? 0.00? ?? ?? ? 0? ?? ?? ? 1? ?? ?? ? 0? ?? ?? ???0

Misses in library cache during parse: 1

Optimizer mode: ALL_ROWS

Parsing user id: 85? ???(recursive depth: 1)

Rows? ???Row Source Operation

-------??---------------------------------------------------

? ?? ?0??TABLE ACCESS FULL LEO2 (cr=0 pr=0 pw=0 time=0 us cost=288 size=2484 card=12)

********************************************************************************

SQL ID: 9nb3n54fy0z8m

Plan Hash: 2258638698

select *

from

leo2 where object_id=2

********************************************************************************

SQL ID: 8mc705qymd7qs

Plan Hash: 2258638698

select *

from

leo2 where object_id=3

如上所示每個sql語句的SQL_ID都是不一樣的,都是相對獨立的,因此每執行1次就要解析1次,兩種情況對比結果顯示,綁定變量要比沒有綁定變量消耗的資源少的少,sql執行的次數越多,這種效果越明顯。所以我們說綁定變量本質就是用一個變量來代替謂詞常量,讓oracle只需要硬解析一次,后續sql都直接使用之前執行計劃來執行,這樣就省卻了很消耗資源的硬解析過程

下面討論綁定變量為什么適合于OLTP而不是OLAP

OLTP

1.適合OLTP系統架構

2.SQL簡單非常相似,結果集非常小,例如?只有謂詞部分不同,余下部分全部相同的SQL語句,這種情況下執行計劃都是一樣的,在執行計劃幾乎不變的情況下,oracle使用變量來代替謂詞常量,使用同一個執行計劃是非常合理的

3.SQL重復率很高,或者只有謂詞條件不同而已

4.DML操作頻繁

5.SQL語句執行條數多,條數越多減少硬解析越有意義

6.基于主鍵做查詢,還有等值查詢,唯一性查詢,這類查詢相對適合綁定變量

select? ?*? ?from??leonarding? ?where? ?id=:leo;

?

OLAP

1.不適合OLAP系統架構

2.SQL的執行計劃多變,會因為值的不同導致執行計劃的不同,可能第一次執行是一種執行計劃,第二次執行是另一種執行計劃,所以不適合進行綁定變量操作,會讓oracle盲目浪費大量資源消耗,SQL語句即使只有謂詞條件不同,oracle應然可能采取不同的執行計劃。

3.SQL重復率較低,大部分都是批量加載批量檢索的操作

4.數據聚合操作頻繁

5.SQL語句執行條數少,SQL硬解析對系統性能影響較小,綁定沒有意義

6.分區表相對不太適合綁定變量技術


?本文轉自 leonarding151CTO博客,原文鏈接:http://blog.51cto.com/leonarding/1131507,如需轉載請自行聯系原作者


創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Oracle 变量绑定与变量窥视合集系列一的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。