Oracle 優化 - CPU 問題


  • 作為 OLTP 應用,一般不太有 CPU 問題,比較少 - 畢竟大多數問題都是 IO 引起;但是偶爾也會有。
  • 問題判斷
    • 很簡單 - OS 出現 CPU 很高的問題,持續高於 90%
    • 應用可能會表現慢
  • 問題調查和解決
    • 硬件
    • 消耗 CPU 的 SQLs
    • 限制消耗 CPU 的 SQLs
  • 硬件
    • 根據經驗和具體情況增加新的 CPUs,曾經很難,在目前虛擬化普遍使用的情況下,不再是太大的困擾。
  • 消耗 CPU 的 SQLs
    • AWR report 里面的 SQL ordered by CPU Time 會明確的告訴原因。比如下面紅色的 SQL ID 69vuwhdz0jw51消耗了 Oracle 57% 的 CPU.
    • 這個 SQL 每次執行 0.22 秒 CPU, 實際耗時 (Elapsed Time / Executions = 0.5 秒左右);再結合具體的 SQL, 沒有優化的余地,那么需要 要么修改代碼減少調用次數,要么修改代碼簡化 SQL

SQL ordered by CPU Time

    • Resources reported for PL/SQL code includes the resources used by all SQL statements called by the code.
    • % Total is the CPU Time divided into the Total CPU Time times 100
    • Total CPU Time (s): 56,812
    • Captured SQL account for 79.1% of Total
    • Total CPU Time (s): 56,812
    • Captured PL/SQL account for 0.9% of Total
CPU Time (s) Elapsed Time (s) Executions CPU per Exec (s) % Total % Total DB Time SQL Id SQL Module SQL Text
32,595 96,381 149,476 0.22 57.37 47.42 69vuwhdz0jw51 JDBC Thin Client  
1,940 5,451 190 10.21 3.41 2.68 47y171y2nuvy9 JDBC Thin Client  
1,232 3,816 5,717 0.22 2.17 1.88 3x365dut1trbz JDBC Thin Client  
1,013 3,147 4,722 0.21 1.78 1.55 8vmfr11dt6bb7 JDBC Thin Client  
908 2,792 4,225 0.21 1.60 1.37 9kxdmwbytzsbs JDBC Thin Client  
848 2,139 451 1.88 1.49 1.05 9zv8ryyqzjudh JDBC Thin Client  
564 1,651 2,530 0.22 0.99 0.81 bdd9ay1mz8x22 JDBC Thin Client  
558 1,637 2,532 0.22 0.98 0.81 1pfxcnwp9ydw9 JDBC Thin Client  
556 1,633 2,532 0.22 0.98 0.80 dwysa8fbxz8du JDBC Thin Client  
500 1,496 2,288 0.22 0.88 0.74 57jgy744z6rq3 JDBC Thin Client  

    • 也可以使用下面的 SQLs 來更細的分析 TOP CPU SQLs.
  • 按照 CPU 總耗時排序 (執行次數 X 每次的 CPU 耗時)

select a.SQL_ID,c.username, CPU_TIME*0.000001 cpu_time_secs,
ELAPSED_TIME*0.000001 elapsed_time_secs,round(ELAPSED_TIME*0.000001/nvl(nullif(executions,0),1)) Elap_per_Exec,
executions,ROWS_PROCESSED,b.piece line#,b.sql_text
from v$sql a, v$sqltext b,dba_users c
where
a.address=b.address
and
(
ELAPSED_TIME*0.000001>10
or executions  > 1000
)
and executions>0
and c.user_id=a.PARSING_USER_ID
order by CPU_TIME,a.HASH_VALUE, b.piece asc

  • 按照 SQL 單次執行的CPU 耗時排序

    select c.username,CPU_TIME*0.000001 cpu_time_secs,
        ELAPSED_TIME*0.000001 elapsed_time_secs,
        round(ELAPSED_TIME*0.000001/nvl(nullif(executions,0),1)) Elap_per_Exec,
        executions,LAST_LOAD_TIME,
        b.piece line#,
        b.sql_text sql_text
from v$sql a, v$sqltext b, dba_users c
where
a.address=b.address
and
(
ELAPSED_TIME*0.000001>10
or executions  > 1000
)
and executions>0
and c.user_id=a.PARSING_USER_ID
order by Elap_per_Exec,ELAPSED_TIME,CPU_TIME,a.HASH_VALUE, b.piece asc;

 

  • 資源限制 - 終止 CPU 耗時超過一定閥值的 SQL

Create resource limit to limit SQLs which run more than 5 minutes.
alter system set resource_limit =true scope=both; (Oracle restart required to take effect)
create profile cpu_limit_3_min limit cpu_per_call 30000;  (time is in 100ths of a second)
alter user XXXX  profile cpu_limit_5_min; (change the user to use the profile)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM