################################################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 ecount(*); 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(200); select dbms_stats.CONVERT_RAW_VALUE('41434345535324',:l) from dual; -- select max(&&column_name),dump(max(&column_name)) from &table_name; #######################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的值
oracle运维-常用SQL
猜你喜欢
转载自blog.51cto.com/10579005/2337788
今日推荐
周排行