Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1791678
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Oracle

2016-05-15 22:48:23

因为项目需要,先前学过的oracle需要重新拾起来,根据自己的理解,同时参考了tom大神的神作,进行了梳理。

当你接手一个陌生的环境,你需要有一个初步的了解:

点击(此处)折叠或打开

  1. show user
  2. show errors
  3. select * from v$version;
  4. select instance_name from v$instance;
  5. select database_name from v$database;
  6. select userenv('language') from dual;
  7. select * from v$logfile;
  8. select * from v$datafile;
  9. select * from controlfile;
  10. desc dual;


作为一个DBA,还需要了解就是oracle的start涉及的几个phase:

start nomout: 构建oracle实例


点击(此处)折叠或打开

  1. Starting an instance includes the following tasks:
  2. ? Searching $ORACLE_HOME/dbs for a file of a particular name in this sequence:
  3. 1. Search for spfile.ora.
  4. 2. If spfile.ora is not found, search for spfile.ora.
  5. 3. If spfile.ora is not found, search for init.ora.
  6. This is the file that contains initialization parameters for the instance. Specifying the
  7. PFILE parameter with STARTUP overrides the default behavior.
  8. ? Allocating the SGA
  9. ? Starting the background processes
  10. ? Opening the alert_.log file and the trace files

mount:alter database mount:


点击(此处)折叠或打开

  1. Mounting a database includes the following tasks:
  2. ? Associating a database with a previously started instance
  3. ? Locating and opening all of the control files specified in the parameter file
  4. ? Reading the control files to obtain the names and statuses of the data files and online redo
  5. log files (However, no checks are performed to verify the existence of the data files and
  6. online redo log files at this time.)
open:

点击(此处)折叠或打开

  1. Opening the database includes the following tasks:
  2. ? Opening the data files
  3. ? Opening the online redo log files



点击(此处)折叠或打开

  1. SQL> startup nomount
  2. ORACLE 例程已经启动。

  3. Total System Global Area 778387456 bytes
  4. Fixed Size 1374808 bytes
  5. Variable Size 494929320 bytes
  6. Database Buffers 276824064 bytes
  7. Redo Buffers 5259264 bytes
  8. SQL> select * from v$controlfile;

  9. 未选定行
  10. SQL> select 'abc' from dual;

  11. 'ABC'
  12. ------
  13. abc
  14. SQL> select value from v$spparameter where name='control_files';

  15. VALUE
  16. --------------------------------------------------------------------------------

  17. H:\app\kinfinger\oradata\rmfdb\control01.ctl
  18. H:\app\kinfinger\flash_recovery_area\rmfdb\control02.ctl

  19. SQL> select * from v$datafile;
  20.  select * from v$datafile
  21.                *
  22. 第 1 行出现错误:
  23. ORA-01507: ??????
  24. SQL> select table_name from user_tables;
  25. select table_name from user_tables
  26.                        *
  27. 第 1 行出现错误:
  28. ORA-01219: ??????: ???????/?????
  29. ORA-01219:database not open:queries allowed on fixed tables/views only
  30. “ORA-01219:数据库未打开:仅允许在固定表/视图中查询”的警告窗口!
  31. 读取init.ora文件,启动instance,即启动SGA和后台进程,这种启动只需要init.ora文件。
  32. SQL> alter database mount;

  33. 数据库已更改。


出于测试目的,这里以windows平台为测试环境。由于oracle本身对系统资源消耗巨大,这里设置为手动启动,自己根据需要启动所需要的地址空间。


