炼数成金 门户 大数据 Oracle 查看内容

Oracle之数据字典屣履造门

2013-7-29 12:00| 发布者: 天空之城| 查看: 3757| 评论: 0|原作者: 网络转载|来自: 比特网

摘要: oracle之数据字典屣履造门   oracle数据字典包括四部分,分别是RDBMS(X$),数据字典表、动态性能视图(v$)和数据字典视图。   1)RDBMS(X$)内部 表:   该部分内容是oracle最低层的表数据,这些表维持着oracle的整 ...

数据库 SQL Oracle BI RAC

       oracle之数据字典屣履造门
  
       oracle数据字典包括四部分,分别是RDBMS(X$),数据字典表、动态性能视图(v$)和数据字典视图。

  1)RDBMS(X$)内部 表:

  该部分内容是oracle较低层的表数据,这些表维持着oracle的整个视图,就像我们人体的血液在我们人体流动维持生命一样。

  因此这些表只能查看、研究不建议修改。oracle对修改内部表产生的后果不负责技术支持工作。

  在查看参数文件的时候我曾经提到x$ksppi和X$ksppcv这两个内部表。

  研究内部表:

  oracle@oracle:~> SQLplus "/as sysdba"

  SQL*Plus: Release 11.1.0.6.0 - Production on 星期四 6月 20 20:38:47 2013

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

  连接到:

  Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production

  With the Partitioning, OLAP, Data Mining and Real Application Testing options

  SYS@orcl#select open_mode from v$database;

  OPEN_MODE

  --------------------

  READ WRITE

  SYS@orcl#grant select on x$ksppi to xiaohai;

  grant select on x$ksppi to xiaohai

  *

  第 1 行出现错误:

  ORA-02030: 只能从固定的表/视图查询

  可以看出内部表不允许进行授权。

  研究内部表方法如下:

  SYS@orcl#set autotrace trace explain;

  SYS@orcl#select * from v$parameter;

  执行计划

  ----------------------------------------------------------

  Plan hash value: 1128103955

  ------------------------------------------------------------------------------

  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

  ------------------------------------------------------------------------------

  | 0 | SELECT STATEMENT | | 1 | 4414 | 1 (100)| 00:00:01 |

  |* 1 | HASH JOIN | | 1 | 4414 | 1 (100)| 00:00:01 |

  |* 2 | FIXED TABLE FULL| X$KSPPI | 1 | 249 | 0 (0)| 00:00:01 |

  | 3 | FIXED TABLE FULL| X$KSPPCV | 100 | 406K| 0 (0)| 00:00:01 |

  ------------------------------------------------------------------------------

  Predicate Information (identified by operation id):

  ---------------------------------------------------

  1 - access("X"."INDX"="Y"."INDX")

  filter(TRANSLATE("KSPPINM",'_','#') NOT LIKE '#%' OR

  "KSPPSTDF"='FALSE' OR BITAND("KSPPSTVF",5)>0)

  2 - filter("X"."INST_ID"=USERENV('INSTANCE') AND

  TRANSLATE("KSPPINM",'_','#') NOT LIKE '##%')

  SYS@orcl#

  autotrace 语句研究:

  该语句工具主要是研究统计sql语句的执行计划并生成报告,进而对dml语句进行检测和优化。

  Generates a report on the execution path used by the SQL optimizer and the statement execution statistics. The report is useful to monitor and tune the performance of DML statements.

  set autotrace off; 不产生报告和执行计划,默认设置。

  set autotrace on explain; 生成执行计划并显示查询信息;

  set autotrace on statistics;只生成语句的统计信息并显示查询信息。

  set autotrace on ; 即生成语句报告有显示语句的执行统计信息和计划

  set autotrace traceonly; 和on一样,区别就是不显示查询信息,查询的数据依然会fetch但是不会print,只显示统计信息和执行计划;

  eg:

  SYS@orcl#set autotrace on explain

  SYS@orcl#select * from t2;

  EMPNO ENAME

  ---------- --------------------

  7521 WARD

  执行计划

  ----------------------------------------------------------

  Plan hash value: 1513984157

  --------------------------------------------------------------------------

  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

  --------------------------------------------------------------------------

  | 0 | SELECT STATEMENT | | 1 | 20 | 2 (0)| 00:00:01 |

  | 1 | TABLE ACCESS FULL| T2 | 1 | 20 | 2 (0)| 00:00:01 |

  --------------------------------------------------------------------------

  Note

  -----

  - dynamic sampling used for this statement

  SYS@orcl#set autotrace on statistices;

  SP2-0735: 未知的 SET 选项开头 "statistice..."

  SYS@orcl#set autotrace on statistics;

  SYS@orcl#select * from t2;

  EMPNO ENAME

  ---------- --------------------

  7521 WARD

  统计信息

  ----------------------------------------------------------

  0 recursive calls

  0 db block gets

  3 consistent gets

  0 physical reads

  0 redo size

  600 bytes sent via SQL*Net to client

  524 bytes received via SQL*Net from client

  2 SQL*Net roundtrips to/from client

  0 sorts (memory)

  0 sorts (disk)

  1 rows processed

  SYS@orcl#select * from t2;

  EMPNO ENAME

  ---------- --------------------

  7521 WARD

  执行计划

  ----------------------------------------------------------

  Plan hash value: 1513984157

  --------------------------------------------------------------------------

  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

  --------------------------------------------------------------------------

  | 0 | SELECT STATEMENT | | 1 | 20 | 2 (0)| 00:00:01 |

  | 1 | TABLE ACCESS FULL| T2 | 1 | 20 | 2 (0)| 00:00:01 |

  --------------------------------------------------------------------------

  Note

  -----

  - dynamic sampling used for this statement

  统计信息

  ----------------------------------------------------------

  0 recursive calls

  0 db block gets

  3 consistent gets

  0 physical reads

  0 redo size

  600 bytes sent via SQL*Net to client

  524 bytes received via SQL*Net from client

  2 SQL*Net roundtrips to/from client

  0 sorts (memory)

  0 sorts (disk)

  1 rows processed

  SYS@orcl#

  SYS@orcl#set autotrace traceonly;

  SYS@orcl#select * from t;

  执行计划

  ----------------------------------------------------------

  Plan hash value: 1601196873

  --------------------------------------------------------------------------

  | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

  --------------------------------------------------------------------------

  | 0 | SELECT STATEMENT | | 5 | 180 | 3 (0)| 00:00:01 |

  | 1 | TABLE ACCESS FULL| T | 5 | 180 | 3 (0)| 00:00:01 |

  --------------------------------------------------------------------------

  统计信息

  ----------------------------------------------------------

  0 recursive calls

  0 db block gets

  4 consistent gets

  0 physical reads

  0 redo size

  1184 bytes sent via SQL*Net to client

  524 bytes received via SQL*Net from client

  2 SQL*Net roundtrips to/from client

  0 sorts (memory)

  0 sorts (disk)

  5 rows processed

  总结:该工具可以查看语句的统计信息可以变化命令使用非常灵活,

  SYS@orcl#set autotrace

  用法: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]

  便于语句调优。在此记录加深一下印象。呵呵,原来这个工具还有多种花样。

  X$kvit 内部表:英文名字叫:kernel layer performance layer information tables transitory instance parameters;

  #select kvitval,kvittag,kvitdsc from x$kvit

  2 ;

  KVITVAL KVITTAG KVITDSC

  ---------- -------------------- --------------------------------------------------------------------------------

  1 ksbcpu number of logical CPUs in the system used by Oracle

  0 ksbcpucore number of physical CPU cores in the system used by Oracle

  0 ksbcpusocket number of physical CPU sockets in the system used by Oracle

  1 ksbcpu_hwm high water mark of number of CPUs used by Oracle

  0 ksbcpucore_hwm high water mark of number of CPU cores on system

  0 ksbcpusocket_hwm high water mark of number of CPU sockets on system

  1 ksbcpu_actual number of available CPUs in the system

  1 ksbcpu_dr CPU dynamic reconfiguration supported

  33456 kcbnbh number of buffers

  25 kcbldq large dirty queue if kcbclw reaches this

  40 kcbfsp Max percentage of LRU list foreground can scan for free

  KVITVAL KVITTAG KVITDSC

  ---------- -------------------- --------------------------------------------------------------------------------

  2 kcbcln Initial percentage of LRU list to keep clean

  800 kcbnbf number buffer objects

  0 kcbwst Flag that indicates recovery or db suspension

  0 kcteln Error Log Number for thread open

  0 kcvgcw SGA: opcode for checkpoint cross-instance call

  0 kcvgcw SGA:opcode for pq checkpoint cross-instance call

  已选择17行。

  SYS@orcl#

  kcbldq和kcbfsp是关于dbwr进程进行脏数据写入磁盘的条件。

  如下:

  SYS@orcl#@getsp.sql

  输入 par 的值: db_large_dirty

  NAME VALUE PDESC

  ------------------------------ -------------------- --------------------------------------------------

  _db_large_dirty_queue 25 Number of buffers which force dirty queue to be wr

  itten

  SYS@orcl#

  SYS@orcl#@getsp.sql

  输入 par 的值: db_block_max_scan

  NAME VALUE PDESC

  ------------------------------ -------------------- --------------------------------------------------

  _db_block_max_scan_pct 40 Percentage of buffers to inspect when looking for

  free

  SYS@orcl#

  总结:从这些信息我们可以研究出了lru原理,知道了dbwr进行脏数据写入磁盘的条件。

  为了学习,进行尝试修改如下:

  SYS@orcl#alter system set "_db_block_max_scan_pct"=50 scope=spfile;

  系统已更改。

  SYS@orcl#startup force;

  ORACLE 例程已经启动。

  Total System Global Area 805875712 bytes

  Fixed Size 2148720 bytes

  Variable Size 562038416 bytes

  Database Buffers 234881024 bytes

  Redo Buffers 6807552 bytes

  数据库已经打开。

  SYS@orcl#show parameter _db_block

  NAME TYPE VALUE

  ------------------------------------ ---------------------- ------------------------------

  _db_block_max_scan_pct integer 50

  SYS@orcl#exit

  从 Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production

  With the Partitioning, OLAP, Data Mining and Real Application Testing options 断开

  oracle@oracle:~/test> sqlplus "/as sysdba"

  SQL*Plus: Release 11.1.0.6.0 - Production on 星期四 6月 20 21:25:47 2013

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

  连接到:

  Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production

  With the Partitioning, OLAP, Data Mining and Real Application Testing options

  SYS@orcl#@getsp.sql

  输入 par 的值: db_block_max_scan

  原值 2: where x.indx=y.indx and x.ksppinm like '%&par%'

  新值 2: where x.indx=y.indx and x.ksppinm like '%db_block_max_scan%'

  NAME VALUE PDESC

  ------------------------------ -------------------- --------------------------------------------------

  _db_block_max_scan_pct 50 Percentage of buffers to inspect when looking for

  free

  SYS@orcl#

  可以看到我们修改的实施在spfile中加入隐含参数,然后在数据启动初始化的时候会引用隐含参数的值。(不建议修改)

  SYS@orcl#alter system reset "_db_block_max_scan_pct";

  系统已更改。

  SYS@orcl#startup force;

  2)数据字典表:

  在创建数据库的时候安装sql.bsp文件进行创建,在数据库启动的时候进行先关字典表的创建。可以通过跟踪数据库启动的过程进行分析。

  eg:

  startup nomount;

  alter session set events '10046 trace nam context level 12';

  alter database mount;

  alter database open;

  如下是截取的跟踪文件信息:

  509 CREATE TABLE USER$("USER#" NUMBER NOT NULL,"NAME" VARCHAR2(30) NOT NULL,"TYPE#" NUMBER NOT NULL,"PASSWORD" VARCHAR2(30),"DATATS#" NUMBER NOT NULL,"TEMPTS#" NUM BER NOT NULL,"CTIME" DATE NOT NULL,"PTIME" DATE,"EXPTIME" DATE,"LTIME" DATE,"RESOURCE$" NUMBER NOT NULL,"AUDIT$" VARCHAR2(38),"DEFROLE" NUMBER NOT NULL,"DEFGRP #" NUMBER,"DEFGRP_SEQ#" NUMBER,"ASTATUS" NUMBER NOT NULL,"LCOUNT" NUMBER NOT NULL,"DEFSCHCLASS" VARCHAR2(30),"EXT_USERNAME" VARCHAR2(4000),"SPARE1" NUMBER,"SPA RE2" NUMBER,"SPARE3" NUMBER,"SPARE4" VARCHAR2(1000),"SPARE5" VARCHAR2(1000),"SPARE6" DATE) STORAGE ( OBJNO 22 TABNO 1) CLUSTER C_USER#(USER#)

  510 END OF STMT

  511 PARSE #1:c=0,e=456,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1371736252499680

  512 BINDS #1:

  513 EXEC #1:c=0,e=217,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=4,tim=1371736252499956

  =====================

  480 PARSING IN CURSOR #1 len=637 dep=1 uid=0 oct=1 lid=0 tim=1371736252496043 hv=1077251290 ad='8fd93c08' sqlid='3cgdmzx03b36u'

  481 CREATE TABLE OBJ$("OBJ#" NUMBER NOT NULL,"DATAOBJ#" NUMBER,"OWNER#" NUMBER NOT NULL,"NAME" VARCHAR2(30) NOT NULL,"NAMESPACE" NUMBER NOT NULL,"SUBNAME" VARCHAR2 (30),"TYPE#" NUMBER NOT NULL,"CTIME" DATE NOT NULL,"MTIME" DATE NOT NULL,"STIME" DATE NOT NULL,"STATUS" NUMBER NOT NULL,"REMOTEOWNER" VARCHAR2(30),"LINKNAME" V ARCHAR2(128),"FLAGS" NUMBER,"OID$" RAW(16),"SPARE1" NUMBER,"SPARE2" NUMBER,"SPARE3" NUMBER,"SPARE4" VARCHAR2(1000),"SPARE5" VARCHAR2(1000),"SPARE6" DATE) PCTFR EE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE ( INITIAL 16K NEXT 1024K MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 OBJNO 18 EXTENTS (FILE 1 BLOCK 121) )

  482 END OF STMT

  483 PARSE #1:c=0,e=552,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1371736252496039

  484 BINDS #1:

  485 EXEC #1:c=0,e=205,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=4,tim=1371736252496308

  当我们创建表的时候,其实是向先关数据字典写入数据,分别是obj$,con$,col$,tab$等等。可以跟踪sql语句进行分析。

  当然了我们创建完表之后,可以通过包进行查看我们当时的ddl语句。

  eg:

  SYS@orcl#select table_name from user_tables where table_name='T';

  TABLE_NAME

  ------------------------------------------------------------

  T

  SYS@orcl#select dbms_metadata.get_ddl('TABLE','T') FROM DUAL;

  DBMS_METADATA.GET_DDL('TABLE','T')

  --------------------------------------------------------------------------------

  CREATE TABLE "SYS"."T"

  ( "TABLESPACE_NAME" VARCHAR2(30) NOT NULL ENABLE,

  SYS@orcl#SET LONG 20000

  SYS@orcl#R

  1* select dbms_metadata.get_ddl('TABLE','T') FROM DUAL

  DBMS_METADATA.GET_DDL('TABLE','T')

  --------------------------------------------------------------------------------

  CREATE TABLE "SYS"."T"

  ( "TABLESPACE_NAME" VARCHAR2(30) NOT NULL ENABLE,

  "USERNAME" VARCHAR2(30) NOT NULL ENABLE,

  "BYTES" NUMBER,

  "MAX_BYTES" NUMBER,

  "BLOCKS" NUMBER,

  "MAX_BLOCKS" NUMBER,

  "DROPPED" VARCHAR2(3)

  ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING

  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645

  DBMS_METADATA.GET_DDL('TABLE','T')

  --------------------------------------------------------------------------------

  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)

  TABLESPACE "USERS"

  研究一下这个dbms_metadata包。

  这个包的接受如下:

  Overview

  This pkg implements the mdAPI, a means to retrieve the aggregated

  definitions of database objects as either XML docs. or their creation DDL,

  or to submit the XML documents to execute the DDL.

  -------------------------------------------------------------------

  SECURITY

  This package is owned by SYS with execute access granted to PUBLIC.

  It runs with invokers rights, i.e., with the security profile of

  the caller. It calls DBMS_METADATA_INT to perform privileged

  functions.

  The object views defined in catmeta.sql implement the package's security

  policy via the WHERE clause on the public views which include syntax to

  control user access to metadata: if the current user is SYS or has

  SELECT_CATALOG_ROLE, then all objects are visible; otherwise, only

  objects in the schema of the current user are visible.

  然后我们在看看这个function是get_ddl内容如下:

  FUNCTION get_ddl (

  object_type IN VARCHAR2,

  name IN VARCHAR2,

  schema IN VARCHAR2 DEFAULT NULL,

  version IN VARCHAR2 DEFAULT 'COMPATIBLE',

  model IN VARCHAR2 DEFAULT 'ORACLE',

  transform IN VARCHAR2 DEFAULT 'DDL')

  RETURN CLOB;

  那么我刚刚传的参数是object_type为table,name为T。另外这个包还有很多功能,后续多多实践进行学习。

  3)静态数据字典视图。

  包括dba_视图,all_视图,user_视图。上级包括下级。如dba_tables,all_tables,user_tables;

  eg:

  SYS@orcl#set autotrace traceonly explain;

  SYS@orcl#select * from dba_tables;

  Predicate Information (identified by operation id):

  ---------------------------------------------------

  1 - access("KSPPI"."INDX"="KSPPCV"."INDX")

  4 - access("T"."BOBJ#"="CO"."OBJ#"(+))

  5 - access("CX"."OWNER#"="CU"."USER#"(+))

  7 - access("T"."DATAOBJ#"="CX"."OBJ#"(+))

  8 - access("O"."OWNER#"="U"."USER#")

  10 - access("T"."TS#"="TS"."TS#")

  12 - access("T"."FILE#"="S"."FILE#"(+) AND "T"."BLOCK#"="S"."BLOCK#"(+) AND

  "T"."TS#"="S"."TS#"(+))

  13 - access("O"."OBJ#"="T"."OBJ#")

  14 - filter(BITAND("T"."PROPERTY",1)=0)

  15 - filter(BITAND("O"."FLAGS",128)=0)

  20 - filter("KSPPI"."KSPPINM"='_dml_monitoring_enabled')

  SYS@orcl#select * from all_tables;

  Predicate Information (identified by operation id):

  ---------------------------------------------------

  1 - filter("O"."OWNER#"=USERENV('SCHEMAID') OR EXISTS (SELECT 0 FROM

  "SYS"."OBJAUTH$" "OA",SYS."X$KZSRO" "X$KZSRO" WHERE "GRANTEE#"="KZ

  SROROL" AND

  "OA"."OBJ#"=:B1) OR EXISTS (SELECT 0 FROM SYS."X$KZSPR" "X$KZSPR"

  WHERE

  "INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45) OR (-"KZSPR

  PRV")=(-47) OR

  (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR (-"KZSPRPRV")=(-50))

  ))

  SYS@orcl#select * from user_tables;

  Predicate Information (identified by operation id):

  ---------------------------------------------------

  1 - access("KSPPI"."INDX"="KSPPCV"."INDX")

  4 - access("T"."BOBJ#"="CO"."OBJ#"(+))

  5 - access("CX"."OWNER#"="CU"."USER#"(+))

  7 - access("T"."DATAOBJ#"="CX"."OBJ#"(+))

  8 - access("T"."TS#"="TS"."TS#")

  10 - access("T"."FILE#"="S"."FILE#"(+) AND "T"."BLOCK#"="S"."BLOCK#"(+) AND

  "T"."TS#"="S"."TS#"(+))

  12 - filter("O"."OWNER#"=USERENV('SCHEMAID') AND BITAND("O"."FLAGS",128)=0)

  13 - filter(BITAND("T"."PROPERTY",1)=0)

  14 - access("O"."OBJ#"="T"."OBJ#")

  19 - filter("KSPPI"."KSPPINM"='_dml_monitoring_enabled')

  显而易见了。呵呵。
4

鲜花
2

握手

雷人

路过

鸡蛋

刚表态过的朋友 (6 人)

最新评论

热门频道

  • 大数据
  • 商业智能
  • 量化投资
  • 科学探索
  • 创业

即将开课

 

GMT+8, 2017-11-21 20:16 , Processed in 0.167505 second(s), 23 queries .