前言:在oracle運維的過程當中,常常會使用到一些以V$開頭的動態視圖,好比V$session, 有一次偶然看到有人用V_$session, 初覺得別人寫錯了,沒想到desc v_$session之後能看到和v$session同樣的結構,再之後又發現以gv$開頭的視圖等等。趁此次在一臺Linux系統上裝oracle的機會,終於弄清楚了這些動態視圖與相應表之間的關係。sql
這些都是由oracle本身管理的數據結構,得從v$fixed_table入手:數據庫
[oracle@3857 admin]$ sqlplus sys/sys@archdw as sysdbasession
SQL*Plus: Release 11.2.0.1.0 Production on Mon Dec 14 11:27:20 2009數據結構
Copyright (c) 1982, 2009, Oracle. All rights reserved.oracle
Connected to:運維
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 – Production.net
With the Partitioning, OLAP, Data Mining and Real Application Testing optionsip
SQL> desc v$fixed_table;get
Name Null? Typeit
– ——–
NAME VARCHAR2(30)
OBJECT_ID NUMBER
TYPE VARCHAR2(5)
TABLE_NUM NUMBER
SQL> select * from v$fixed_table
NAME OBJECT_ID TYPE TABLE_NUM
———- —– ———-
X$KQFTA 4294950912 TABLE 0
X$KQFVI 4294950913 TABLE 1
GV$PROCESS 4294951256 VIEW 65537
V$PROCESS 4294950917 VIEW 65537
GV$BGPROCESS 4294951257 VIEW 65537
………………………………………
從上面能夠看到GV$與V$是視圖,X$是表。那它們之間是什麼關係呢?從另外一個視圖v$fixed_view_definition中獲得以下信息(以v$fixed_table爲例):
SQL> set linesize 100
SQL> col view_name for a15
SQL> col view_definition for a80
SQL> select * from v$fixed_view_definition where view_name=’V$FIXED_TABLE’;
VIEW_NAME VIEW_DEFINITION
———————- –
V$FIXED_TABLE select NAME , OBJECT_ID , TYPE , TABLE_NUM from GV$FIXED_TABLE where inst_id =
USERENV(’Instance’)
SQL> select * from v$fixed_view_definition where view_name=’GV$FIXED_TABLE’;
VIEW_NAME VIEW_DEFINITION
————————- –
GV$FIXED_TABLE select inst_id,kqftanam, kqftaobj, ‘TABLE’, indx from x$kqfta union all select i
nst_id,kqfvinam, kqfviobj, ‘VIEW’, 65537 from x$kqfvi union all select inst_id,k
qfdtnam, kqfdtobj, ‘TABLE’, 65537 from x$kqfdt
原來gv$是全局視圖,而v$是針對某個實例的視圖,$X是全部gv$的數據來源,從gv$到v$須要加上where inst_id = USERENV(’Instance’)。通常來講一個oracle數據庫只會有一個實例對其操做,但在RAC上能夠有多臺實例同時裝載並打開一個數據庫,在RAC上獲得的結果是:
etl@ALIDW> select distinct inst_id from gv$session;
INST_ID
———-
1
2
4
3
這rac上有四個實例。嗯,再次加深了對實例與數據庫的理解。
那gv_$與v_$的定義又在什麼地方呢?原來在$ORACLE_HOME/rdbms/admin存放着系統管理腳本,在catalog.sql中發現:
–CATCTL -S Initial scripts single process
@@cdstrt
@@cdfixed.sql
@@cdcore.sql
–CATCTL -M
@@cdplsql.sql
@@cdsqlddl.sql
進一步在cdfixed.sql中找到
create or replace view v_$fixed_table as select * from v$fixed_table;
create or replace public synonym v$fixed_table for v_$fixed_table;
grant select on v_$fixed_table to select_catalog_role;
create or replace view gv_$fixed_table as select * from gv$fixed_table;
create or replace public synonym gv$fixed_table for gv_$fixed_table;
grant select on gv_$fixed_table to select_catalog_role;
[oracle@3857 admin]$ sqlplus sys/sys@archdw as sysdba
SQL> select * from user_role_privs;
USERNAME GRANTED_ROLE ADM DEF OS_
— — —
SYS SELECT_CATALOG_ROLE YES YES NO
所以咱們經常使用的v$ 是v_$的同義詞,v_$是基於真正的視圖v$,而真正的v$視圖是在gv$的基礎上限制inst_id獲得;
咱們經常使用的gv$是gv_$的同義詞,gv_$基於真正的視圖gv$,而真正的gv$視圖基於系統表X$。