点击(此处)折叠或打开

  1. lsnrctl status
  2. 正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
  3. TNS-12541: TNS: 无监听程序
  4.  TNS-12560: TNS: 协议适配器错误
  5.   TNS-00511: 无监听程序
  6.    32-bit Windows Error: 2: No such file or directory

  7. C:\Windows\system32>net start OracleServiceRMFDB
  8. OracleServiceRMFDB 服务正在启动 ......................
  9. OracleServiceRMFDB 服务已经启动成功。

  10. C:\Windows\system32>lsnrctl status

  11. LSNRCTL for 32-bit Windows: Version 11.2.0.1.0 - Production on 15-5月 -2016 16:2
  12. 7:27

  13. Copyright (c) 1991, 2010, Oracle. All rights reserved.

  14. 正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=kinfinger-PC)(PORT=1521)))
  15. LISTENER 的 STATUS
  16. ------------------------
  17. 别名 LISTENER
  18. 版本 TNSLSNR for 32-bit Windows: Version 11.2.0.1.0 - Produ
  19. ction
  20. 启动日期 15-5月 -2016 16:20:17
  21. 正常运行时间 0 天 0 小时 7 分 12 秒
  22. 跟踪级别 off
  23. 安全性 ON: Local OS Authentication
  24. SNMP OFF
  25. 监听程序参数文件 H:\app\kinfinger\product\11.2.0\dbhome_1\network\admin
  26. \listener.ora
  27. 监听程序日志文件 h:\app\kinfinger\diag\tnslsnr\kinfinger-PC\listener\al
  28. ert\log.xml
  29. 监听端点概要...
  30.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=kinfinger-PC)(PORT=1521)))
  31.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))
  32. 服务摘要..
  33. 服务 "CLRExtProc" 包含 1 个实例。
  34.   实例 "CLRExtProc", 状态 UNKNOWN, 包含此服务的 1 个处理程序...
  35. 服务 "rmfdb" 包含 1 个实例。
  36.   实例 "rmfdb", 状态 READY, 包含此服务的 1 个处理程序...
  37. 服务 "rmfdbXDB" 包含 1 个实例。
  38.   实例 "rmfdb", 状态 READY, 包含此服务的 1 个处理程序...
  39. 命令执行成功


特别注意:这里CMD需要有管理员权限,否则,监听器启动不起来。且没有任何报错误提示信息。

在启动监听器的时候,oracle实例没有注册,只有在启动实例后,status显示实例注册成功。至于注册2个实例的区别,后续研究。

当你需要连接到server时,你需要配置SQLNET.ORA,,需要用到一个调试工具TNSPING


点击(此处)折叠或打开

  1. H:\oracleexp>TnsPING LOCALHOST

  2. TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 15-5月 -
  3. 2016 21:55:40

  4. Copyright (c) 1997, 2010, Oracle. All rights reserved.

  5. 已使用的参数文件:
  6. H:\app\kinfinger\product\11.2.0\dbhome_1\network\admin\sqlnet.ora

  7. 已使用 EZCONNECT 适配器来解析别名
  8. 尝试连接 (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST
  9. =127.0.0.1)(PORT=1521)))
  10. OK (40 毫秒)

环境配置以及启动搞定以后,作为DBA,首先需要熟悉的就是你的工具sqlplus

这里学习一下TOM 大神的表空间监控脚本:


