欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle DBA常用语句第1/2页

程序员文章站 2023-11-17 10:51:04
查看表空间的名称及大小复制代码 代码如下: sql>select t.tablespace_name, round(sum(bytes/(1024*1024)),0)...
查看表空间的名称及大小
复制代码 代码如下:

sql>select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size
from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name;

查看表空间物理文件的名称及大小:
复制代码 代码如下:

sql>select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;

查看回滚段名称及大小:
复制代码 代码如下:

sql>select segment_name, tablespace_name, r.status,
(initial_extent/1024) initialextent,(next_extent/1024) nextextent,
max_extents, v.curext curextent from dba_rollback_segs r, v$rollstat v
where r.segment_id = v.usn(+) order by segment_name;

如何查看某个回滚段里面,跑的什么事物或者正在执行什么sql语句:
复制代码 代码如下:

sql>select d.sql_text,a.name
from v$rollname a,v$transaction b,v$session c,v$sqltext d
where a.usn=b.xidusn and b.addr=c.taddr and c.sql_address=
d.address and c.sql_hash_value=d.hash_value
and a.usn=1;

(备注:你要看哪个,就把usn=?写成几就行了)
查看控制文件:
sql>select * from v$controlfile;
查看日志文件:
sql> col member format a50
sql>select * from v$logfile;
如何查看当前sql*plus用户的sid和serial#:
sql>select sid, serial#, status from v$session where audsid=userenv('sessionid');
如何查看当前数据库的字符集:
sql>select userenv('language') from dual;
sql>select userenv('lang') from dual;
怎么判断当前正在使用何种sql优化方式:
用explain plan产生explain plan,检查plan_table中id=0的position列的值。
sql>select decode(nvl(position,-1),-1,'rbo',1,'cbo') from plan_table where id=0;
如何查看系统当前最新的scn号:
sql>select max(ktuxescnw * power(2,32) + ktuxescnb) from x$ktuxe;
在oracle中查找trace文件的脚本:
sql>select u_dump.value || '/' || instance.value || '_ora_' ||
v$process.spid || nvl2(v$process.traceid, '_' || v$process.traceid, null ) || '.trc'"trace file" from v$parameter u_dump cross join v$parameter instance cross join v$process join v$session on v$process.addr = v$session.paddr where u_dump.name = 'user_dump_dest' and
instance.name = 'instance_name' and v$session.audsid=sys_context('userenv','sessionid');
sql>select d.value || '/ora_' || p.spid || '.trc' trace_file_name
from (select p.spid from sys.v_$mystat m,sys.v_$session s,
sys.v_$process p where m.statistic# = 1 and
s.sid = m.sid and p.addr = s.paddr) p,(select value from sys.v_$parameter where name ='user_dump_dest') d;
如何查看客户端登陆的ip地址:
sql>select sys_context('userenv','ip_address') from dual;
如何在生产数据库中创建一个追踪客户端ip地址的触发器:
复制代码 代码如下:

sql>create or replace trigger on_logon_trigger
after logon on database
begin
dbms_application_info.set_client_info(sys_context('userenv', 'ip_address'));
end;
rem 记录登陆信息的触发器
create or replace trigger logon_history
after logon on database --when (user='wacos') --only for user 'wacos'
begin
insert into session_history
select username,sid,serial#,audsid,osuser,action,sysdate,null,sys_context('userenv','ip_address'),terminal,machine,program
from v$session where audsid = userenv('sessionid');
end;

查询当前日期:
sql> select to_char(sysdate,'yyyy-mm-dd,hh24:mi:ss') from dual;
查看所有表空间对应的数据文件名:
sql>select distinct file_name,tablespace_name,autoextensible from dba_data_files;
查看表空间的使用情况:
复制代码 代码如下:

sql>select sum(bytes)/(1024*1024) as free_space,tablespace_name
from dba_free_space group by tablespace_name;
sql>select a.tablespace_name,a.bytes total,b.bytes used, c.bytes free,
(b.bytes*100)/a.bytes "% used",(c.bytes*100)/a.bytes "% free"
from sys.sm$ts_avail a,sys.sm$ts_used b,sys.sm$ts_free c
where a.tablespace_name=b.tablespace_name and a.tablespace_name=c.tablespace_name;
column tablespace_name format a18;
column sum_m format a12;
column used_m format a12;
column free_m format a12;
column pto_m format 9.99;
select s.tablespace_name,ceil(sum(s.bytes/1024/1024))||'m' sum_m,ceil(sum(s.usedspace/1024/1024))||'m' used_m,ceil(sum(s.freespace/1024/1024))||'m' free_m, sum(s.usedspace)/sum(s.bytes) ptused
from (select b.file_id,b.tablespace_name,b.bytes,
(b.bytes-sum(nvl(a.bytes,0))) usedspace,
sum(nvl(a.bytes,0)) freespace,(sum(nvl(a.bytes,0))/(b.bytes)) * 100 freepercentratio from sys.dba_free_space a,sys.dba_data_files b
where a.file_id(+)=b.file_id group by b.file_id,b.tablespace_name,b.bytes
order by b.tablespace_name) s group by s.tablespace_name
order by sum(s.freespace)/sum(s.bytes) desc;

查看数据文件的hwm(可以resize的最小空间)和文件头大小:
复制代码 代码如下:

select v1.file_name,v1.file_id,
num1 totle_space,
num3 free_space,
num1-num3 "used_space(hwm)",
nvl(num2,0) data_space,
num1-num3-nvl(num2,0) file_head
from
(select file_name,file_id,sum(bytes) num1 from dba_data_files group by file_name,file_id) v1,
(select file_id,sum(bytes) num2 from dba_extents group by file_id) v2,
(select file_id,sum(bytes) num3 from dba_free_space group by file_id) v3
where v1.file_id=v2.file_id(+)
and v1.file_id=v3.file_id(+);

1