oracle運維-常用SQL

################################################V$active_session_history################################################
set line 1000
col session_id for 99999999
col session_serial# for 9999999
col program for a30
col sql_id for a30
col event for a30
col seq# for 999999
col count(*) for 99999
select session_id,session_serial#,program,sql_id,event,seq#,count(*) 
from v$active_session_history
where sample_time between to_date('2018-12-27 08:00:00','yyyy-mm-dd hh24:mi:ss') 
and to_date('2018-12-28 09:00:00','yyyy-mm-dd hh24:mi:ss')
and sql_id is not null
group by session_id,session_serial#,program,sql_id,event,seq#
having count(*) >1;



set line 100
col sample_time for a30
col sample_id for 9999999
col event for a30
col seq# for 9999999999
col sql_id for a30
select sample_time,sample_id,event,seq#,sql_id
from v$active_session_history
where session_id=&session_id
and session_serial#=&session_serial#
and event='&event'
and seq#=&seq#;



set line 200
col session_id for 999999999999
col session_serial for 9999999999
col xid for 9999999999
col program for a30
col event for a30
col blocking_session for 99999999
select session_id,session_serial,xid,program,event,blocking_session
from v$active_session_history
where between to_date('2018-12-27 08:00:00','yyyy-mm-dd hh24:mi:ss') 
and to_date('2018-12-28 09:00:00','yyyy-mm-dd hh24:mi:ss')
and sql_id is not null
and xid is not null


