driving_site:Oracle 跨庫關聯資料時速度問題

ForTechnology發表於2011-08-17
driving_site:Oracle 跨庫關聯資料時速度問題 收藏
       在近期的一個資料庫查詢統計中,需要通過db_link跨庫進行表的關聯,其中A庫 中的某張擁有百萬以上資料的表與B庫 中的擁有千條記錄以上的表進行關聯。兩張表進行關聯的欄位都建有索引。但在實際查詢中如果從B庫進行兩表的關聯卻會導致A庫中大表的全表掃描,查詢速度慢是次要的,關鍵是全表掃描會影響到A庫的正常執行。
       通過對SQL語句的不斷調整、優化發現當兩表進行簡單的關聯查詢時,如果在select 後面採用"*"獲取所有資料時,查詢不會導致全表掃描,但在select 後面取具體欄位時將會導致大表的全表掃描。
      在簡單查詢的基礎上進行group分組將直接導致大表的全表掃描,在此種情況下,所有的語句優化技巧都將失效,因為索引無效,此時必須採取特殊的方法建立 起關聯查詢的索引,強制指定在關聯過程中那一張表作為主要驅動表,那一張作為從表。在跨庫關聯查詢的過程中,因為表不在同一個資料庫中(同時在不同操作系 統的伺服器上)將導致關聯過程中將一張表的資料提取到另一個資料庫中進行關聯。此時問題出現,如果在關聯過程中將大表資料提取到小表所在庫進行運算將導致 大表的全表掃描,整個運算過程將非常慢且影響A庫的正常執行。
      最終解決全表掃描的辦法是,如果在B庫對不同資料庫的兩張大小表進行關聯時,通過driving_site強制指定主驅動表,即以所指定的表為主要表,將其它表作為從表提取到驅動表所在的庫進行關聯運算。具體語句如下:
    select /*+driving_site(main)*/  a.*,b.* from A.a main@BigTableDB,B.b minor where main.id=minor.id and .......
  這樣一來就可以避免大表所在庫的全表掃描,查詢速度將成級數級提高。
解釋:
當指定 /*+driving_site(main)*/ 時, oracle將會從minor表獲取全部資料到main表所在的資料庫來進行關聯運算.(索引起作用,速度快)
若指定 /*+driving_site(minor)*/ 時, oracle將會從main表獲取全部資料到minor表所在的資料庫來進行關聯運算.(索引失敗,全表掃描)
當然Oracle中有很多的關鍵字用於指定關聯查詢時的運算方法,等後面碰到具體應用例項實再作一一介紹。
引用:
DRIVING_SITE 作用和 ORDERED 類似。 DRIVING_SITE 通常在分散式查詢中使用。如果沒有這個提示, Oracle 會先從遠端點檢索,並將它們連線到本地站點中。通過使用 DRIVING_SITE ,我們可以先在本地進行檢索,將檢索後的資料傳送到遠端節點進行連線。
提示:合理使用 DRIVING_SITE ,可以在分散式查詢中大大減少網路流量。
Oracle Hint 收藏
 
Hint 是Oracle 提供的一種SQL語法,它允許使用者在SQL語句中插入相關的語法,從而影響SQL的執行方式。
 
因為Hint的特殊作用,所以對於開發人員不應該在程式碼中使用它,Hint 更像是Oracle提供給DBA用來分析問題的工具 。在SQL程式碼中使用Hint,可能導致非常嚴重的後果,因為資料庫的資料是變化的,在某一時刻使用這個執行計劃是最優的,在另一個時刻,卻可能很差,這也是CBO 取代RBO的原因之一,規則是死的,而資料是時刻變化的,為了獲得最正確的執行計劃,只有知道表中資料的實際情況,通過計算各種執行計劃的成本,則其最優,才是最科學的,這也是CBO的工作機制。 在SQL程式碼中加入Hint,特別是效能相關的Hint是很危險的做法。
Hints
Hints are comments in a SQL statement that pass instructions to the Oracle Database optimizer. The optimizer uses these hints to choose an execution plan for the statement, unless some condition exists that prevents the optimizer from doing so.
Hints were introduced in Oracle7, when users had little recourse if the optimizer generated suboptimal plans. Now Oracle provides a number of tools, including the SQL Tuning Advisor, SQL plan management, and SQL Performance Analyzer, to help you address performance problems that are not solved by the optimizer. Oracle strongly recommends that you use those tools rather than hints. The tools are far superior to hints, because when used on an ongoing basis, they provide fresh solutions as your data and database environment change.
Hints should be used sparingly, and only after you have collected statistics on the relevant tables and evaluated the optimizer plan without hints using the EXPLAIN PLAN statement. Changing database conditions as well as query performance enhancements in subsequent releases can have significant impact on how hints in your code affect performance.
The remainder of this section provides information on some commonly used hints. If you decide to use hints rather than the more advanced tuning tools, be aware that any short-term benefit resulting from the use of hints may not continue to result in improved performance over the long term.
Oracle 聯機文件對Hint的說明:
http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/sql_elements006.htm#SQLRF50705

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25897606/viewspace-705157/,如需轉載,請註明出處,否則將追究法律責任。

相關文章