Hard Parse&Soft Parse,hardparse

來源:互聯網
上載者:User

Hard Parse&Soft Parse,hardparse

DDL每次執行都需要進行硬解析。

SQL 解析過程

Oracle對此SQL將進行幾個步驟的處理過程:

1、語法檢查(syntax check): 檢查此sql的拼字是否文法。

2、語義檢查(semantic check): 諸如檢查sql語句中的訪問對象是否存在及該使用者是否具備相應的許可權。

3、對sql語句進行解析(prase): 利用內部演算法對sql進行解析,產生解析樹(parse tree)及執行計畫(execution plan)。

4、執行sql,返回結果(execute and return)


5個執行步驟:

1:文法分析

2:許可權與對象檢查

3: 在共用池中檢查是否有完全相同的之前完全解析好的. 如果存在,直接跳過4和5,運行Sql, 此時算soft parse.

4:選擇執行計畫

5:產生執行計畫


3的解釋:

Oracle將會對傳遞進來的SQL語句使用HASH函數運算得出HASH值,再與共用池中現有語句的HASH值進行比較看是否一一對應。現有資料庫中SQL語句的HASH值我們可以通過訪問v$sql、v$sqlarea、v$sqltext等資料字典中的HASH_VALUE列查詢得出。

如果SQL語句的HASH值一致,那麼ORACLE事實上還需要對SQL語句的語義進行再次檢測,以決定是否一致。那麼為什麼Oracle需要再次對語句文本進行檢測呢?不是SQL語句的HASH值已經對應上了?事實上就算是SQL語句的HASH值已經對應上了,並不能說明這兩條SQL語句就已經可以共用了。


Dictionary Cache

The data dictionary is a collection of database tables and views containing reference information about the database, its structures, and its users. Oracle accesses the data dictionary frequently during SQL statement parsing. This access is essential to the continuing operation of Oracle.

The data dictionary is accessed so often by Oracle that two special locations in memory are designated to hold dictionary data. One area is called the data dictionary cache, also known as the row cache because it holds data as rows instead of buffers (which hold entire blocks of data). The other area in memory to hold dictionary data is the library cache. All Oracle user processes share these two caches for access to data dictionary information.


Parsing

Parsing is one stage in the processing of a SQL statement. When an application issues a SQL statement, the application makes a parse call to Oracle. During the parse call, Oracle:

  • Checks the statement for syntactic and semantic validity

  • Determines whether the process issuing the statement has privileges to run it

  • Allocates a private SQL area for the statement

Oracle also determines whether there is an existing shared SQL area containing the parsed representation of the statement in the library cache. If so, the user process uses this parsed representation and runs the statement immediately. If not, Oracle generates the parsed representation of the statement, and the user process allocates a shared SQL area for the statement in the library cache and stores its parsed representation there.

Note the difference between an application making a parse call for a SQL statement and Oracle actually parsing the statement. A parse call by theapplication associates a SQL statement with a private SQL area. After a statement has been associated with a private SQL area, it can be run repeatedly without your application making a parse call. A parse operation by Oracle allocates a shared SQL area for a SQL statement. Once a shared SQL area has been allocated for a statement, it can be run repeatedly without being reparsed.

Both parse calls and parsing can be expensive relative to execution, so perform them as seldom as possible.





java為啥使用 preparestatement麻煩告訴我

其中分析分為硬分析(Hard Parse)和軟分析(Soft Parse)。一條SQL語句通過語法檢查後,Oracle 會先去shared pool 中找是否有相同的sql,如果找著了,就叫軟分析,然後執行SQL語句。硬分析主要是檢查該sql所涉及到的所有對象是否有效以及許可權等關係,然後根據RBO或CBO模式產生執行計畫,然後才執行SQL語句。可以看出,硬分析比軟分析多了很多動作,而這裡面的關鍵是“在shared pool 中是否有相同的sql”,而這就取決於是否使用綁定變數。另:oracle9i引入了soft soft parse,先到pga中的session cursor cache list列表中去尋找(session cursor cache list的長度是由session_cache_cursor參數決定的),如果沒有找到這條sql,這時候才去檢查shard_pool.對於Oltp系統,很多時候硬分析的代價比執行還要高,這個我們可以通過10046事件跟蹤得知。(2)共用池中SQL語句數量太多,重用性極低,加速了SQL語句的老化,導致共用池片段過多。共用池中不同的SQL語句數量巨大,根據LRU原則,一些語句逐漸老化,最終被清理出共用池;這樣就導致shared_pool_size 裡面命中率下降,共用池片段增多,可用記憶體空間不足。而為了維護共用池內部結構,需要使用latch,一種內部生命週期很短的lock,這將使用大量的cpu 資源,使得效能急劇下降。不使用綁定變數違背了oracle 的shared pool 的設計的原則,違背了這個設計用來共用的思想。2、怎麼查看沒有使用綁定變數select * from v$sql or v$sqlarea 查看是否有很多類似的語句,除了變數不一樣,其他的都一樣3、如何使用綁定變數?編寫java 程式時,我們習慣都是定義JAVA 的程式變數,放入SQL 陳述式中,如String v_id = 'xxxxx';String v_sql = 'select name from table_a where id = ' + v_id ; 以上代碼,看起來是使用了變數v_id ,但這卻是java 的程式變數,而不是oracle 的綁定變數,語句傳遞到資料庫後,此java 的程式變數已經被替換成具體的常量值,變成:select * from table_a where name = 'xxxxx' ;假定這個語句第一次執行,會進行硬分析。後來,同一段java 代碼中v_id 值發現變化(v_id = 'yyyyyy'),資料庫又接收到這樣的語句:select * from table_a where name = 'yyyyyy' ;ORACLE 並不認為以上兩條語句是相同的語句,因此對第二條語句會又做一次硬分析。這兩條語句的執行計畫可是一樣的!其實,只需將以上java 代碼改成以下這樣,就使用了oracle 的綁定變數:String v_id = 'xxxxx';String v_sql = 'select name from table_a where id = ? '; //嵌入綁定變數stmt = con.prepareStatement( v_sql );stmt.setString(1, v_id ); //為綁定變數賦值stmt.executeQuery()......餘下全文>>
 
oracle中:=與=:的不同?

變數綁定 是指在sql語句的條件中使用變數而不是常量。比如shared pool裡有兩條sql語句,
select * from tab1 where col1=1;
select * from tab1 where col1=2;
對oracle資料庫來說,這是兩條完全不同的SQL,對這兩條語句都需要進行hard parse。因為oracle會根據sql語句的文本去計算每個字元在記憶體裡的hash值,因此雖然上述兩條SQL只有一個字元不一樣,oracle根據hash演算法在記憶體中得到的hash地址就不一樣,所以oracle就會認為這是兩條完全不同的語句。而如果將上述SQL改寫成select * from tab1 where col1=:var1;,然後通過對變數var1的賦值去查詢,那麼oracle對這條語句第一次會進行hard parse,以後就只進行soft parse。假設某條語句被重複執行了幾十萬次,那麼使用bind var帶來的好處是巨大的。一個應用程式如果bind var使用不充分,那麼幾乎一定會伴隨著嚴重的效能問題。

綁定變數是相對文本變數來講的,所謂文本變數是指在SQL直接書寫查詢條件,這樣的SQL在不同條件下需要反覆解析,綁定變數是指使用變數來代替直接書寫條件,查詢bind value在運行時傳遞,然後綁定執行。優點是減少硬解析,降低CPU的爭用,節省shared_pool ;缺點是不能使用histogram,sql最佳化比較困難
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.