oracle編程藝術--runstst工具

来源:http://www.cnblogs.com/befer/archive/2016/01/30/5171144.html
-Advertisement-
Play Games

runstats工具是《 oracle database 9i/10g/11g編程藝術 深入資料庫體繫結構》作者寫的一個統計性能工具,能對做同一件事的兩個方法進行比較,得到孰優孰劣的結果。 (看到runstats想到了db2 里有runstats命令收集統計信息) runststs工具主要測量三個要


runstats工具是《 oracle database 9i/10g/11g編程藝術 深入資料庫體繫結構》作者寫的一個統計性能工具,能對做同一件事的兩個方法進行比較,得到孰優孰劣的結果。

(看到runstats想到了db2 里有runstats命令收集統計信息)

runststs工具主要測量三個要素

  • 牆上時鐘(wall clock) 或耗用時間(elapsed time)
  • 系統統計結果,會併排地顯示每個方法做某件事(如執行一個解析調用)的次數,並展示出二者之差
  • 閂定(latch)這個是報告的關鍵輸出

要使用該工具,需要能訪問V$視圖,並創建一個表來存儲統計結果,還需要創建runstats包,下麵是在scott用戶下創建該工具,以下試驗在ORACLE 11.2.0.1.0上進行

使用SYS用戶登錄,執行以下語句

--預設scott無創建視圖許可權,創建視圖時會報ORA-01031: insufficient privileges
grant create view to scott;
--將以下4個動態性能視圖原表SELECT許可權賦給scott
grant SELECT on v_$statname to scott ;
grant SELECT on v_$mystat to scott ;
grant SELECT on v_$latch to scott ;
grant SELECT on v_$timer to scott ;


scott用戶下登錄,執行以下語句

--創建統計結果表
create or replace view stats
as select 'STAT...' || a.name name, b.value 
      from sys.v_$statname a, sys.v_$mystat b 
     where a.statistic# = b.statistic# 
    union all 
    select 'LATCH.' || name,  gets 
      from sys.v_$latch 
    union all 
    select 'STAT...Elapsed Time', hsecs from sys.v_$timer; 

--創建臨時表收集統計結果
create global temporary table run_stats
(runid varchar2(15),
 name varchar2(80),
 value int)
 on commit preserve rows;  
    
--創建runstats包
-- runstats包含3個API,runstats測試開始時調用rs_start,rs_middle會在測試中調用,完成時調用rs_stop,列印報告
-- rs_stop的p_difference_threshold參數,用來控制最後列印的數據量,輸入這個參數可只查看差值大於參數的統計結果和閂信息,預設為0全部顯示

create or replace package runstats_pkg
as 
       procedure rs_start;
       procedure rs_middle;
       procedure rs_stop(p_difference_threshold in number default 0);
end ;
/

