oracleif判断语句

发布网友 发布时间:2022-04-22 02:35

我来回答

2个回答

懂视网 时间:2022-04-08 02:55

1.数据池命中率 --db_pool命中率(要求:95%左右): select 100 - 100 * ((select value from v$sysstat where name = ‘physical reads‘)) / ((select value from v$sysstat where name = ‘consistent gets‘) + (select value from v$sysstat where name = ‘db block gets‘)) from dual; select * from V$PARAMETER where name in (‘sga_max_size‘, ‘db_cache_size‘, ‘shared_pool_size‘, ‘pga_aggregate_target‘); --2.查找前十条性能差的sql(磁盘读取较大,缺少索引或语句不合理) SELECT * FROM (SELECT PARSING_USER_ID EXECUTIONS, SORTS, COMMAND_TYPE, DISK_READS, sql_text FROM v$sqlarea ORDER BY disk_reads DESC) WHERE ROWNUM < 10; SELECT sql_text, hash_value, executions, buffer_gets, disk_reads, parse_calls FROM V$SQLAREA WHERE buffer_gets > 10000000 OR disk_reads > 1000000 ORDER BY buffer_gets + 100 * disk_reads DESC; --3.根据unix上Top命令看到的PID,查找对应的SQl SELECT P.pid pid, S.sid sid, P.spid spid, S.username username, S.osuser osname, P.serial# S_#, P.terminal, P.program program, P.background, S.status, RTRIM(SUBSTR(a.sql_text, 1, 80)) SQL FROM v$process P, v$session S, v$sqlarea A WHERE P.addr = s.paddr AND S.sql_address = a.address(+) AND P.spid LIKE ‘%CPU最高的进程对应的PID%‘; --4.察看IO情况: select df.name 文件名, fs.phyrds 读次数, fs.phywrts 写次数, (fs.readtim/decode(fs.phyrds,0,-1,fs.phyrds)) 读时间, (fs.writetim/decode(fs.phywrts,0,-1,fs.phywrts)) 写时间 from v$datafile df, v$filestat fs where df.file#=fs.file# order by df.name; --5.表空间察看 SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)", TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100, 2), ‘990.99‘) "使用比", F.TOTAL_BYTES "空闲空间(M)", F.MAX_BYTES "最大块(M)" FROM (SELECT TABLESPACE_NAME, ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES, ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES FROM SYS.DBA_FREE_SPACE GROUP BY TABLESPACE_NAME) F, (SELECT DD.TABLESPACE_NAME,ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB FROM SYS.DBA_DATA_FILES DD GROUP BY DD.TABLESPACE_NAME) D WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME ORDER BY 4 DESC; -- 数据表总行数排序 select t.table_name, t.num_rows, t.* from user_tables t where t.num_rows > 0 order by t.num_rows desc -- 普通表数据大小排序 select segment_name, trunc(bytes/1024/1024) from user_segments where segment_type = ‘TABLE‘ order by bytes desc; -- 含LOB类型表的数据大小 SELECT A.TABLE_NAME, A.COLUMN_NAME, trunc(B.BYTES / 1024 / 1024), B.SEGMENT_NAME, B.SEGMENT_TYPE, B.TABLESPACE_NAME, B.BLOCKS, B.EXTENTS FROM USER_LOBS A, USER_SEGMENTS B WHERE A.SEGMENT_NAME = B.SEGMENT_NAME ORDER BY B.BYTES DESC; -- 数据表总的数据大小占用 select rowSize + nvl(lobSize, 0) dataSize, t1.*, t2.* from ( select segment_name, trunc(bytes/1024/1024) rowSize from user_segments where segment_type = ‘TABLE‘ ) t1 left join ( SELECT A.TABLE_NAME, A.COLUMN_NAME, trunc(B.BYTES / 1024 / 1024) lobSize, B.SEGMENT_NAME, B.SEGMENT_TYPE, B.TABLESPACE_NAME, B.BLOCKS, B.EXTENTS FROM USER_LOBS A, USER_SEGMENTS B WHERE A.SEGMENT_NAME = B.SEGMENT_NAME ) t2 on t1.segment_name = t2.table_name order by rowSize + nvl(lobSize, 0) desc --6.运行时间很长的SQL select username, sid, opname, round(sofar * 100 / totalwork, 0) || ‘%‘ as progress, time_remaining, sql_text from v$session_longops, v$sql where time_remaining <> 0 and sql_address = address and sql_hash_value = hash_value; --7.锁相关的查询 select ‘kill -9 ‘||PS.SPID, ‘alter system kill session ‘‘‘||vs.sid||‘,‘||vs.serial#||‘‘‘;‘, /* DECODE(V$LOCK.TYPE, ‘TM‘, ‘TABLE LOCK‘, ‘TX‘, ‘ROW LOCK‘, NULL) LOCK_LEVEL,*/ Decode(VL.LOCKED_MODE,0,‘[0] none‘, 1,‘[1] null 空‘, 2,‘[2] Row-S 行共用(RS):共用表鎖,sub share ‘, 3,‘[3] Row-X 行獨佔(RX):用於行的修改,sub exclusive ‘, 4,‘[4] Share 共用鎖(S):阻止其他DML操作,share‘, 5,‘[5] S/Row-X 共用行獨佔(SRX):阻止其他事務操作,share/sub exclusive ‘, 6,‘[6] exclusive 獨佔(X):獨立訪問使用,exclusive ‘, ‘[‘||VL.LOCKED_MODE||‘] Other Lock‘) LockMode, PS.SPID,OS_USER_NAME,VS.PROGRAM,VS.MACHINE,ORACLE_USERNAME,OBJECT_NAME,vs.LOGON_TIME , Vs.status,vs.MODULE from V$LOCKED_OBJECT VL ,DBA_OBJECTS OB ,V$SESSION VS ,v$process PS WHERE VL.OBJECT_ID = OB.OBJECT_ID AND VL.SESSION_ID = VS.SID AND Ps.ADDR = Vs.PADDR select ‘kill -9 ‘||PS.SPID, ‘alter system kill session ‘‘‘||vs.sid||‘,‘||vs.serial#||‘‘‘;‘, /* DECODE(V$LOCK.TYPE, ‘TM‘, ‘TABLE LOCK‘, ‘TX‘, ‘ROW LOCK‘, NULL) LOCK_LEVEL,*/ Decode(VL.LOCKED_MODE,0,‘[0] none‘, 1,‘[1] null 空‘, 2,‘[2] Row-S 行共用(RS):共用表鎖,sub share ‘, 3,‘[3] Row-X 行獨佔(RX):用於行的修改,sub exclusive ‘, 4,‘[4] Share 共用鎖(S):阻止其他DML操作,share‘, 5,‘[5] S/Row-X 共用行獨佔(SRX):阻止其他事務操作,share/sub exclusive ‘, 6,‘[6] exclusive 獨佔(X):獨立訪問使用,exclusive ‘, ‘[‘||VL.LOCKED_MODE||‘] Other Lock‘) LockMode, PS.SPID,OS_USER_NAME,VS.PROGRAM,VS.MACHINE,ORACLE_USERNAME,OBJECT_NAME,vs.LOGON_TIME , Vs.status,vs.MODULE, vs.SQL_ID, st.SQL_TEXT from V$LOCKED_OBJECT VL ,DBA_OBJECTS OB ,V$SESSION VS ,v$process PS, v$sqlarea st WHERE VL.OBJECT_ID = OB.OBJECT_ID AND VL.SESSION_ID = VS.SID AND Ps.ADDR = Vs.PADDR and vs.SQL_ID = st.SQL_ID AND VS.USERNAME = ‘LC019999‘ select st.SQL_FULLTEXT, vs.* from v$session vs join v$sql st on vs.SQL_ID = st.SQL_ID where vs.USERNAME = ‘scott‘ and st.SQL_TEXT like ‘%%‘ --8.产生kill会话的Sql语句 select A.SID, B.SPID, A.SERIAL#, a.lockwait, A.USERNAME, A.OSUSER, a.logon_time, a.last_call_et / 3600 LAST_HOUR, A.STATUS, ‘orakill ‘ || sid || ‘ ‘ || spid HOST_COMMAND, ‘alter system kill session ‘‘‘ || A.sid || ‘,‘ || A.SERIAL# || ‘‘‘‘ SQL_COMMAND from v$session A, V$PROCESS B where A.PADDR = B.ADDR AND SID > 6; -- 最近10分钟最消耗CPU的SQL语句: select sql_text from ( select sql_id,count(*) as cn from v$active_session_history where sample_time > sysdate - 10/24/60 and session_type <> ‘BACKGROUND‘ and SESSION_STATE = ‘ON CPU‘ group by sql_id order by cn desc ) ash, v$sql s where ash.sql_id=s.sql_id; -- 最近10分钟最消耗IO的SQL语句: select sql_text from ( select sql_id,count(*) as cn from v$active_session_history where sample_time > sysdate - 10/24/60 and session_type <> ‘BACKGROUND‘ and WAIT_CLASS=‘User I/O‘ group by sql_id order by cn desc ) ash, v$sql s where ash.sql_id=s.sql_id;

  --清空缓存,Command window执行:
  Alter system flush shared_pool;


  --analyze table 更新统计信息:
  analyze table my_table compute statistics;


  --删除数据后,释放数据空间
  alter table my_table enable row movement;
  alter table my_table shrink space cascade;
  alter table my_table disable row movement;


