Oracle 11g R2性能優化 tkprof

来源:https://www.cnblogs.com/dbabd/archive/2019/01/21/10297694.html
-Advertisement-
Play Games

正文 另一篇博文總結了關於SQL TRACE工具的使用方式,但是產生的trace文件格式閱讀起來並不是十分友好,為了更好的分析trace文件,Oracle也自帶了一個格式化工具 tkprof 。 tkprof 工具用於處理原始的trace文件,合併彙總trace文件當中一些項目值,然後進行格式化,這 ...


正文

另一篇博文總結了關於SQL TRACE工具的使用方式,但是產生的trace文件格式閱讀起來並不是十分友好,為了更好的分析trace文件,Oracle也自帶了一個格式化工具tkproftkprof工具用於處理原始的trace文件,合併彙總trace文件當中一些項目值,然後進行格式化,這樣就使得trace文件的可讀性更好。

關於tkprof的詳細說明可以參考官方文檔:Understanding SQL Trace and TKPROF

關於trace文件的生成可以參考另一篇博文:Oracle 11g R2性能優化 SQL TRACE

主要說明

tkprof命令位於 $ORACLE_HOME/bin/路徑下,通過命令tkprof直接獲取相關選項參數:

$ tkprof
Usage: tkprof tracefile outputfile [explain= ] [table= ]
[print= ] [insert= ] [sys= ] [sort= ]

關鍵選項

  • tracefile
    需要格式化的trace文件

  • outputfile
    格式化完成後的輸出文件

  • explain=
    通過給定方式連接資料庫,併為每條語句生成執行計劃

  • sort=
    指定排序鍵來展示SQL語句

其他選項如下:


  table=schema.tablename   Use 'schema.tablename' with 'explain=' option. -- 指定PLAN_TABLE生成執行計劃,預設為系統的PLAN_TABLE
  explain=user/password    Connect to ORACLE and issue EXPLAIN PLAN. 
  print=integer    List only the first 'integer' SQL statements. -- 顯示指定數目的SQL語句
  aggregate=yes|no -- 指定是否對重覆SQL語句信息合併,預設yes合併,如果指定為no,對重覆SQL語句信息會獨立輸出一個條目
  insert=filename  List SQL statements and data inside INSERT statements. -- 創建一張表將每個跟蹤的SQL語句的一行統計信息插入到表中
  sys=no           TKPROF does not list SQL statements run as user SYS. -- 指定不記錄由SYS用戶執行的語句
  record=filename  Record non-recursive statements found in the trace file. -- 指定生成不包含遞歸語句的SQL語句文件
  waits=yes|no     Record summary for any wait events found in the trace file. -- 指定為語句生成等待事件信息(如有)

-- 以下為排序選項說明

  sort=option      Set of zero or more of the following sort options:

-- 1、解析調用期間執行排序
    prscnt  number of times parse was called                     -- 解析次數                    
    prscpu  cpu time parsing                                     -- 解析消耗CPU時間
    prsela  elapsed time parsing                                 -- 解析所消耗時間
    prsdsk  number of disk reads during parse                    -- 解析時物理讀次數
    prsqry  number of buffers for consistent read during parse   -- 解析時一致讀次數
    prscu   number of buffers for current read during parse      -- 解析時當前讀次數
    prsmis  number of misses in library cache during parse       -- 解析時庫緩存區未命中次數

-- 2、執行調用期間執行排序
    execnt  number of execute was called                         -- 執行次數
    execpu  cpu time spent executing                             -- 執行時消耗CPU時間
    exeela  elapsed time executing                               -- 執行所消耗的時間
    exedsk  number of disk reads during execute                  -- 執行時物理讀次數
    exeqry  number of buffers for consistent read during execute -- 執行時一致讀次數
    execu   number of buffers for current read during execute    -- 執行時當前讀次數
    exerow  number of rows processed during execute              -- 執行時處理的記錄數
    exemis  number of library cache misses during execute        -- 執行時庫緩衝區未命中次數

-- 3、提取調用期間執行排序
    fchcnt  number of times fetch was called                     -- 提取數據次數
    fchcpu  cpu time spent fetching                              -- 提取時消耗CPU時間
    fchela  elapsed time fetching                                -- 提取所消耗的時間
    fchdsk  number of disk reads during fetch                    -- 提取時物理讀次數  
    fchqry  number of buffers for consistent read during fetch   -- 提取時一致讀次數
    fchcu   number of buffers for current read during fetch      -- 提取時當前讀次數
    fchrow  number of rows fetched                               -- 提取的記錄數
    
    userid  userid of user that parsed the cursor                -- 按游標解析時的userid排序                

關於tkprof工具更詳細的用法可以參考Oracle MOS文檔:TKProf Interpretation (9i and above) (文檔 ID 760786.1)

用法示例

對SCOTT開啟跟蹤

$ sqlplus scott/tiger

SCOTT@dbabd> alter session set tracefile_identifier = 'SCOTT';

Session altered.