create or replace package body runstats_pkg 
    as 
    g_start number; 
    g_run1  number; 
    g_run2  number; 
    
    --清空統計結果表,插入上一次統計結果,獲取當前定時器值
    procedure rs_start 
    is 
    begin 
       delete from run_stats; 
       
       insert into run_stats 
       select 'before', stats.* from stats; 
         g_start := dbms_utility.get_cpu_time; 
   end; 
 
   procedure rs_middle 
   is 
   begin 
       g_run1 := (dbms_utility.get_cpu_time-g_start); 
  
       insert into run_stats 
       select 'after 1', stats.* from stats; 
       g_start := dbms_utility.get_cpu_time; 
  
   end; 

   --列印每次運行累計CPU時間,分別列印兩次運行的統計結果和閂值(只列印超過p_difference_threshold的結果)
   procedure rs_stop(p_difference_threshold in number default 0) 
   is 
   begin 
       g_run2 := (dbms_utility.get_cpu_time-g_start); 
  
       dbms_output.put_line 
       ( 'Run1 ran in ' || g_run1 || ' cpu hsecs' ); 
       dbms_output.put_line 
       ( 'Run2 ran in ' || g_run2 || ' cpu hsecs' ); 
           if ( g_run2 <> 0 ) 
           then 
       dbms_output.put_line 
       ( 'run 1 ran in ' || round(g_run1/g_run2*100,2) || 
         '% of the time' ); 
           end if; 
       dbms_output.put_line( chr(9) ); 
  
       insert into run_stats 
       select 'after 2', stats.* from stats; 
  
       dbms_output.put_line 
       ( rpad( 'Name', 30 ) || lpad( 'Run1', 12 ) || 
         lpad( 'Run2', 12 ) || lpad( 'Diff', 12 ) ); 
  
       for x in 
       ( select rpad( a.name, 30 ) || 
                to_char( b.value-a.value, '999,999,999' ) || 
                to_char( c.value-b.value, '999,999,999' ) || 
                 to_char( ( (c.value-b.value)-(b.value-a.value)),  
                                    '999,999,999' ) data 
           from run_stats a, run_stats b, run_stats c 
          where a.name = b.name 
            and b.name = c.name 
            and a.runid = 'before' 
            and b.runid = 'after 1' 
            and c.runid = 'after 2' 
             
            and abs( (c.value-b.value) - (b.value-a.value) ) 
                  > p_difference_threshold 
          order by abs( (c.value-b.value)-(b.value-a.value)) 
       ) loop 
           dbms_output.put_line( x.data ); 
       end loop; 
  
       dbms_output.put_line( chr(9) ); 
       dbms_output.put_line 
       ( 'Run1 latches total versus runs -- difference and pct' ); 
       dbms_output.put_line 
       ( lpad( 'Run1', 12 ) || lpad( 'Run2', 12 ) || 
         lpad( 'Diff', 12 ) || lpad( 'Pct', 10 ) ); 
  
       for x in 
       ( select to_char( run1, '999,999,999' ) || 
                to_char( run2, '999,999,999' ) || 
                to_char( diff, '999,999,999' ) || 
                to_char( round( run1/decode( run2, 0,  
                             to_number(0), run2) *100,2 ), '99,999.99' ) || '%' data 
           from ( select sum(b.value-a.value) run1, sum(c.value-b.value) run2, 
                         sum( (c.value-b.value)-(b.value-a.value)) diff 
                    from run_stats a, run_stats b, run_stats c 
                   where a.name = b.name 
                    and b.name = c.name 
                     and a.runid = 'before' 
                     and b.runid = 'after 1' 
                     and c.runid = 'after 2' 
                     and a.name like 'LATCH%' 
                   ) 
       ) loop 
           dbms_output.put_line( x.data ); 
       end loop; 
   end; 
  
  end; 
  /

工具創建好了之後,可以拿個例子來測試一下,把下麵語句寫入test.sql,做成一個SQL文件,sqlplus中執行

drop table testStat;
create table testStat(id varchar2(10));
exec runstats_pkg.rs_start;
exec dbms_output.put_line('rs_start....');
insert into testStat select level from dual connect by level <=500000;
commit;
exec dbms_output.put_line('insert completed....');
exec runstats_pkg.rs_middle;
exec dbms_output.put_line('rs_middle....');
begin
  for i in 1 .. 500000
    loop
    insert into testStat values (i);
    end loop;
    commit;
   end;
   /
exec dbms_output.put_line('loop insert....');
exec runstats_pkg.rs_stop(0);

結果如下:

[oracle@RHEL65 test]$ sqlplus scott/oracle@orcl @t.sql

SQL*Plus: Release 11.2.0.1.0 Production on Sat Jan 30 16:54:09 2016

Copyright (c) 1982, 2009, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


Table dropped.


Table created.


PL/SQL procedure successfully completed.

rs_start....

PL/SQL procedure successfully completed.


500000 rows created.


Commit complete.

insert completed....

PL/SQL procedure successfully completed.


PL/SQL procedure successfully completed.

rs_middle....

PL/SQL procedure successfully completed.


PL/SQL procedure successfully completed.

loop insert....

PL/SQL procedure successfully completed.

Run1 ran in 66 cpu hsecs
Run2 ran in 2217 cpu hsecs
run 1 ran in 2.98% of the time

