Oracle 常用SQL

查询回收站内容,大于20则需要清理

SELECT count(1) FROM dba_recyclebin;

 

清理回收站所有内容

purge dba_recyclebin;

 

查询所有表空间

SELECT 'RESULT='||df.tablespace_name "Tablespace",df.bytes/(1024*1024) "Total Size(MB)",
    sum(fs.bytes)/(1024*1024) "Free Size(MB)", round(sum(fs.bytes)*100/df.bytes) "% Free",
    round((df.bytes-sum(fs.bytes))*100/df.bytes) "% Used"
    FROM (SELECT a.bytes,a.tablespace_name FROM dba_free_space a  UNION ALL SELECT b.BYTES_FREE,b.tablespace_name FROM V$TEMP_SPACE_HEADER b )fs,
    (SELECT tablespace_name, sum(bytes) bytes FROM dba_data_files GROUP BY tablespace_name UNION ALL  SELECT tablespace_name, SUM(bytes) total FROM dba_temp_files GROUP BY tablespace_name) df          
    WHERE fs.tablespace_name = df.tablespace_name
GROUP BY df.tablespace_name, df.bytes;

 

查询某个表空间被谁占用

SELECT * FROM
(SELECT owner,segment_name, sum(bytes)/1024/1024 Mbytese FROM dba_segments  WHERE  tablespace_name IN('CBS_DEFAULT_DAT')
GROUP BY owner,segment_name  ORDER BY Mbytese DESC )
WHERE rownum<=10;

 

统计信息定时任务是否正常,结果数大于 0则表明正常

SELECT count(1)  FROM dba_autotask_job_history a   WHERE  a.client_name='auto optimizer stats collection' AND  job_status='SUCCEEDED' AND   a.window_start_time >=sysdate-2;

 

检查SQL是否绑定变量

SELECT substr(sql_text, 1, 40) "SQL", count(*),
       sum(executions) "TotExecs"
  FROM v$sqlarea
 WHERE executions < 5
 GROUP BY substr(sql_text, 1, 40)
HAVING count(*) > 30
 ORDER BY 2 DESC;

 

SQL返回查询时间超过5秒的SQL

SELECT p.spid, s.sid, s.last_call_et,
       s.machine, s.program, s.terminal,
       w.event,q.sql_text
  FROM v$process p,
       v$session s,
       v$sqltext q,
       v$session_wait w
 WHERE p.addr = s.paddr
   AND s.sql_address = q.address
   AND s.STATUS = 'ACTIVE'
   AND p.background IS NULL
   AND s.sid = w.sid
   AND s.last_call_et > 5
 ORDER BY s.last_call_et DESC, s.sid, q.piece;

 

检查分区表是否存在全局索引,如果查询返回纪录,则表明建立了全局索引,需要重新建立为本地索引或者全局分区索引

 SELECT a.owner, a.table_name, a.index_name, a.partitioned, a.STATUS
  FROM dba_indexes a, dba_part_tables b
 WHERE a.table_name = b.table_name
   AND a.owner NOT IN ('SYS', 'SYSTEM')
   AND a.partitioned <> 'YES';

 

检查业务对象并行度设置

   SELECT owner, object_name, degree
  FROM (SELECT a.owner, a.table_name object_name, a.degree
          FROM dba_tables a
         WHERE trim(a.degree) > '1'
        UNION ALL
        SELECT a.owner, a.index_name object_name, a.degree
          FROM dba_indexes a
         WHERE trim(a.degree) > '1'
       ) b
 WHERE b.owner NOT IN ('SYS', 'SYSTEM', 'SYSMAN', 'MGMT_VIEW')
   AND b.owner NOT IN (SELECT c.username FROM dba_users_with_defpwd c);

 

 设置索引并行度为1

ALTER INDEX IDX_NAME noparallel;

 

设置表并行度为1

ALTER TABLE TABLE_NAME noparallel;

 

检查非正常的job

SELECT JOB,SCHEMA_USER,WHAT FROM  dba_jobs  WHERE  broken='Y';

 

修复job:检查Oracle告警日志,分析job失败的原因。找出对应的job号后(20),用以下命令将其的broken改为N

exec dbms_job.broken(20,false);

 

检查是否存在失效对象

SELECT t.owner, t.object_type,
       t.object_name, t.STATUS
  FROM dba_objects t
 WHERE t.STATUS = 'INVALID'
--如果没用,建议删除。
--如果有用,则需要重新编译。
--重编译对象
ALTER object_type object_owner.object_name compile;
--重编译用户
execute dbms_utility.compile_schema(schema => '',compile_all => false);
--重编译数据库
@?/rdbms/admin/utlrp.sql

 

在线创建索引

CREATE INDEX index_name ON table_name(col_name)  tablespace tablespace_name online;

 

并行创建索引(如果分在线创建,请将online参数去掉)

CREATE INDEX index_name ON table_name(col_name)  parallel 4 tablespace tablespace_name online;
ALTER INDEX index_name noparallel;

 

在线重建索引

expdp sysdb/sys_db1 dumpfile=sysdb_expdb20120415.dmp directory=dpump_dir logfile=sysdb.log EXCLUDE=TABLE:\"IN\(\'MDSP_CDR\',\'T_BME_TASKRUNRESULT\',\'TB_INTERACTLOG\'\)\"

 

 

导出数据排除某些表

expdp sysdb/sys_db1 dumpfile=sysdb_expdb20120415.dmp directory=dpump_dir logfile=sysdb.log EXCLUDE=TABLE:\"IN\(\'MDSP_CDR\',\'T_BME_TASKRUNRESULT\',\'TB_INTERACTLOG\'\)\";

 

单表导入导出