点击(此处)折叠或打开

  1. set serveroutput on size 1000000;
  2. create or replace procedure show_space(
  3. p_segname in varchar2,
  4. p_owner in varchar2 default user,
  5. p_type in varchar2 default 'TABLE',
  6. p_partition in varchar2 default NULL
  7. )
  8. -- -this procedure use authid current user so it can query DBA_*
  9. -- -views using priviliges from a role and so it can be installed
  10. -- -once per database,instead of once user that wants to use it authid current_user
  11. AUTHID CURRENT_USER
  12. as
  13.    l_free_blks number;
  14.    l_total_blocks number;
  15.    l_total_bytes number;
  16.    l_unused_blocks number;
  17.    l_unused_bytes number;
  18.    l_LastUsedExtFileID number;
  19.    l_LastUsedExtBlockID number;
  20.    l_LAST_USED_BLOCK number;
  21.    l_segment_space_mgmt varchar2(255);
  22.    l_unformatted_blocks number;
  23.    l_unformatted_bytes number;
  24.    l_fs1_blocks number; l_fs1_bytes number;
  25.    l_fs2_blocks      number; l_fs2_bytes number;
  26.    l_fs3_blocks number; l_fs3_bytes number;
  27.    l_fs4_blocks number; l_fs4_bytes number;
  28.    l_full_blocks number; l_full_bytes number;

  29.    --inline procedure to print out numbers nicely formatted
  30.    --with a smple label
  31.    procedure p(p_label in varchar2,p_num in number)
  32.    is
  33.    begin
  34.         dbms_output.put_line(rpad(p_label,40,'.')||to_char(p_num,'999,999,999,999'));
  35.    end;
  36. begin
  37. -- this query is executed dynamically in order to allow this procedure
  38. -- to be create by a user who has access to DBA_SEGMENTS/TABLESPACES
  39. -- via a role as is customary
  40. -- note:at runtime,the invoker must have access to these two views
  41. -- this query dertermines if the object is an assm object or not
  42.     begin
  43.     execute immediate
  44.         'select ts.segment_space_management
  45.         from dba_segments seg,dba_tablespace ts
  46.         where seg.segment_name = :p_segname
  47.         and (:p_partition is null or seg.partition_name = :p_partition)
  48.         and seg.owner = :p_owner
  49.         and seg.tablespace_name=ts.tablespace_name'
  50.         into l_segment_space_mgmt
  51.         using p_segname,p_partition,p_partition,p_owner;
  52.     exception
  53.      when too_many_rows then
  54.      dbms_output.put_line
  55.      ('This must be a partitioned table,use p_partition =>');
  56.      return;
  57. end;
  58. --if the object is in an an assm tablespace ,we must use this API
  59. --call to get space infomation,else we use the free_blocks
  60. --API for the user management segments
  61. if l_segment_space_mgmt = 'AUTO' then
  62.     dbms_space.space_usage
  63.     (p_owner,p_segname,p_type,
  64.     l_unformatted_blocks,l_unformatted_bytes,
  65.     l_fs1_blocks,l_fs1_bytes,
  66.     l_fs2_blocks,l_fs2_bytes,
  67.     l_fs3_blocks,l_fs3_bytes,
  68.     l_fs4_blocks,l_fs4_bytes,
  69.     l_full_blocks,l_full_bytes,p_partition);
  70.     p('Unformatted Blocks',l_unformatted_blocks);
  71.     p('FS1 BLOCKS(0-25) ',l_fs1_blocks);
  72.     p('FS2 BLOCKS(25-50) ',l_fs2_blocks);
  73.     p('FS3 BLOCKS(50-75) ',l_fs3_blocks);
  74.     p('FS4 BLOCKS(75-100)',l_fs4_blocks);
  75. else
  76.     dbms_space.free_blocks(
  77.     segment_owner         => p_owner,
  78.     segment_name         => p_segname,
  79.     segment_type         => p_type,
  80.     freelist_group_id => 0,
  81.     free_blks         => l_free_blks
  82.     );
  83.     p('Free Blocks ',l_free_blks);
  84. end if;
  85. --and then the unused space api call to get the rest of the information
  86. dbms_space.unused_space
  87. (
  88.      segment_owner          => p_owner,
  89.     segment_name          => p_segname,
  90.     segment_type          => p_type,
  91.     partition_name          => p_partition,
  92.     total_blocks          => l_total_blocks,
  93.     total_bytes          => l_total_bytes,
  94.     unused_blocks          => l_unused_blocks,
  95.     unused_bytes          => l_unused_bytes,
  96.     LAST_USED_EXTENT_FILE_ID => l_LastUsedExtFileID,
  97.     LAST_USED_EXTENT_BLOCK_ID => l_LastUsedExtBlockID,
  98.     LAST_USED_BLOCK      => l_LAST_USED_BLOCK
  99.     );

  100.     p('Total      Blocks',l_total_blocks);
  101.     p('Total Bytes',l_total_bytes);
  102.     p('Total MBytes',trunc(l_total_bytes/1024/1024));
  103.     p('Unused Blocks',l_unused_blocks);
  104.     p('Unused Bytes',l_unused_bytes);
  105.     p('Last Used Ext FileID',l_LastUsedExtFileID);
  106.     p('Last used EXt Block ID ',l_LastUsedExtBlockID);
  107.     p('Last used Block',l_LAST_USED_BLOCK);
  108. end;
  109. /


