1、查找排序最多的SQL

SQL>
SELECT HASH_VALUE, SQL_TEXT, SORTS, EXECUTIONS FROM V$SQLAREA ORDER BY SORTS DESC;

2、查找磁盘读写最多的SQL

SQL>
SELECT * FROM (SELECT sql_text,disk_reads "total disk" , executions "total exec",disk_reads/executions "disk/exec" 
FROM v$sql WHERE executions>0 
and is_obsolete='N' ORDER BY 4 desc) WHERE ROWNUM<11 ;

3、查找工作量最大的SQL(实际上也是按磁盘读写来排序的)

SQL>
select substr(to_char(s.pct, '99.00'), 2) || '%' load,s.executions executes,p.sql_text 
from(select address,disk_reads,executions,pct,rank() 
over (order by disk_reads desc) ranking 
from (select address,disk_reads,executions,100 * ratio_to_report(disk_reads) over () pct 
from sys.v_$sql 
where command_type != 47) 
where disk_reads > 50 * executions) s,sys.v_$sqltext p 
where s.ranking <= 5 and p.address = s.address order by 1, s.address, p.piece;

4、查找低效的SQL语句

SQL>
select executions,disk_reads,buffer_gets,round((buffer_gets-disk_reads)/buffer_gets,2) Hit_radio,round(disk_reads/executions,2) reads_per_run,sql_text 
From v$sqlarea 
Where executions>0 and buffer_gets >0 
and (buffer_gets-disk_reads)/buffer_gets<0.8 Order by 4 desc;

5、根据sid查看对应连接正在运行的sql

SQL>
select /*+ push_subq */command_type,sql_text,sharable_mem,persistent_mem,runtime_mem,sorts,version_count,loaded_versions,open_versions,users_opening,executions,users_executing,loads,first_load_time,invalidations,parse_calls,disk_reads,buffer_gets,rows_processed,sysdate start_time,sysdate finish_time,’>’||address sql_address,’N’status 
From v$sqlarea 
Where address=(select sql_address from v$session where sid=&sid);

6、查看datafile数据文件使用情况(sys用户执行)

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; 

7、查看数据库表空间使用情况
语句一:

SQL>
SELECT a.tablespace_name "表空间名", 
total "表空间大小", 
free "表空间剩余大小", 
(total - free) "表空间使用大小", 
total / (1024 * 1024 * 1024) "表空间大小(G)", 
free / (1024 * 1024 * 1024) "表空间剩余大小(G)", 
(total - free) / (1024 * 1024 * 1024) "表空间使用大小(G)", 
round((total - free) / total, 4) * 100 "使用率 %" 
FROM (SELECT tablespace_name, SUM(bytes) free 
FROM dba_free_space 
GROUP BY tablespace_name) a, 
(SELECT tablespace_name, SUM(bytes) total 
FROM dba_data_files 
GROUP BY tablespace_name) b 
WHERE a.tablespace_name = b.tablespace_name;

语句二:

SQL>
SELECT SUM(bytes) / (1024 * 1024) AS free_space, tablespace_name 
FROM dba_free_space 
GROUP BY tablespace_name; 
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; 

8、查看表空间物理文件的名称及大小

SQL>
SELECT tablespace_name,
 file_id,
 file_name,
 round(bytes / (1024 * 1024 * 1024), 0) total_space_GB
 FROM dba_data_files
 ORDER BY tablespace_name;

9、查看数据库的创建日期和归档方式

SQL>
 SELECT created, log_mode, log_mode FROM v$database;

10、查询是否开启归档模式

 SQL>
 select name,log_mode,open_mode from v$database;

若是归档模式,则LOG_MODE=ARCHIVELOG
若是非归档模式,则LOG_MODE=NOARCHIVELOG
11、查看表空间是否自动扩展

SQL>
select file_name,autoextensible,increment_by from dba_data_files;

