ORACLE 如何查看索引重建進度情況

来源:http://www.cnblogs.com/kerrycode/archive/2016/07/15/5673224.html
-Advertisement-
Play Games

在ORACLE資料庫中,如果一個比較大的索引在重建過程中耗費時間比較長,那麼怎麼查看索引重建耗費的時間,以及完成了多少(比例)了呢,我們可以通過V$SESSION_LONGOPS視圖來查看索引重建的時間和進度。 官方文檔關於V$SESSION_LONGOPS的介紹如下 V$SESSION_LONGO... ...


   在ORACLE資料庫中,如果一個比較大的索引在重建過程中耗費時間比較長,那麼怎麼查看索引重建耗費的時間,以及完成了多少(比例)了呢,我們可以通過V$SESSION_LONGOPS視圖來查看索引重建的時間和進度。

 

官方文檔關於V$SESSION_LONGOPS的介紹如下

V$SESSION_LONGOPS

This view displays the status of various operations that run for longer than 6 seconds (in absolute time). These operations currently include many backup and recovery functions, statistics gathering, and query execution, and more operations are added for every Oracle release

To monitor query execution progress, you must be using the cost-based optimizer and you must:

Set the TIMED_STATISTICS or SQL_TRACE parameter to true

Gather statistics for your objects with the ANALYZE statement or the DBMS_STATS package

 

這個視圖顯示運行時間超過6秒的各類資料庫操作的狀態,這些操作包括備份、恢復功能,統計信息收集,以及查詢操作等。

 

要監控查詢執行進展情況,你必須是CBO優化器模式,並且滿足下麵條件:

  •    TIMED_STATISTICS或SQL_TRACE參數為true。
  •    使用DBMS_STATS包或ANLYZE語句收集、分析過對象的統計信息。

 

 

Column

DateType

Description

Description(中文)

SID

NUMBER

Session identifier

Session標識

SERIAL#

NUMBER

Session serial number

Session串號

OPNAME

VARCHAR2(64)

Brief description of the operation

操作簡要說明

TARGET

VARCHAR2(64)

The object on which the operation is carried out

操作的對象

TARGET_DESC

VARCHAR2(32)

Description of the target

目標對象說明

SOFAR

NUMBER

The units of work done so far

迄今為止完成的工作量

TOTALWORK

NUMBER

The total units of work

總工作量

UNITS

VARCHAR2(32)

The units of measurement

工作量單位

START_TIME

DATE

The starting time of operation

操作開始時間

LAST_UPDATE_TIME

DATE

Time when statistics last updated

統計項最後更新時間

TIMESTAMP

DATE

Timestamp

TIME_REMAINING

NUMBER

Estimate (in seconds) of time remaining for the operation to complete

預計完成操作的剩餘時間(秒)

ELAPSED_SECONDS

NUMBER

The number of elapsed seconds from the start of operations

從操作開始總花費時間(秒)

CONTEXT

NUMBER

Context

上下文關係

MESSAGE

VARCHAR2(512)

Statistics summary message

統計項的完整描述

USERNAME

VARCHAR2(30)

User ID of the user performing the operation

操作用戶

SQL_ADDRESS

RAW(4 | 8)

Used with the value of the SQL_HASH_VALUEcolumn to identify the SQL statement associated with the operation

SQL_HASH_VALUE

NUMBER

Used with the value of the SQL_ADDRESS column to identify the SQL statement associated with the operation

SQL_ID

VARCHAR2(13)

SQL identifier of the SQL statement associated with the operation

QCSID

NUMBER

Session identifier of the parallel coordinator

 

下麵我們來演示一下,首先構造了一個大表TEST_INDX,表TEST_INDX上建有一個索引IDX_TEST_INDX。我們開啟兩個會話視窗:

 

會話視窗1,執行下麵SQL語句:

SQL>  SELECT SID, SERIAL#, STATUS FROM V$SESSION WHERE AUDSID=USERENV('SESSIONID');
 
       SID    SERIAL# STATUS