SCOTT@dbabd> exec dbms_session.session_trace_enable(true, true, 'all_executions');

PL/SQL procedure successfully completed.

執行一條SQL語句

SCOTT@dbabd> select * from emp;

     EMPNO ENAME      JOB                    MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK                 7902 1980-12-17 00:00:00        800                    20
      7499 ALLEN      SALESMAN              7698 1981-02-20 00:00:00       1600        300         30
      7521 WARD       SALESMAN              7698 1981-02-22 00:00:00       1250        500         30
      7566 JONES      MANAGER               7839 1981-04-02 00:00:00       2975                    20
      7654 MARTIN     SALESMAN              7698 1981-09-28 00:00:00       1250       1400         30
      7698 BLAKE      MANAGER               7839 1981-05-01 00:00:00       2850                    30
      7782 CLARK      MANAGER               7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT      ANALYST               7566 1987-04-19 00:00:00       3000                    20
      7839 KING       PRESIDENT                  1981-11-17 00:00:00       5000                    10
      7844 TURNER     SALESMAN              7698 1981-09-08 00:00:00       1500          0         30
      7876 ADAMS      CLERK                 7788 1987-05-23 00:00:00       1100                    20
      7900 JAMES      CLERK                 7698 1981-12-03 00:00:00        950                    30
      7902 FORD       ANALYST               7566 1981-12-03 00:00:00       3000                    20
      7934 MILLER     CLERK                 7782 1982-01-23 00:00:00       1300                    10

14 rows selected.

對SCOTT關閉跟蹤

SCOTT@dbabd> exec dbms_session.session_trace_disable();

PL/SQL procedure successfully completed.

tkprof分析trace文件

執行如下命令生成格式化文件:

$ tkprof /data/app/oracle/diag/rdbms/dbabd/dbabd/trace/dbabd_ora_18629_SCOTT.trc /data/app/scott_trace.log explain=scott/tiger aggregate=yes sys=no waits=yes sort=fchela

查看tkprof生成文件:

$ cat /data/app/scott_trace.log

-- 開頭概要部分,基本信息說明,包括tkprof版本、trace文件路徑、排序選項和報告參數說明
TKPROF: Release 11.2.0.4.0 - Development on Thu Jan 17 17:41:04 2019

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

Trace file: /data/app/oracle/diag/rdbms/dbabd/dbabd/trace/dbabd_ora_18629_SCOTT.trc
Sort options: fchela
********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
********************************************************************************

-- 跟蹤的SQL語句文本、SQL ID和執行計劃HASH

SQL ID: a2dk8bdn0ujx7 Plan Hash: 3956160932

select *
from
 emp

-- SQL執行的統計信息

/*
call:調用類型
count:調用執行次數
cpu:需要的CPU時間(單位:秒)
elapsed:需要消耗的時間(單位:秒)
disk:發生物理讀次數
query:發生一致讀次數
current:發生當前讀次數
rows:獲取的行數
*/

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      0.00       0.00          0          7          0          14
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.00       0.00          0          7          0          14

Misses in library cache during parse: 1     -- 庫緩存沒有命中數,說明是硬解析
Optimizer mode: ALL_ROWS                    -- 優化器模式
Parsing user id: 83  (SCOTT)                -- 解析用戶
Number of plan statistics captured: 1       -- 執行計劃統計信息獲取數

-- SQL語句實際執行消耗的資源信息
/*
Rows:操作實際返回記錄數
Row Source Operation:當前行操作的訪問方式
    cr:一致性讀的數據塊,對應query的Fetch值
    pr:物理讀的數據塊,對應disk的Fetch值
    pw:物理寫的數據塊
    time:執行時間
    cost:優化器執行成本
    size:處理的位元組數
    card:處理的記錄數
*/
Rows (1st) Rows (avg) Rows (max)  Row Source Operation
---------- ---------- ----------  ---------------------------------------------------
        14         14         14  TABLE ACCESS FULL EMP (cr=7 pr=0 pw=0 time=211 us cost=3 size=532 card=14)

-- 指定explain選項輸出的執行計劃
Rows     Execution Plan
-------  ---------------------------------------------------
      0  SELECT STATEMENT   MODE: ALL_ROWS
     14   TABLE ACCESS   MODE: ANALYZED (FULL) OF 'EMP' (TABLE)

-- 指定選項waits=yes選項輸出的等待事件信息統計
Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       2        0.00          0.00
  SQL*Net message from client                     2      128.00        128.00
********************************************************************************

SQL ID: bnqn0qyvy59qf Plan Hash: 0

BEGIN dbms_session.session_trace_enable(true, true, 'all_executions'); END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0         61          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        1      0.00       0.00          0         61          0           1

Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 83  (SCOTT)

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       1        0.00          0.00
  SQL*Net message from client                     1       68.47         68.47
********************************************************************************

SQL ID: 23d3sap7cask4 Plan Hash: 0

BEGIN dbms_session.session_trace_disable(); END;


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           1
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        2      0.00       0.00          0          0          0           1

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 83  (SCOTT)


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