获取指定SQL在内存中的执行计划:
select sql_id,child_number,sql_text from v$sql where sql_text like ‘select count(1) from emp a where a.dept_no =%‘;
select * from table(DBMS_XPLAN.DISPLAY_CURSOR(‘sql_id‘,0)); 

1、PL/SQL Developer 中使用F5
2、explain plan for select count(1) from emp a where a.dept_no=5;
 select * from table(dbms_xplan.display());
3、sqlplus中使用 set autotrace traceonly exp;

Oracle常用的诊断语句

标签:

热心网友 时间:2022-04-08 00:03

oracle的if语句采用decode函数。

DECODE(value,if1,then1,if2,then2,if3,then3,...,else)

表示如果value 等于if1时,DECODE函数的结果返回then1,...,如果不等于任何一个if值,则返回else。

Oracle数据库是对标准sql语言的过程化扩展,因此产生了pl/sql语言。其中的if语句大量使用使得程序模块化的功能方便实用。现在要讨论的是if语句的基本使用方法。

连接数据库

请输入用户名:  scott/123456

设置环境变量

SQL> set serveroutput on 

定义两个字符串变量,然后赋值,接着使用if……then语句比较两个字符串变量的长度,并输出比较结果。

declare

a varchar(10);

b varchar(10);

begin