以及用户测试表空间脚本:big_table.sql


点击(此处)折叠或打开

  1. truncate table hugetable;
  2. drop table hugetable;
  3. commit;
  4. create table hugetable
  5.   as
  6.   select rownum id,a.*
  7.   from all_objects a
  8.   where 1=0
  9.   /
  10.   alter table hugetable nologging;
  11. declare
  12.   l_cnt number;
  13.   l_rows number :=&1;
  14.   begin
  15.   insert /*+ append */
  16.     into hugetable
  17.     select rownum,a.*
  18.     from all_objects a
  19.     where rownum <= l_rows ;
  20.     l_cnt := sql%rowcount;
  21.     commit;
  22.     while(l_cnt < l_rows)
  23.     loop
  24.       insert /*+ append */ into hugetable
  25.         select rownum+l_cnt,
  26.         owner,object_name,subobject_name,object_id,data_object_id,
  27.         object_type,created,last_ddl_time,timestamp,status,
  28.         temporary,generated,secondary,namespace,edition_name
  29.         from hugetable
  30.         where rownum <= l_rows - l_cnt;
  31.         l_cnt := l_cnt+sql%rowcount;
  32.         commit;
  33.     end loop;
  34.   end;
  35.   /
  36.   alter table hugetable add constraint
  37.   hugetable_pk primary key(id);
  38.   prompt 'end of procedure '
  39.   exec dbms_stats.gather_table_stats(user,'hugetable',estimate_percent => 1);
  40. select count(*) from hugetable ;


然后,就是你需要配置你得sqlplus,格式化:


点击(此处)折叠或打开

  1. set serveroutput on size unlimited
  2. set trimpool on
  3. set long 5000
  4. set linesize 100
  5. set pagesize 9999999
  6. set plan_plus_exp format a80
  7. set termout off
  8. define gname=idle
  9. column global_name new_Value gname
  10. select lower(user)||'@'||substr(global_name,1,decode(dot,0,length(global_name),dot-1)) global_name
  11. from (select global_name,instr(global_name,'.') dot from global_name);
  12. set sqlprompt '&gname'
  13. set termout on


pagesize: 多少N打印一次列标题

trimpool:对spool不进行trim,使其长度为linesize

linesize: 行宽

serveroutput:启用DBMS_OUTPUT输出

column plan_plus_exp format a80:设置explain的输出为80

long: clob,lob的默认大小为5000

sqlprompt:我是谁,在哪里, 一旦你conn,还是需要你show user知道你是谁.

trace的使用:


点击(此处)折叠或打开

  1. H:\app\kinfinger\product\11.2.0\dbhome_1\RDBMS\ADMIN
  2. @utlxplan
  3. grant all on plan_table to public
  4. set autotrace on
  5. set autotrace on explain
  6. set autotrace on statistics
  7. set autotrace only
  8. set autotrace off
  9. 查看plan_table:
  10. set autotrace traceonly
  11. select * from hugetable;
  12. 执行计划
  13. ----------------------------------------------------------
  14. Plan hash value: 4224224188

  15. -------------------------------------------------------------------------------
  16. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  17. -------------------------------------------------------------------------------
  18. | 0 | SELECT STATEMENT | | 9975 | 886K| 38 (0)| 00:00:01 |
  19. | 1 | TABLE ACCESS FULL| HUGETABLE | 9975 | 886K| 38 (0)| 00:00:01 |
  20.  select * from hugetable where id=5;
  21. 执行计划
  22. ----------------------------------------------------------
  23. Plan hash value: 232715781

  24. --------------------------------------------------------------------------------
  25. ------------
  26. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)
  27. | Time |
  28. --------------------------------------------------------------------------------
  29. ------------
  30. | 0 | SELECT STATEMENT | | 1 | 91 | 2 (0)
  31. | 00:00:01 |
  32. | 1 | TABLE
阅读(2572) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~