-- 開啟跟蹤期間所有非遞歸SQL語句執行信息統計彙總
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        2      0.01       0.01          0          0          0           0
Execute      3      0.00       0.00          0         61          0           2
Fetch        2      0.00       0.00          0          7          0          14
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        7      0.01       0.01          0         68          0          16

Misses in library cache during parse: 2
Misses in library cache during execute: 1

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       3        0.00          0.00
  SQL*Net message from client                     3      128.00        196.48


-- 開啟跟蹤期間所有遞歸SQL語句執行信息統計彙總
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute     23      0.00       0.00          0          0          0           0
Fetch       37      0.00       0.00          2         82          0          28
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total       60      0.00       0.00          2         82          0          28

Misses in library cache during parse: 0

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  db file sequential read                         2        0.00          0.00

-- 所有分析的SQL語句彙總統計
    3  user  SQL statements in session.
   13  internal SQL statements in session.
   16  SQL statements in session.
    1  statement EXPLAINed in this session.
********************************************************************************
Trace file: /data/app/oracle/diag/rdbms/dbabd/dbabd/trace/dbabd_ora_18629_SCOTT.trc
Trace file compatibility: 11.1.0.7
Sort options: fchela
       1  session in tracefile.
       3  user  SQL statements in trace file.
      13  internal SQL statements in trace file.
      16  SQL statements in trace file.
      16  unique SQL statements in trace file.
       1  SQL statements EXPLAINed using schema:
           SCOTT.prof$plan_table
             Default table was used.
             Table was created.
             Table was dropped.
     411  lines in trace file.
     196  elapsed seconds in trace file.

總結

以上梳理了關於tkprof工具的簡單用法,經過它格式化之後的trace文件更具有可讀性,真實地統計了SQL語句在執行過程當中資源的消耗。但是它提供的是彙總後的統計信息,如果需要瞭解SQL語句執行時每個步驟的資源消耗情況可以通過閱讀原始的trace文件,這裡不再進行深入討論了。SQL性能優化博大精深,涉及的知識面廣泛,也經常涉及Oracle底層運行機制和操作系統底層實現,需要自己學習與提高的地方還有很多。

參考

https://docs.oracle.com/cd/E11882_01/server.112/e41573/sqltrace.htm#PFGRF94981
TKProf Interpretation (9i and above) (文檔 ID 760786.1)

☆〖本人水平有限,文中如有錯誤還請留言批評指正!〗☆


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 會話持久性連接簡介: 會話保持是負載均衡器設備的一種機制,用於識別客戶端與伺服器之間交互過程的關連性,在進行負載均衡的同時還保證一系列相關連的訪問請求會保持分配到同一臺伺服器上。針對不同的業務場景需要不同的會話保持配置,並且並不是所有業務系統都需要會話保持配置。以最典型的 HTTP 應用為例,在大多 ...
  • 命令: ls 對應英文: list 作用: 查看當前文件夾下的內容 選項: -a 查看隱藏文件, 文件名前帶 . 號的文件 -l 以詳細列表形式顯示 -h 人性化方式顯示文件大小(註:-h必須和-l同時使用) ...
  • 本文收錄在容器技術學習系列文章總目錄 1、Kubernetes設計架構 Kubernetes集群包含有節點代理kubelet和Master組件(APIs, scheduler, etc),一切都基於分散式的存儲系統。下麵這張圖是Kubernetes的架構圖。 2、Kubernetes節點 2.1 介 ...
  • 轉自: https://www.linuxidc.com/Linux/2017-06/144916.htm 與大家分享下Linux系統中創建用戶、設置密碼、修改用戶、刪除用戶的命令,希望對你有所幫助。 useradd testuser 創建用戶testuserpasswd testuser 給已創建 ...
  • 上一節大致瞭解TCP/IP協議棧是個啥東西,依舊是霧裡看花的狀態,有很多時候學一門新知識時,開頭總是很急躁,無從下手,剛學會一點兒,卻發現連點皮毛都不算,成就感太低,所以任何時候學習最重要的是要在合適的時間掌握應該掌握的知識,循序漸進方能進步。 先簡單回顧一下,TCP/IP協議棧: 如果為每一層加上 ...
  • 一 簡介 Ansible是一款極其簡單的自動化運維工具, 基於Python開發, 集合了眾多運維工具(puppet, cfengine, chef, func, fabric)的優點。 實現了批量系統配置, 批量程式部署, 批量運行命令等功能。 Ansible是基於模塊工作的, 本身沒有批量部署的能 ...
  • 一. 背景 今天在MongoDB 4.0.4版本下,在還原恢複數據庫時報錯。 主要錯誤為: Failed: restore error: error applying oplog: applyOps: not authorized on admin to execute command { appl ...
  • 安裝完mysql之後,登陸以後,不管運行任何命令,總是提示這個:mac mysql error You must reset your password using ALTER USER statement before executing this statement. step 1: set p ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...