set line 200
col session_id for 999999
col session_serial# for 9999999
col seq# for 99999999
col event for a30
col count(*) for 999999
select inst_num, session_id,session_serial#,seq#,event,
count(*)
from dba_hist_active_sess_history
where sample_time between to_date('2018-12-28 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date('2018-12-29 00:00:00','yyyy-mm-dd hh24:mi:ss')
group by inst_num, session_id,session_serial#,seq#,event
order by count(*);


set line 100
col sample_id for 999999
col inst_num for 99
col blocking_session for 999999
select sample_id,inst_num,blocking_session 
from dba_hist_active_sess_history
where sample_time between to_date('2018-12-28 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date('2018-12-29 00:00:00','yyyy-mm-dd hh24:mi:ss')
and event='&event'
and inst_num=&inst_num
and session_id=&session_id
and session_serial=&session_serial
and seq#=&seq#


set line 100
col program for a30
col inst_num for 99
col sql_id for a30
select program,inst_num,event,sql_id
from dba_hist_active_sess_history
where sample_id=&sample_id
and inst_num=&inst_num
and session_id=&session_id
and session_serial#=&session_serial#




-- in_connection_mgmt
-- in_parse
-- in_hard_parse
-- in_sql_execution
-- in_plsql_execution
-- in_plsql_rpc
-- in_plsql_compliation
-- in_java_execution
-- in_bind
-- in_cursor_close
-- in_sequence_load

select 
in_connection_mgmt,
in_parse,
in_hard_parse,
in_sql_execution,
in_plsql_execution,
in_plsql_rpc,
in_plsql_compliation,
in_java_execution,
in_bind,
in_cursor_close,
in_sequence_load
from dba_hist_active_sess_history
where inst_num=&inst_num
and SESSION_ID=&session_id
and session_serial#=&session_serial#


set line 100
col session_id for 99999999
col session_serial# for 9999999
col sql_id for a30
col sql_exec_id for 9999999
col count(*) for 99999999
select session_id,session_serial#,sql_id,sql_exec_id,count(*)
from dba_hist_active_sess_history
group by session_id,session_serial#,sql_id,sql_exec_id
order by count(*);


set line 100
col opname for a30
col plsql_entry_id for 999999
col p1 for a30
col p2 for a30
col p3 for a30
select opname,plsql_entry_id,p1,p2,p3
from dba_hist_active_sess_history
where instance_number=&instance_number
and session_id=&session_id
and session_serial#=&session_serial


set line 100
col program for a30
col inst_num for 99
col blocking_session for 99999999
col blocking_session_serial# for 99999999999
col blocking_inst_id for 9999
select program,inst_num,event,blocking_session,blocking_session_serial#,blocking_inst_id,sql_id
from dba_hist_active_sess_history
where inst_num=&inst_num
and session_id=&session_id
and session_serial#=&session_serial#


select type,parameter from v$rowcache where cache#=10





###############################V$session_longops,v$session#####查看進程剩餘耗時#####################################

set line 200
col sid for 999999999999
col SERIAL# for 99999999999
col PROGRAM for a30
col opname for a30
col time_remaining for 99999999999999

SELECT a.SID,a.SERIAL#,a.PROGRAM "程序進程",b.OPNAME "操作名稱",b.TIME_REMAINING "剩餘時間(秒)"
FROM v$session a,v$session_longops b
WHERE a.SID = b.SID
AND a.SERIAL# = b.SERIAL#
AND a.PROGRAM LIKE '%rman%'
AND time_remaining >0;


#############################oradebug#########################################################################################
1.session Tracing 
alter session set tracefile_identifier='10046'; 

alter session set timed_statistics = true;
alter session set statistics_level=all;
alter session set max_dump_file_size = unlimited;

alter session set events '10046 trace name context forever,level 12';

2.tracing process
select p.PID,p.SPID,s.SID
from v$process p,v$session s
where s.paddr = p.addr
and s.sid = &SESSION_ID
/

select 'ospid: ' || p.spid || ' # ''' ||s.sid||','||s.serial#||''' '||
  s.osuser || ' ' ||s.machine ||' '||s.username ||' '||s.program line
from v$session s , v$process p
where p.addr = s.paddr
and s.username <> ' ';



connect / as sysdba
oradebug setospid 9834
oradebug unlimit
oradebug event 10046 trace name context forever,level 12
oradebug tracefile_name
oradebug event 10046 trace name context off


connect / as sysdba
oradebug setorapid 9834
oradebug unlimit
oradebug event 10046 trace name context forever,level 12
oradebug tracefile_name
oradebug event 10046 trace name context off

-- 10053 trace



####################################################v$SQLAREA####################################################################

yyyy-mm-dd hh24:mi:ss
set line 200
col sql_id for a30
col last_active_time for a30
col first_load_time
col last_load_time
col executions
col buffer_gets
col user_io_wait_time
col cpu_time
col elapsed_time
col physical_read_requests
col physical_read_bytes


select 
    sql_id,
    last_active_time,
    first_load_time,
    last_load_time,
    executions,
    buffer_gets,
    user_io_wait_time,
    cpu_time,
    elapsed_time,
    physical_read_requests,
    physical_read_bytes
from v$SQLAREA
order by physical_read_bytes desc;


##############################################實驗:檢查兩個表中的數據及對應的數據的條數是否相同########################################

1.建立視圖
create or replace view v as
    select * from emp where deptno!=10
    union all
    select * from emp where ename='SCOTT'
2.要求查找視圖V和表emp中不同的數據
SELECT rownum,empno,ename
FROM    v
WHERE   ename='SCOTT';


SELECT rownum,empno,ename
FROM    emp
WHERE   ename='SCOTT';

3.比較數據集不同
select v.empno,v.ename ,b.empno,b.ename
from v
full join
emp b on(v.empno=b.empno)
where (v.empno is null or b.empno is null);

####################################################統計信息######################################
dba_tab_statistics
dba_tab_col_statistics

set line 100
-- var low_value number;
-- var high_value varchar2(100);
-- select DBMS_STATS.CONVERT_RAW_VALUE('yuiovbssas',:high_value) from  dual;
col owner for a10
col table_name for a20
col column_name for a30
col high_value for a30
col low_value for a30
col num_nulls for 9999
col histogram for a30
select owner,table_name,column_name,high_value,low_value,num_nulls,histogram
from dba_tab_col_statistics
where table_name=''
and column_name=''
and owner='';


set line 200
col owner for a20
col table_name for a20
col num_rows for 99999
col partition_name for a20
col LAST_ANALYZED for a30
col blocks for 99999
col empty_blocks for 99999
select owner,table_name,num_rows,partition_name,LAST_ANALYZED,blocks,empty_blocks
from dba_tab_statistics
where owner=''
and table_name='';



-- -- -- -- -- -- -- -- -- -- 索引統計系信息-- -- -- -- -- -- -- -- 
查看索引的聚簇因子,
-- 聚簇因子應該與表的塊數近似
set line 100
col ower for a10
col table_name for a20
col index_name for a20
col clustering_factor for 999999999
col NUM_ROWS for 99999
select owner,table_name,index_name,clustering_factor,NUM_ROWS 
from dba_indexes 
where index_name='&index_name'
and owner='&owner'
and table_name='&table_name';

查看數據表當前佔用block數目
select count(dbms_rowid.rowid_block_number(rowid)) from &table_name;


-- -- -- -- -- -- -- -- -- -- -- --列統計信息 -- -- -- -- -- -- -- -- -- -- -- -- 
set line 100 verify off
col low_value for a30
col high_value for a30
col num_distinct for 99999
col num_nulls for 9999999
select low_value,high_value,num_distinct,num_nulls 
from dba_tab_col_statistics 
where table_name='&table_name'
and owner='&owner'
and column_name='&column_name';





-- 1   41,43,43,45,53,53,24  5F75746C245F6C6E635F696E645F7061727473  10807   0
var l varchar2(100);
exec dbms_stats.convert_row_value('5F75746C245F6C6E635F696E645F7061727473',:l);
print l;






#######################V$sqlarea########################################
set line 200
col sql_id for a30
col plan_hash_value for 30
col executions for 99999
col first_load_time for a30
col disk_reads for 99999
col buffer_gets for 9999999
col cpu_time for 9999999999
col last_load_time for 999999999
col last_active_time for 99999999
col loads for 999999
select sql_id,plan_hash_value,executions,first_load_time,disk_reads,buffer_gets,cpu_time,last_load_time,last_active_time,loads
from v$sqlarea
where sql_id='<>';


###################################################################查詢隱含參數

SELECT   KSPPINM, KSPPSTVL, KSPPDESC
FROM   X$KSPPI X, X$KSPPCV Y
WHERE   X.INDX = Y.INDX AND  KSPPINM = '_db_file_optimizer_read_count';

#################通過創建索引來避免全表掃描,不必要的排序,函數索引,重建索引避免全表掃描#################

1.創建複合索引
create index <index_name> on <table_name>(column_name,0)  tablespace <tablespace_name>;
原理:雖然對於單鍵值B樹索引而言NULL值不入索引,但對於複合索引B樹索引來說,NULL值是入索引的。


2.對於需要排序的列創建索引
create index <index_name> table_name(column_name) tablespace <tablespace_name>;

3.
百分號在最前面的like條件確實是沒法走索引的,-----'%....'
但同時我們也知道對於有百分號的like條件,
如果百分號的like條件的最後面,則這種情況下是可以走索引的。-----'...%'
在不改變SQL的情形下把百分號從like條件的最前面移到最後面

create index <index_name> on <table_name>(reverse(column_name)) ;

4.創建全局索引
create index <index_name> on <table_name> (<column_name>) online tablespace <tablespace_name>




###################對症下藥
1.如果是統計信息不準或是因爲CBO計算某些SQL的成本所用的公式先天不足而導致的SQL性能問題,
我們可以通過重新收集統計信息,或者手動修改統計信息,或者使用Hint來加以解決。

2.如果是SQL語句的寫法問題,我們可以通過在不更改業務邏輯的情況下改寫SQL來加以解決。

3.如果是不必要的全表掃描/排序而導致的目標SQL的性能問題,我們可以通過建立合適的索引(包括函數索引、位圖索引)來解決。

4.如果是因爲各種原因導致的目標SQL的執行計劃不穩定,可以通過使用hint、sql profile或者spm來加以解決。

5.如果是表或者索引的不良設計導致的目標SQL的性能問題,我們可以通過重新設計表/索引,重新組織表的裏的數據來加以解決。

6.如果上述調整措施都失效,可以考慮用並行來縮短目標SQL的執行時間。

7.如果上述調整措施都失效,調整SQL。





#################創建索引##############################
create index <index_name> table_name(column_name) tablespace <tablespace_name> online nologging parallel 2;
alter index <index_name> noparallel;





##################分區表信息確認##################
1.


set line 100
col table_name for a30
col num_rows for 999999
col blocks for 9999999
col avg_row_len for 99999999
col partitioned for a10
col ext_M for 9999999
select table_name,
num_rows ,--記錄數
blocks,
avg_row_len,
partitioned, --是否分區
num_rows * avg_row_len /1024/1024/1024/0.9 est_M  -- "數據量大小"
from dba_tables
where table_name='&table_name';





set line 100
col partitioning_type for a10
col subpartitioning_type for a10
select partitioning_type, --分區類型
subpartitioning_type 
from dba_part_tables 
where table_name='&table_name'


set line 100
col owner for a10
col name for a20
col object_type for a30
col column_name for a30 --分區列
col column_position for a30
select * 
from dba_part_key_columns 
where name ='&table_name';


#############SPM########################
在當前session中禁掉spm並同時開啓自動捕獲sql plan baseline
alter session set optimizer_use_sql_plan_baseline=false;
alter session set optimizer_capture_sql_plan_baseline=true;

select sql_handle,plan_name,origin,enabled,accepted,sql_text from dba_sql_plan_baseline where sql_text like  '';

############################執行計劃######################
alter session set statistics_level='ALL' ;
set line 200 pages 100
select *  from  table(dbms_xplan.display_cursor(null,null,'allstats last'));

-------------------------------------------------------------------------------------
| Id  | Operation          | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |      1 |        |      9 |00:00:00.01 |       4 |
|*  1 |  COUNT STOPKEY     |      |      1 |        |      9 |00:00:00.01 |       4 |
|   2 |   TABLE ACCESS FULL| T1   |      1 |      9 |      9 |00:00:00.01 |       4 |
-------------------------------------------------------------------------------------
Starts:表示每個具體執行計劃步驟被重複執行的次數
E-Rows:表示執行計劃中cbo對每一個具體執行計劃步驟返回結果集cardinality的預估值
A-Rows:表示執行計劃中每個具體步驟的返回結果集的cardinality的實際值
A-Time:表示執行計劃中每一個具體執行計劃中實際執行時間
Buffers:表示執行計劃中每一個具體執行計劃步驟實際消耗的邏輯讀



###########################選擇率################################3
對於like '%...%'這樣的模糊查詢條件,cbo默認的可選擇率5%來評估其返回結果集的cardinality的值


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章