expdp sysdb/sysdb TABLES=TB1 directory= dpump_dir dumpfile=sysdb_tb_mobileno.dmp
impdp sysdb/sysdb t directory=dpump_dir dumpfile= dumpfile=sysdb_tb_mobileno.dmp  TABLES= TB1;

 

在不同用户恢复表数据

expdp sysdb/sys_db1 directory=dmpdir content=ALL DUMPFILE=Tcategory_Relation.dmp TABLES=Tcategory_Relation
impdp sysdb5/sys_db1 DIRECTORY=dmpdir DUMPFILE=Tcategory_Relation.dmp  TABLES=Tcategory_Relation  remap_schema=sysdb:sysdb5;

 

对表转移表空间,需要停业务操作,表重建后索引也要重建

ALTER TABLE table_name move tablespace <new tablespace>;
ALTER INDEX index_name  rebuild online;

 

对索引转移表空间

ALTER INDEX PK_MDSP_T_RENTINFO_2 rebuild online tablespace TS_MDSP_IDX;

 

降低高水位

ALTER TABLE table_name enable row movement;
ALTER TABLE table_name shrink space compact;
ALTER TABLE table_name shrink space cascade;

 

收集表的统计信息

exec  dbms_stats.gather_table_stats(ownname=>'大写的数据库用户名',tabname=>'大写的表名',degree=>4,cascade=>true,force=>true,no_invalidate=>false);

 

收集用户的统计信息

exec  dbms_stats.gather_schema_stats(ownname=> '大写的数据库用户名',estimate_percent => dbms_stats.AUTO_SAMPLE_SIZE,degree=>4,cascade=>true,force=>true,no_invalidate=>false);

 

 

如何检查失效的索引

SELECT a.owner, a.index_name, a.STATUS
  FROM dba_indexes a
 WHERE a.STATUS = 'UNUSABLE'
UNION ALL
SELECT a.index_owner, a.index_name, a.STATUS
  FROM dba_ind_partitions a
 WHERE a.STATUS = 'UNUSABLE';

 

检查失效索引并显示重建语句

SELECT 'alter index  '||  a.owner||'.'|| a.index_name || ' rebuild online;' AS rebuild_index_sql
     FROM DBA_indexes a
    WHERE a.partitioned <> 'YES'
      AND ( a.STATUS = 'UNUSABLE')
UNION
SELECT 'alter index  '||  t.index_owner||'.'|| t.index_name || ' rebuild partition ' ||
          t.partition_name || ' online;' AS rebuild_index_sql
     FROM DBA_ind_partitions t,user_part_indexes n
    WHERE ( t.STATUS = 'UNUSABLE');

 

如何恢复删除的数据,如果数据被误删除,而且时间不是很长,可以从undo表空间将表数据恢复,请修改为现场的表明和时间

CREATE TABLE table_name_new AS  SELECT * FROM table_name AS of timestamp to_timestamp  ('2013-03-26 11:00:00','YYYY-MM-DD HH24:MI:SS');

 

创建snapshot

execute dbms_workload_repository.create_snapshot;

 

收集AWR报告

@?/rdbms/admin/awrrpt.sql

 

收集ASH报告

@?/rdbms/admin/ashrpt.sql

 

查询session历史信息

 

SELECT *
FROM dba_hist_active_sess_history  t
WHERE  t.sample_time > to_date('20130811 070000', 'yyyymmdd hh24miss')
AND t.sample_time < to_date('20130811 090000', 'yyyymmdd hh24miss');

 

检查事务锁 

SELECT 'session ' || c.locker ||
       ' lock session ' || c.locked
       AS "result"
  FROM ( SELECT a.sid locked,
                b.sid locker
           FROM v$lock a, v$lock b
          WHERE a.request > 0
            AND a.id1 = b.id1
            AND a.id2 = b.ID2
            AND a.type = b.type
            AND a.addr <> b.addr
        ) c,
        v$session d
 WHERE c.locker = d.sid;

 

检查锁并显示杀锁语句

SELECT DISTINCT 'kill -9 ' ||
       p.spid || ';'  AS "result"
  FROM ( SELECT a.sid locked,
                b.sid locker
           FROM v$lock a, v$lock b
          WHERE a.request > 0
            AND a.id1 = b.id1
            AND a.id2 = b.ID2
            AND a.type = b.type
            AND a.addr <> b.addr
        ) c,
        v$session d,
        v$process p
 WHERE c.locker = d.sid AND  d.paddr=p.addr;

 

清理归档日志操作

--方法一
  rman target /
  crosscheck backup;
   DELETE obsolete;
   DELETE expired backup;
   crosscheck archivelog ALL; 
   DELETE expired archivelog ALL;
  
--方法2
  rman target /
allocate channel FOR maintenance type disk; 
DELETE obsolete device type disk;

 

如何获取SQL的执行计划,从AWR报告中找到想要获取的sqlSQL ID,然后用以下sql获取:

SELECT * FROM TABLE(dbms_xplan.display_cursor('atfwcg8anrykp'));

  •  
    锁表查询的代码有以下的形式:
    select count(*) from v$locked_object;
    select * from v$locked_object;
    查看哪个表被锁
    select b.owner,b.object_name,a.session_id,a.locked_mode
    from v$locked_object a,dba_objects b
    where b.object_id = a.object_id;
    查看是哪个session引起的
    select b.username,b.sid,b.serial#,logon_time
    from v$locked_object a,v$session b
    where a.session_id = b.sid order by b.logon_time;
    杀掉对应进程,执行命令:
    alter system kill session'1025,41';
    其中1025为sid,41为serial#.
     

 

posted @ 2017-05-14 15:43  当年亦如是  阅读(227)  评论(0编辑  收藏  举报