---------- ---------- --------
       827        746 ACTIVE
 
SQL>  ALTER INDEX IDX_TEST_INDX REBUILD;
 
Index altered.
 
SQL> 

clip_image001[4]

 

在會話視窗2,執行下麵SQL

SQL> SELECT SID, SERIAL#, STATUS FROM V$SESSION WHERE AUDSID=USERENV('SESSIONID');
 
       SID    SERIAL# STATUS
---------- ---------- --------
       808       1003 ACTIVE
 
SQL> col opname format a32
SQL> col target format a32
SQL> col perwork format a12
SQL> set linesize 1200
SQL> select sid
  2        ,opname
  3        ,target
  4        ,sofar
  5        ,totalwork
  6        ,trunc(sofar/totalwork*100,2)||'%' as perwork
  7  from v$session_longops where sofar!=totalwork and sid=&sid;
Enter value for sid: 827
old   7: from v$session_longops where sofar!=totalwork and sid=&sid
new   7: from v$session_longops where sofar!=totalwork and sid=827
 
       SID OPNAME                        TARGET        SOFAR  TOTALWORK PERWORK
---------- --------------------- ------------------ ---------- ---------- --------
       827 Index Fast Full Scan      TEST.TEST_INDX    27914     157907 17.67%
 
SQL> /
Enter value for sid: 827
old   7: from v$session_longops where sofar!=totalwork and sid=&sid
new   7: from v$session_longops where sofar!=totalwork and sid=827
 
       SID OPNAME                        TARGET       SOFAR  TOTALWORK PERWORK
---------- -------------------- ------------------ ---------- ---------- -------
       827 Index Fast Full Scan   TEST.TEST_INDX      105075     157907 66.54%
 
SQL> /
Enter value for sid: 827
old   7: from v$session_longops where sofar!=totalwork and sid=&sid
new   7: from v$session_longops where sofar!=totalwork and sid=827
 
       SID OPNAME                   TARGET          SOFAR  TOTALWORK PERWORK
---------- ---------------  ------------------- ---------- ---------- --------
       827 Sort Output                              41728     175125 23.82%
 
SQL> 

clip_image002[4]

 

註意,這個SQL有時候需要一點時間才能看到結果,因為v$session_longpos中記錄的是執行時間超過6秒的操作,另外,你有時候會看到在Index Fast Full Scan之後,出現Sort Output操作。這個是索引重建時的排序操作,對這個Sort OutPut有點不清楚,在https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:3114287916999 這個鏈接裡面,看到Tom大師的這麼一個回覆:

 

It is not showing you the end to end time of the index create, it is showing you the end to end time of the STEPS within the index create.

For example, I had a sort_area_size of 1m. I created a table t with 1,000,000 rows based on all_objects. On this table, I put an index on object_id. This took many TABLE SCAN followed by SORT/MERGE followed by SORT/OUTPUT steps. Each was timed independently.

Next, I dropped that index and set my sort_area_size to 300m (large enough to avoid a sort to disk). This time, the ONLY thing in v$session_longops was a TABLE SCAN, no sort/merge, no sort/output. Since we didn't swap to disk, these steps were avoided.

So, I'll guess -- your single key index was done in memory, your concatenated key was not.

 

也就是說,如果sort_area_size足夠大,就不會出現Sort Merge或Sort Output操作,因為在sort_area_size不夠大的時候,就會使用臨時表空間的臨時段來排序。由於沒有查到較權威的官方資料,猜測是在索引重建過程中,由於sort_area_size不夠大,所以要使用磁碟排序,即使用了臨時表空間來排序,所以出現了Sort Output操作,它表示記憶體排序輸出到磁碟進行排序(當然僅僅是個人猜測,如有不正確的地方,敬請指正),另外在metalink上也看到這樣一段介紹:

 

First, there are the temporary segments that are used to store partial sort

