Oracle 性能相关常用脚本(SQL)
在缺乏的可视化工具来监控数据库性能的情形下,常用的脚本就派上用场了,下面提供几个关于Oracle性能相关的脚本供大家参考。以下脚本均在Oracle 10g测试通过,Oracle 11g可能要做相应调整。
1、寻找最多BUFFER_GETS开销的SQL 语句
- --filename: top_sql_by_buffer_gets.sql
- --Identify heavy SQL (Get the SQL with heavy BUFFER_GETS)
- SET LINESIZE 190
- COL sql_text FORMAT a100 WRAP
- SET PAGESIZE 100
- SELECT *
- FROM ( SELECT sql_text,
- sql_id,
- executions,
- disk_reads,
- buffer_gets
- FROM v$sqlarea
- WHERE DECODE (executions, 0, buffer_gets, buffer_gets / executions) >
- (SELECT AVG (DECODE (executions, 0, buffer_gets, buffer_gets / executions))
- + STDDEV (DECODE (executions, 0, buffer_gets, buffer_gets / executions))
- FROM v$sqlarea)
- AND parsing_user_id != 3D
- ORDER BY 5 DESC) x /*更正@20140613,原来为order by 4,感谢网友lmalds指正*/
- WHERE ROWNUM <= 10;
2、寻找最多DISK_READS开销的SQL 语句
- --filename:top_sql_disk_reads.sql
- --Identify heavy SQL (Get the SQL with heavy DISK_READS)
- SET LINESIZE 190
- COL sql_text FORMAT a100 WRAP
- SET PAGESIZE 100
- SELECT *
- FROM ( SELECT sql_text,
- sql_id,
- executions,
- disk_reads,
- buffer_gets
- FROM v$sqlarea
- WHERE DECODE (executions, 0, disk_reads, disk_reads / executions) >
- (SELECT AVG (DECODE (executions, 0, disk_reads, disk_reads / executions))
- + STDDEV (DECODE (executions, 0, disk_reads, disk_reads / executions))
- FROM v$sqlarea)
- AND parsing_user_id != 3D
- ORDER BY 4 DESC) x /* 更正@20140613,原来为order by 3,谢谢网友lmalds指正*/
- WHERE ROWNUM <= 10;
3、寻找最近30分钟导致资源过高开销的事件
- --filename:top_event_in_30_min.sql
- --Last 30 minutes result those resources that are in high demand on your system.
- SET LINESIZE 180
- COL event FORMAT a60
- COL total_wait_time FORMAT 999999999999999999
- SELECT active_session_history.event,
- SUM (
- active_session_history.wait_time
- + active_session_history.time_waited)
- total_wait_time
- FROM v$active_session_history active_session_history
- WHERE active_session_history.sample_time BETWEEN SYSDATE - 60 / 2880
- AND SYSDATE
- AND active_session_history.event IS NOT NULL
- GROUP BY active_session_history.event
- ORDER BY 2 DESC;
4、查找最近30分钟内等待最多的用户
- --filename:top_wait_by_user.sql
- --What user is waiting the most?
- SET LINESIZE 180
- COL event FORMAT a60
- COL total_wait_time FORMAT 999999999999999999
- SELECT ss.sid,
- NVL (ss.username, 'oracle') AS username,
- SUM (ash.wait_time + ash.time_waited) total_wait_time
- FROM v$active_session_history ash, v$session ss
- WHERE ash.sample_time BETWEEN SYSDATE - 60 / 2880 AND SYSDATE AND ash.session_id = ss.sid
- GROUP BY ss.sid, ss.username
- ORDER BY 3 DESC;
5、查找30分钟消耗最多资源的SQL语句
- --filename:top_sql_by_wait.sql
- -- What SQL is currently using the most resources?
- SET LINESIZE 180
- COL sql_text FORMAT a90 WRAP
- COL username FORMAT a20 WRAP
- SET PAGESIZE 200
- SELECT *
- FROM ( SELECT sqlarea.sql_text,
- dba_users.username,
- sqlarea.sql_id,
- SUM (active_session_history.wait_time + active_session_history.time_waited)
- total_wait_time
- FROM v$active_session_history active_session_history, v$sqlarea sqlarea, dba_users
- WHERE active_session_history.sample_time BETWEEN SYSDATE - 60 / 2880 AND SYSDATE
- AND active_session_history.sql_id = sqlarea.sql_id
- AND active_session_history.user_id = dba_users.user_id
- GROUP BY active_session_history.user_id,
- sqlarea.sql_text,
- sqlarea.sql_id,
- dba_users.username
- ORDER BY 4 DESC) x
- WHERE ROWNUM <= 11;
6、等待最多的对象
- --filename:top_object_by_wait.sql
- --What object is currently causing the highest resource waits?
- SET LINESIZE 180
- COLUMN OBJECT_NAME FORMAT a30
- COLUMN EVENT FORMAT a30
- SELECT dba_objects.object_name,
- dba_objects.object_type,
- active_session_history.event,
- SUM (active_session_history.wait_time + active_session_history.time_waited) ttl_wait_time
- FROM v$active_session_history active_session_history, dba_objects
- WHERE active_session_history.sample_time BETWEEN SYSDATE - 60 / 2880 AND SYSDATE
- AND active_session_history.current_obj# = dba_objects.object_id
- GROUP BY dba_objects.object_name, dba_objects.object_type, active_session_history.event
- ORDER BY 4 DESC;
7、寻找基于指定时间范围内的历史SQL语句
- --注该查询受到awr快照相关参数的影响
- -- filename:top_sql_in_spec_time.sql
- --Top SQLs Elaps time and CPU time in a given time range..
- --X.ELAPSED_TIME/1000000 => From Micro second to second
- --X.ELAPSED_TIME/1000000/X.EXECUTIONS_DELTA => How many times the sql ran
- SET PAUSE ON
- SET PAUSE 'Press Return To Continue'
- SET LINESIZE 180
- COL sql_text FORMAT a80 WRAP
- SELECT sql_text,
- dhst.sql_id,
- ROUND (x.elapsed_time / 1000000 / x.executions_delta, 3) elapsed_time_sec,
- ROUND (x.cpu_time / 1000000 / x.executions_delta, 3) cpu_time_sec,
- x.elapsed_time,
- x.cpu_time,
- executions_delta AS exec_delta
- FROM dba_hist_sqltext dhst,
- ( SELECT dhss.sql_id sql_id,
- SUM (dhss.cpu_time_delta) cpu_time,
- SUM (dhss.elapsed_time_delta) elapsed_time,
- CASE SUM (dhss.executions_delta) WHEN 0 THEN 1 ELSE SUM (dhss.executions_delta) END
- AS executions_delta
- FROM dba_hist_sqlstat dhss
- WHERE dhss.snap_id IN
- (SELECT snap_id
- FROM dba_hist_snapshot
- WHERE begin_interval_time >= TO_DATE ('&input_start_date', 'YYYYMMDD HH24:MI')
- AND end_interval_time <= TO_DATE ('&input_end_date', 'YYYYMMDD HH24:MI'))
- GROUP BY dhss.sql_id) x
- WHERE x.sql_id = dhst.sql_id
- ORDER BY elapsed_time_sec DESC;
8、寻找基于指定时间范围内及指定用户的历史SQL语句
- --注该查询受到awr快照相关参数的影响
- --Author : Robinson
- --Blog : http://blog.csdn.net/robinson_0612
- SELECT DBMS_LOB.SUBSTR (sql_text, 4000, 1) AS sql,
- ROUND (x.elapsed_time / 1000000, 2) elapsed_time_sec,
- ROUND (x.cpu_time / 1000000, 2) cpu_time_sec,
- x.executions_delta AS exec_num,
- ROUND ( (x.elapsed_time / 1000000) / x.executions_delta, 2) AS exec_time_per_query_sec
- FROM dba_hist_sqltext dhst,
- ( SELECT dhss.sql_id sql_id,
- SUM (dhss.cpu_time_delta) cpu_time,
- SUM (dhss.elapsed_time_delta) elapsed_time,
- CASE SUM (dhss.executions_delta) WHEN 0 THEN 1 ELSE SUM (dhss.executions_delta) END
- AS executions_delta
- --DHSS.EXECUTIONS_DELTA = No of queries execution (per hour)
- FROM dba_hist_sqlstat dhss
- WHERE dhss.snap_id IN
- (SELECT snap_id
- FROM dba_hist_snapshot
- WHERE begin_interval_time >= TO_DATE ('&input_start_date', 'YYYYMMDD HH24:MI')
- AND end_interval_time <= TO_DATE ('&input_end_date', 'YYYYMMDD HH24:MI'))
- AND dhss.parsing_schema_name LIKE UPPER ('%&input_username%')
- GROUP BY dhss.sql_id) x
- WHERE x.sql_id = dhst.sql_id
- ORDER BY elapsed_time_sec DESC;
9、SQL语句被执行的次数
- --exe_delta表明在指定时间内增长的次数
- -- filename: sql_exec_num.sql
- -- How many Times a query executed?
- SET LINESIZE 180
- SET VERIFY OFF
- SELECT TO_CHAR (s.begin_interval_time, 'yyyymmdd hh24:mi:ss'),
- sql.sql_id AS sql_id,
- sql.executions_delta AS exe_delta,
- sql.executions_total
- FROM dba_hist_sqlstat sql, dba_hist_snapshot s
- WHERE sql_id = '&input_sql_id'
- AND s.snap_id = sql.snap_id
- AND s.begin_interval_time > TO_DATE ('&input_start_date', 'YYYYMMDD HH24:MI')
- AND s.begin_interval_time < TO_DATE ('&input_end_date', 'YYYYMMDD HH24:MI')
- ORDER BY s.begin_interval_time;
- 转:http://blog.csdn.net/leshami/article/details/8904804
Oracle 性能相关常用脚本(SQL)的更多相关文章
- SqlServer和Oracle中一些常用的sql语句9 SQL优化
--SQL查询优化 尽量避免使用or,not,distinct运算符,简化连接条件 /*Or运算符*/ use db_business go select * from 仓库 where 城市='北京 ...
- SqlServer和Oracle中一些常用的sql语句10 特殊应用
--482, ORACLE / SQL SERVER --订购数量超过平均值的书籍 WITH Orders_Book AS ( SELECT Book_Name, SUM(Qty) Book_Qty ...
- SqlServer和Oracle中一些常用的sql语句3 行列转换
--217, SQL SERVER SELECT Cust_Name , MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009- ...
- SqlServer和Oracle中一些常用的sql语句5 流程控制语句
--在sql语句中 begin...end 用来设定一个程序块 相关于c#中的{} declare @yz real,@w int --声明变量 set @w=120 --为变量赋值 if @w< ...
- 常用脚本--SQL Server获取OS日志
--=================================================== --SQL Server获取OS日志: ), ), ), ) select @start_d ...
- SqlServer和Oracle中一些常用的sql语句6 存储过程
--不带参数的存储过程 CREATE procedure proc_sql1 as begin declare @i int set @i=0 while @i<26 begin print c ...
- SqlServer和Oracle中一些常用的sql语句7 游标
declare db_cursor4 scroll cursor for select * from 供应商 --声明游标 open db_cursor4 --打开游标 fetch first fro ...
- SqlServer和Oracle中一些常用的sql语句8 触发器和事务
--创建和执行事后触发器 --更新仓库备份表中记录时自动创建数据表且插入三条记录 create trigger db_trigger1 on 仓库备份 for update as begin if E ...
- SqlServer和Oracle中一些常用的sql语句4 局部/全局变量
--把wh1仓库号中姓名含有"平"字的职工工资在原来的基础上加288 update 职工备份 set 工资=工资+288 where 仓库号='wh1' and 姓名 like ' ...
随机推荐
- Java 网络编程(二)
以下例开始本文的内容: 例1,需求:上传图片. 客户端: 服务端点. 读取客户端已有的图片数据. 通过socket输出流将数据发给服务端. 读取服务端反馈信息. 关闭. class PicClient ...
- 理解 JMeter 聚合报告(Aggregate Report)
Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...
- oci.dll文件是用来干嘛的? 如果没有安装ORACLE客户端提示oci.dll未加载
oracle数据库开发编程中,没有找到oci.dll,一般是系统的 path 设置有问题, 查找oci.dll, 然后加入到系统路径.oci.dll 可下载解压到系统盘的system32目录下.然后打 ...
- HTML基本操作
插入图片: 1.利用链接(静态) <img src="http://www.kmwzjs.com/useruploads/images/20101020_057600100825157 ...
- Android 核心分析 之七Service深入分析
Service深入分析 上一章我们分析了Android IPC架构,知道了Android服务构建的一些基本理念和原理,本章我们将深入分析Android的服务.Android体系架构中三种意义上服务: ...
- 点击Button后,执行MouseDown的过程(使用Call Stack观察很清楚)
Form1上放两个按钮Button1和Button2,默认输入焦点是Button1,现在点击Button2,产生WM_LBUTTONDOWN消息 procedure TForm1.Button2Mou ...
- 一步步来配置安卓开发环境ADTBundle
前提 安装JDK,并且要正确配置环境变量. 特别要注意安装配置完后,CMD命令行窗口里输入 java 和 javac 查看是否有内容输出,若提示找不到命令就是你JDK配置错误了 至于如何配置JDK看这 ...
- 如何在github上展示作品——为你的项目生成一个快速访问的网址如(DaisyWang88.github.io)
(这里值针对Windos系统的,因为本人用的是Window系统,暂时没有条件在其他平台上测试) 1.创建命名为 <userName>.github.io的仓库. 这里的u ...
- skip-name-resolv
skip-name-resolve skip-name-resolve 简单解释 MySQL server received a request from you to allow you to co ...
- Error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)"
Error 2 error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToStr ...