DROP USER過程中出現的奇怪的物件資訊

yangtingkun發表於2009-10-07

在刪除使用者的時候,在後臺查詢了一下DBA_SEGMENTS檢視,結果發現其他的物件資訊。

 

 

在一個會話上執行使用者的刪除操作:

SQL> SELECT DISTINCT OWNER, TABLESPACE_NAME FROM DBA_SEGMENTS;

OWNER                          TABLESPACE_NAME
------------------------------ ------------------------------
SYSTEM                         SYSAUX
SYSMAN                         SYSAUX
SYS                            SYSTEM
OUTLN                          SYSTEM
DBSNMP                         SYSAUX
GPO_OPERATOR                   GPO
WMSYS                          SYSAUX
ORDSYS                         SYSAUX
MDSYS                          SYSAUX
ZHEJIANG_KHD                   ZHEJIANG
SYS                            UNDOTBS2
SYSTEM                         SYSTEM
TSMSYS                         SYSAUX
EXFSYS                         SYSAUX
CTXSYS                         SYSAUX
DMSYS                          SYSAUX
NDMAIN                         NDMAIN
SPOTLIGHT                      SYSAUX
SYS                            UNDOTBS1
SYS                            SYSAUX
XDB                            SYSAUX
OLAPSYS                        SYSAUX
GPO                            GPO
GPO_BUYER_CATALOG              GPO
ZHEJIANG                       ZHEJIANG
GPO_SHANGHAI_DATA              GPO
ZHEJIANG_OPERATOR              ZHEJIANG
GPO_SALER_BID                  GPO

已選擇28行。

SQL> SELECT COUNT(*) 
  2  FROM DBA_SEGMENTS
  3  WHERE TABLESPACE_NAME = 'GPO';

  COUNT(*)
----------
      1550

SQL> BEGIN
  2  FOR I IN (SELECT USERNAME FROM DBA_USERS WHERE USERNAME LIKE 'GPO%') LOOP
  3  EXECUTE IMMEDIATE 'DROP USER ' || I.USERNAME || ' CASCADE';
  4  END LOOP;
  5  END;
  6  /

PL/SQL 過程已成功完成。

DROP USER的過程中,在另一個會話中檢查DBA_SEGEMNTS檢視,意外發現了奇怪的物件:

SQL> SELECT OWNER, SEGMENT_NAME, TABLESPACE_NAME
  2  FROM DBA_SEGMENTS
  3  WHERE TABLESPACE_NAME = 'GPO';

OWNER                          SEGMENT_NAME                   TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
GPO                            GPO_BAL_ORG_DEFRAY_HIS         GPO
GPO                            GPO_BAL_ORG_DEFRAY_LOG         GPO
GPO                            GPO_BAL_ORG_PRO                GPO
GPO                            GPO_BAL_USER_INFO              GPO
GPO                            GPO_BID_NOTICE                 GPO
.
.
.
GPO                            IDX_GPO_BID_RESULT_PROD        GPO
GPO                            IDX_GPO_HIT_COMM_1             GPO
GPO                            41.97672                       GPO
GPO                            41.16904                       GPO

已選擇153行。

其他物件都很正常,但是最後兩個物件名稱很奇怪,可以確定這兩個物件的名稱不是使用者建立時指定的。

由於同時在執行刪除操作,很可能是Oracle在刪除物件時從資料字典中去掉了一些資訊,導致了這樣的顯示結果。

不過這兩個物件的名稱肯定有一定的意義,肯定不可能是兩個任意的小數。

前面那個41難道是執行DROP使用者的會話:

SQL> SELECT COUNT(*) 
  2  FROM DBA_SEGMENTS
  3  WHERE TABLESPACE_NAME = 'GPO';

  COUNT(*)
----------
         0

SQL> SELECT SID FROM V$MYSTAT WHERE ROWNUM = 1;

       SID
----------
       312

回到刪除使用者的會話執行上面的SQL,發現會話ID312。看來和會話資訊沒有關係。

既然是DBA_SEGMENTS,那麼這個數字多半和儲存位置有關係,難道是FILE.BLOCK的格式,檢查資料檔案:

SQL> SELECT FILE_ID, FILE_NAME, TABLESPACE_NAME
  2  FROM DBA_DATA_FILES
  3  WHERE FILE_ID = 41;

   FILE_ID FILE_NAME                                          TABLESPACE_NAME
---------- -------------------------------------------------- ------------------------------
        41 +MEMBER/tradedb/tradedb_gpo_2_4g                   GPO

這個結果已經足夠說明問題了。不過由於物件已經被刪除,因此從DBA_EXTENTS檢視中無法獲得刪除物件的BLOCK資訊,即使使用閃回查詢也得不到結果。

幸好當時資料庫是利用利用源資料庫的備份恢復出來的,因此物理結構和源資料庫保持一致。在源資料庫中對DBA_EXTENTS進行查詢:

SQL> SELECT OWNER, SEGMENT_NAME
  2  FROM DBA_EXTENTS
  3  WHERE FILE_ID = 41
  4  AND BLOCK_ID <= 16904
  5  AND BLOCK_ID + BLOCKS >= 16904;

OWNER                          SEGMENT_NAME
------------------------------ --------------------------------------------------
GPO                            GPO_BAL_ORG_DEFRAY

SQL> SELECT OWNER, SEGMENT_NAME
  2  FROM DBA_EXTENTS
  3  WHERE FILE_ID = 41
  4  AND BLOCK_ID <= 97672
  5  AND BLOCK_ID + BLOCKS >= 97672;

OWNER                          SEGMENT_NAME
------------------------------ -------------------------------------------------
GPO                            PK_GPO_BAL_ORG_DEFRAY

不用再做進一步的查詢,從名稱上已經可以看到二者是表和主鍵的關係。

其實不只是刪除物件的過程,在新增物件時,也可能出現這種臨時物件:

SQL> BEGIN
  2  FOR I IN 1..2 LOOP
  3  EXECUTE IMMEDIATE 'CREATE TABLE T' || I || ' AS SELECT * FROM DBA_OBJECTS';
  4  END LOOP;
  5  END;
  6  /

PL/SQL procedure successfully completed.

在這個SQL的執行過程中,在另一個節點上執行查詢:

SQL> SELECT OWNER, SEGMENT_NAME, TABLESPACE_NAME
  2  FROM DBA_SEGMENTS
  3  WHERE TABLESPACE_NAME = 'YANGTK'
  4  AND WNER = 'A';

OWNER                          SEGMENT_NAME                   TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
A                              T1                             YANGTK
A                              T2                             YANGTK
A                              T3                             YANGTK
A                              T4                             YANGTK
A                              T5                             YANGTK
A                              T6                             YANGTK
A                              T7                             YANGTK
A                              T8                             YANGTK
A                              T9                             YANGTK
A                              T10                            YANGTK
A                              T11                            YANGTK
A                              T12                            YANGTK
A                              T13                            YANGTK
A                              T14                            YANGTK
A                              8.7571                         YANGTK

15 rows selected.

等語句執行完成,檢查建立的物件資訊:

SQL> SELECT OWNER, SEGMENT_NAME, TABLESPACE_NAME
  2  FROM DBA_EXTENTS
  3  WHERE FILE_ID = 8
  4  AND BLOCK_ID <= 7571
  5  AND BLOCK_ID + BLOCKS >= 7571;

OWNER                          SEGMENT_NAME                   TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
A                              T15                            YANGTK

Oracle在建立或刪除物件的過程中,可能會臨時使用物件的儲存位置資訊來代替物件的名稱。

 

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

相關文章