data when the SORT_AREA_SIZE is too small to process the complete sort set

These segments are built in the user's default TEMPORARY tablespace.

Second, as the index is being rebuilt, it uses a segment which is defined as

a temporary segment until the rebuild is complete. Once this segment is fully

populated, the old index can be dropped and this temporary segment is redefined

as a permanent segment with the index name.

 

 

下麵我們對索引重建做一個10046跟蹤

 
SQL> alter session set events '10046 trace name context forever, level 12';
 
Session altered.
 
SQL> ALTER INDEX TEST.IDX_TEST_INDX REBUILD;
 
 
Index altered.
SQL> alter session set events '10046 trace name context off';
 
Session altered.

此時在trc文件裡面,你會看到大量的'direct path read temp'等待事件,表示重建索引時用到了臨時表空間做磁碟排序操作,由於索引非常大,所以產生了這個等待事件。

clip_image003[4]

clip_image004[4]

 

如果跟蹤一個非常小的索引重建,你在跟蹤文件裡面是看不到這個信息的。

 
SQL> alter session set events '10046 trace name context forever, level 12';
 
Session altered.
 
SQL> alter index scott.pk_emp rebuild;
 
Index altered.
 
SQL> alter session set events '10046 trace name context off';
 
Session altered.
 
SQL> 

clip_image005[4]

 

如果你根本不知道會話信息,如果我只知道是在那個表上重建索引,也可以根據表名來查詢,如下所示,我們還增加了開始時間等欄位

 

SQL> col opname format a32
SQL> col target format a32
SQL> col start_time format a24
SQL> col elapsed_seconds format 99
SQL> col perwork format a12
SQL> select sid
	   

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

-Advertisement-
Play Games
更多相關文章
  • App Store: 天的故事 1. 界面優化 2. 修複bug App Store: 天的故事 ...
  • 02
    02 ...
  • 相信大家都熟悉自動識別提示吧,在我們的生活中隨處可見,今天就讓我為大家簡單介紹一下它是如何設計的。 所謂自動識別輸入即是根據用戶輸入的已有信息,為用戶提示可能的值,方便用戶完成輸入。在Android設備上這種功能分為:AutoCompleteTextView和MultiAutoCompleteTex ...
  • 這兩天使用Reveal工具查看"手機淘寶"App的UI層次時,發現其圖片輪播使用了三個UIButton的復用來實現的圖片迴圈無縫滾動。於是乎就有了今天這篇博客,看到“手機淘寶”這個幻燈片的UI層級時,就想要動手使用三個Button來實現一下,當然本篇博客使用是Swift語言,思路就是使用三個Butt ...
  • -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* 1,不使用動畫 UIViewAnimationTransitionNone 2,從左向右旋轉翻頁 UIViewAnimationTransitionFlipFro ...
  • 當 IDENTITY_INSERT 設置為 OFF 時,不能向表 '#TT' 中的標識列插入顯式值。我是在SqlServer寫存儲過程中遇到的這個錯誤,當時就心想:臨時表怎麼會有主鍵呢,我也沒有設置主鍵。然後我就和同事一塊調試,終於發現了。原因就是我把真實的數據表中id(其實就是主鍵)取出來放到臨時 ...
  • 1、準備文件並設置編碼格式為UTF-8並上傳Linux 2、新建一個Java Project 3、導入jar 4、編寫Map()和Reduce() 5、將代碼輸出成jar 6、在linux中啟動hdfs 7、修改兩個配置文件 8、在linux中啟動yarn 9、運行mapReduce 10、查看運行 ...
  • nfluxDB是一個當下比較流行的時序資料庫,InfluxDB使用 Go 語言編寫,無需外部依賴,安裝配置非常方便,適合構建大型分散式系統的監控系統。 本文是一系列InfluxDB學習教程的目錄,現主要包含以下文章。 InfluxDB學習之InfluxDB的安裝和簡介 InfluxDB學習之Infl ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...