a:='beijing';

b:='guangdong';

if length(a)>length(b)

then dbms_output.put_line('a>b');

end if;

end;

过if……then……else语句实现只有年龄大于等于56岁,才可以申请退休,否则程序会提示不可以申请退休。

declare

a number(10);

begin

a:=&x;

if a>=56

then dbms_output.put_line('可以申请退休');

else dbms_output.put_line('不可以申请退休');

end if;

end;

制定一个月份数值,然后使用if……then……elsif语句判断它所属的季节,并输出季节信息。

declare

mon number(10);

begin

mon:=&x;

if mon=3 or mon=4 or mon=5

then dbms_output.put_line('春节');

elsif mon=6 or mon=7 or mon=8 then dbms_output.put_line('夏季');

elsif mon=9 or mon=10 or mon=11 then dbms_output.put_line('秋季');

elsif mon=12 or mon=1 or mon=2 then dbms_output.put_line('冬季');

end if;

end;

制定一个季度数值,然后使用case语句判断它所包含的月份信息并输出。

declare

ss number(10);

begin

ss:=&x;

case

when ss=1 then dbms_output.put_line('包含月份3,4,5');

when ss=2 then dbms_output.put_line('包含月份6,7,8');

when ss=3 then dbms_output.put_line('包含月份9,10,11');

when ss=4 then dbms_output.put_line('包含月份12,1,2');

end case;

end;

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com