tkprof命令列工具用法小結

edwardking888發表於2010-07-12
作用:格式化跟蹤檔案,例如格式化sql trace,10046產生的輸出檔案。

1)TKPROF的引數:
不輸入任何引數,直接輸入tkprof,回車,可以獲得一個完整的引數列表.
C:\>tkprof
Usage: tkprof tracefile outputfile [explain= ] [table= ]
              [print= ] [insert= ] [sys= ] [sort= ]
  table=schema.tablename   Use 'schema.tablename' with 'explain=' option.
  explain=user/password    Connect to ORACLE and issue EXPLAIN PLAN.
  print=integer    List only the first 'integer' SQL statements.
  aggregate=yes|no
  insert=filename  List SQL statements and
data inside INSERT statements.
  sys=no           TKPROF does not list SQL statements run as user SYS.
  record=filename  Record non-recursive statements found in the trace file.
  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:
    prscnt  number of times parse was called
    prscpu  cpu time parsing
    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
    execnt  number of execute was called
    execpu  cpu time spent executing
    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
    fchcnt  number of times fetch was called
    fchcpu  cpu time spent fetching
    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

2)幾個重要引數的用法講解
  • sys引數,如果不指定預設值為yes.這個引數的含義是,輸出檔案中是否包含以SYS使用者執行的sql語句。這個引數還是蠻有用的,我們執行sql語句的時候,後臺經常會執行很多遞迴的語句,比如你輸入了SELECT * FROM TEST;如果這個語句是硬解析的話,那麼會產生很多遞迴的SQL,遞迴的去查詢表的統計資訊,列的統計資訊,索引的統計資訊等,當然遞迴的不止是這些。這些遞迴的sql都是以SYS使用者執行的,如果你不希望看到這些遞迴SQL,那麼就加上這個引數sys=no.
  • record引數,它指定的是一個路徑下的檔案,這個檔案用來生成在跟蹤檔案中找到的所有的非遞迴SQL。比如你在SQLPLUS裡執行了三條語句,select * from a;select * from b;select * from c;,那麼如果你指定了這個引數如:record=c:\test.log,那麼你用tkprof格式化跟蹤檔案後,這個test.log裡就會記錄這三個SQL。這個特性在有些時候還是滿有用的,因為跟蹤檔案往往都會比較大,找起來會比較費勁,我們可以通過指定這個引數先大體瞭解下,跟蹤檔案裡都有哪些非遞迴SQL。而且這個功能還有助於我們重演SQL語句(繫結變數的不可以)。
  • aggregate引數,它指定tkprof是否將同樣文字內容的sql聚合處理,比如,你執行了十次select * from a,如果你指定這個引數為no(預設情況),那麼產生的輸出檔案會有十個這樣語句的執行資訊,如果你指定的是yes,那麼tkprof會把這十次的執行資訊彙總顯示。這個引數怎麼指定就看你的需要了,個人覺得還是滿有用的一個引數。
  • sort引數,這個引數是經常使用到的一個引數,它用來指定tkprof輸出檔案裡sql語句按照什麼排序,預設是按照執行的先後順序排序的,我們可以指定它按照其他方式排序,比如磁碟讀取數,CPU時間等。這個引數最經常用的方式是:sort=prsela,exeela,fchela,其實這三個值加起來就是響應時間,即按照響應時間排序。這裡別產生誤解,tkprof會根據prsela,exeela,fchela三個值的和進行排序,而不是像SQL語句似的一個個的排序。
  • print引數,它經常搭配sort引數一起使用,用來指定tkprof輸出sql語句的數量。這兩個引數搭配使用起來就比較妙,比如你想知道一個跟蹤檔案裡響應時間排前十的SQL,那麼你就可以sort=prsela,exeela,fchela print=10來搭配使用。
  • explain引數,這個引數的含義是為每一個SQL提供一個執行計劃。使用的方法是explain=使用者名稱/密碼,其實原理很簡單,就是通過你指定的使用者名稱,密碼登陸資料庫,然後為每一個sql執行以下explain plan for sql,輸出到plan_table裡,最後新增到輸出檔案裡。注意,由於explain plan for 命令要求執行操作的使用者要對sql語句裡包含的物件都有執行許可權,如果包含檢視,也要對檢視基於的基礎表有執行許可權,否則產生不了執行計劃。注意增加了這個引數後,執行tkprof會比較慢。
  • wait引數,指定輸出檔案中包含不包含等待事件,預設是包含的。一般都取預設值。
基本最長用到的就這些,其他的我就不介紹了,平時這些也就基本夠用了。

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

相關文章