12、查询表空间最大扩展大小(注:当表空间开启了自动扩展特性,表空间会一直扩展到操作系统支持的最大大小)

SQL>
select tablespace_name,maxblocks*8 from dba_data_files;

13、查看总消耗时间最多的前10条SQL语句

SQL>
select * from (select v.sql_id,v.child_number,v.sql_text,v.elapsed_time,v.cpu_time,v.disk_reads,rank() 
over(order by v.elapsed_time desc) elapsed_rank 
from v$sql v) a where elapsed_rank <= 10;

14、查看CPU消耗时间最多的前10条SQL语句

SQL>
select * from (select v.sql_id,v.child_number,v.sql_text,v.elapsed_time,v.cpu_time,v.disk_reads,rank() 
over(order by v.cpu_time desc) elapsed_rank from v$sql v) a 
where elapsed_rank <= 10;

15、查看消耗磁盘读取最多的前10条SQL语句

SQL>
select * from (select v.sql_id,v.child_number,v.sql_text,v.elapsed_time,v.cpu_time,v.disk_reads,rank() over(order by v.disk_reads desc) elapsed_rank 
from v$sql v) a 
where elapsed_rank <= 10;

16、列出cpu_time占用top 10的sql

SQL>
select cpu_time,sql_text 
from (select sql_text,cpu_time,rank() over (order by cpu_time desc) exec_rank from v$sql) 
where exec_rank <=10;

17、执行次数最多的top 10

SQL>
select sql_text,executions 
from (select sql_text,executions,rank() over (order by executions desc) exec_rank from v$sql) 
where exec_rank <=10;

18、遭遇cpu过多占用,表现为%usr很高,top 或者topas中cpu占用最多的进程为oracle server process.则根据pid可以找出该pid对应的sql_text

SQL>
select se.username,se.machine,sq.cpu_time,sq.sql_text 
from v$process p,v$session se,v$sqlarea sq 
where p.addr=se.paddr and se.sql_hash_value=sq.hash_value and p.addr='&pid';

19、如何查询Oracle 编码格式

SQL>
select value from nls_database_parameters where parameter='NLS_CHARACTERSET';

20、查询oracle客户端编码

SQL>
select * from nls_instance_parameters where parameter='NLS_LANGUAGE';

21、缓冲区命中率:大于98%为最佳

SQL>
select (1-(sum(decode(name, 'physical reads',value,0))/(sum(decode(name, 'db block gets',value,0)) +sum(decode(name,'consistent gets',value,0))))) * 100 "Hit Ratio" 
from v$sysstat;

22、数据字典缓存命中率:大于98%为最佳

SQL>
select (1-(sum(getmisses)/sum(gets))) * 100 "Hit Ratio" from v$rowcache;

23、库缓存命中率:大于98%为最佳

SQL>
select Sum(Pins)/(Sum(Pins) + Sum(Reloads)) * 100 "Hit Ratio" from V$LibraryCache;

24、空闲的数据缓冲区的比例,空闲比例高于25%时,数据缓冲区设置得太大了,可能会浪费资源

SQL>
select decode(state,0, 'FREE',1,decode(lrba_seq,0,'AVAILABLE','BEING USED'),3, 'BEING USED', state) "BLOCK STATUS",count(*) 
from x$bh group by decode(state,0,'FREE',1,decode(lrba_seq,0,'AVAILABLE','BEING USED'),3, 'BEING USED', state);

25、最浪费内存的前10个语句占所有语句的比例(小于5%为最佳)

SQL>
select sum(pct_bufgets) "Percent" 
from (select rank() over ( order by buffer_gets desc ) as rank_bufgets,to_char(100 * ratio_to_report(buffer_gets) over (),'999.99') pct_bufgets 
from v$sqlarea ) where rank_bufgets < 11;

26、调整滥用磁盘读操作的主要语句

SQL>
select disk_reads, substr(sql_text,1,4000) from v$sqlarea  order by disk_reads desc;