Name                                  Run1        Run2        Diff
STAT...opened cursors current           -1           0           1
STAT...redo synch writes                 2           1          -1
STAT...commit txn count during           2           3           1
STAT...IMU Flushes                       2           1          -1
STAT...rows fetched via callba           5           4          -1
STAT...cursor authentications            0           1           1
STAT...buffer is pinned count            1           2           1
STAT...parse time elapsed                1           0          -1
LATCH.channel handle pool latc           2           1          -1
LATCH.queued dump request                0           1           1
LATCH.MinActiveScn Latch                 0           1           1
LATCH.Shared B-Tree                      1           2           1
LATCH.hash table modification            1           0          -1
LATCH.SQL memory manager latch           0           1           1
LATCH.kwqbsn:qsga                        0           1           1
LATCH.threshold alerts latch             0           1           1
STAT...IMU pool not allocated            0           2           2
STAT...IMU- failed to get a pr           0           2           2
STAT...SQL*Net roundtrips to/f          10           8          -2
LATCH.ksuosstats global area             0           2           2
LATCH.dml lock allocation                2           4           2
STAT...user calls                       15          12          -3
STAT...sorts (memory)                   11           8          -3
STAT...sorts (rows)                      5           2          -3
LATCH.object stats modificatio           7           4          -3
LATCH.kcbtsemkid latch                   0           3           3
LATCH.managed standby latch              0           3           3
LATCH.parameter list                     0           3           3
LATCH.session state list latch           3           0          -3
LATCH.session switching                  1           5           4
LATCH.ksv allocation latch               0           4           4
LATCH.sort extent pool                   0           4           4
LATCH.deferred cleanup latch             0           4           4
LATCH.cp sga latch                       0           4           4
LATCH.parallel query alloc buf           1           5           4
LATCH.ncodef allocation latch            0           4           4
LATCH.qmn task queue latch               0           4           4
LATCH.ASM network state latch            0           4           4
STAT...write clones created in           0           5           5
STAT...immediate (CURRENT) blo          14           9          -5
LATCH.resmgr:active threads              0           5           5
LATCH.resmgr:schema config               0           5           5
LATCH.job_queue_processes para           0           5           5
STAT...table scans (short tabl           6          12           6
STAT...table scan blocks gotte           4          10           6
LATCH.FAL Queue                          0           6           6
LATCH.alert log latch                    0           6           6
LATCH.reservation so alloc lat           0           6           6
LATCH.transaction allocation            15           8          -7
LATCH.OS process allocation              0           9           9
LATCH.KMG MMAN ready and start           0           9           9
LATCH.Change Notification Hash           0           9           9
LATCH.Real-time plan statistic           0           9           9
STAT...redo buffer allocation            0          10          10
STAT...physical read total IO            2          13          11
STAT...physical reads                    2          13          11
STAT...physical reads cache              2          13          11
STAT...physical read IO reques           2          13          11
LATCH.cache buffer handles           1,064       1,076          12
LATCH.archive control                    0          12          12
LATCH.Reserved Space Latch               0          12          12
LATCH.session timer                      0          12          12
LATCH.kks stats                          1          15          14
LATCH.shared pool simulator             10          24          14
STAT...Heap Segment Array Upda          23           8         -15
STAT...switch current to new b          31          14         -17
STAT...calls to get snapshot s         188         171         -17
STAT...cluster key scans                51          34         -17
STAT...cluster key scan block           51          34         -17
STAT...index scans kdiixs1              88         105          17
STAT...deferred (CURRENT) bloc          32          14         -18
LATCH.FIB s.o chain latch                0          18          18
STAT...consistent changes               63          44         -19
STAT...table fetch by rowid             31          50          19
STAT...shared hash latch upgra          50          70          20
LATCH.archive process latch              0          21          21
LATCH.space background task la           0          21          21
STAT...consistent gets - exami         888         912          24
STAT...index fetch by key               52          28         -24
LATCH.FOB s.o list latch                 2          26          24
STAT...commit cleanouts                867         842         -25
STAT...commit cleanouts succes         861         836         -25
STAT...no work - consistent re         146         171          25
STAT...workarea memory allocat         -46         -21          25
LATCH.session idle bit                  35          63          28
STAT...hot buffers moved to he           0          29          29
LATCH.In memory undo latch              15          45          30
STAT...buffer is not pinned co         277         312          35
STAT...redo log space requests           0          40          40
LATCH.SGA IO buffer pool latch           2          45          43
LATCH.DML lock allocation              139          93         -46
LATCH.post/wait queue                    3          51          48
LATCH.active service list                0          51          51
LATCH.file cache latch                  46         108          62
STAT...cleanout - number of kt         744         817          73
STAT...active txn count during         743         816          73
LATCH.call allocation                    8          82          74
LATCH.active checkpoint queue            9          84          75
LATCH.session allocation                12         115         103
LATCH.ASM db client latch                2         106         104
LATCH.object queue header heap          22         132         110
LATCH.Consistent RBA                    15         129         114
LATCH.lgwr LWN SCN                      15         129         114
LATCH.mostly latch-free SCN             15         131         116
STAT...table scan rows gotten           50         176         126
LATCH.message pool operations            4         130         126
STAT...enqueue releases                280         409         129
STAT...enqueue requests                280         409         129
STAT...enqueue conversions               3         147         144
LATCH.JS queue state obj latch           0         180         180
STAT...messages sent                    14         202         188
STAT...file io wait time                66         276         210
STAT...non-idle wait count              21         251         230
STAT...redo log space wait tim           0         355         355
STAT...IMU undo allocation siz         712       1,080         368
STAT...change write time                 3         403         	   

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

-Advertisement-
Play Games
更多相關文章
  • js如何使用指定字元分割字元串:在實際應用中,可能需要將字元進行分割,然後進行相關操作,下麵就給出這樣一個代碼實例供大家參考。代碼如下: var str="a-n-t-z-o-n-e"; var theArray=str.split("-"); console.log(theArray); 以上代碼
  • <link>和@import url()引入外部css文件的區別:標題中的兩種方式都可以引入外部css文件,關於它們的基本用法這裡就不多介紹了,具體可以參閱相關閱讀。相關閱讀:(1).<link>標簽可以參閱HTML的<link>標簽一章節。(2).@import url()可以參閱css的@imp
  • 點擊增加或者減少商品數量並且自動計算總價格:本章節介紹一下如何實現點擊按鈕來添加或者刪除商品的數量,並且能夠自動計算商品的總價格。代碼實例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" con
  • javascript計算字元串的長度區分中英文:計算字元串的長度是常有的操作,代碼如下: var str="I love 螞蟻部落"; console.log(str.length); 使用length計算字元串的長度,得出的結果是字元串中字元的個數,而在實際應用中可能需要計算字元串所占有的位元組長度
  • 使用<link>標簽引入css文件簡單代碼實例:本章節介紹一下如何利用<link>標簽引入外部css文件。雖然這個非常的簡單,但是很多初學者比較容易忘記它的基本使用格式。下麵就演示一下如何使用此標簽引入外部樣式表:代碼如下: <link rel="stylesheet" type="text/css
  • 獲取table表格指定列的所有單元格的內容:本章節介紹一下如何獲取表格指定列的所有單元格的內容,這可能在一些應用或者說擴展應用中會需要。代碼實例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author"
  • 在SQL SERVER 2005/2008中,如果將有序的記錄插入臨時表,則從臨時表查詢出來的記錄是有序的(不依賴ORDER BY也是有序狀態),但是從SQL SERVER 2012開始,即使插入的記錄集有序,查詢出來的結果變成無序了。需要依賴ORDER BY來或得到一個有序結果。例如下麵例子: S...
  • OLEDB等待事件介紹 OLEDB等待類型是SQL SERVER 資料庫中最常見的幾種等待類型之一。它意味著某個會話(SPID)通過SQL Server Native Client OLEDB Provider發生了調用請求並等待資料庫返回所需的數據。它出現在遠程系統(remote system )...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...