oracle-调优-如何分析分析sql历史执行信息

简介:

查看sql的文本

set line 300
col sql_text for a200
select * from dba_hist_sqltext where sql_id='dxxxh350mfyvj';
      DBID SQL_ID        SQL_TEXT                                                                                                                                                                                                 COMMAND_TYPE
---------- ------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------
 280856689 dxxxh350mfyvj SELECT 'X' FROM tableA WHERE (RCARD, MOCODE) IN (SELECT RCARD, MOCODE FROM (SELECT /*+index(PK_tableA)*/ RCARD, MOCODE FROM tableA WHERE RCARD = :B2 AND ORGID = :B1 ORDER BY MDATE            3
                          DESC, MTIME DESC) WHERE ROWNUM = 1) AND ORGID = :B1 FOR UPDATE                                                                                                                                                      
                                                                                                                                                                                                                                                                                                            
已选择 1 行。

查看sql的历史执行计划有几个

select distinct SQL_ID,PLAN_HASH_VALUE,to_char(TIMESTAMP,'yyyymmdd hh24:mi:ss')  TIMESTAMP
from dba_hist_sql_plan 
where SQL_ID='dxxxh350mfyvj' order by TIMESTAMP;

SQL_ID        PLAN_HASH_VALUE TIMESTAMP        
------------- --------------- -----------------
dxxxh350mfyvj      3728660352 20220218 22:10:58
已选择 1 行。

select distinct SQL_ID,PLAN_HASH_VALUE,to_char(TIMESTAMP,'yyyymmdd hh24:mi:ss')  TIMESTAMP
from v$sql_plan
where SQL_ID='2usurkuv91kq5' order by TIMESTAMP;

根据sql_id和plan_hash,查看sql的执行计划

set line 300
col options for a25
col operation for a30
col object_name for a30
select plan_hash_value,id,operation,options,object_name,depth,cost,to_char(TIMESTAMP,'yyyymmdd hh24:mi:ss')
    from DBA_HIST_SQL_PLAN  
    where sql_id ='dxxxh350mfyvj' 
    and plan_hash_value in (3728660352)
    order by plan_hash_value,ID,TIMESTAMP;

PLAN_HASH_VALUE         ID OPERATION                      OPTIONS                   OBJECT_NAME                         DEPTH       COST TO_CHAR(TIMESTAMP,'YYYYMMDDHH24:MI:SS')
--------------- ---------- ------------------------------ ------------------------- ------------------------------ ---------- ---------- ---------------------------------------
     3728660352          0 SELECT STATEMENT                                                                                 0          8 20220218 22:10:58                      
     3728660352          1 FOR UPDATE                                                                                       1            20220218 22:10:58                      
     3728660352          2 BUFFER                         SORT                                                              2            20220218 22:10:58                      
     3728660352          3 NESTED LOOPS                                                                                     3          8 20220218 22:10:58                      
     3728660352          4 VIEW                                                     VW_NSO_1                                4          6 20220218 22:10:58                      
     3728660352          5 SORT                           UNIQUE                                                            5            20220218 22:10:58                      
     3728660352          6 COUNT                          STOPKEY                                                           6            20220218 22:10:58                      
     3728660352          7 VIEW                                                                                             7          6 20220218 22:10:58                      
     3728660352          8 SORT                           ORDER BY STOPKEY                                                  8          6 20220218 22:10:58                      
     3728660352          9 PARTITION HASH                 SINGLE                                                            9          5 20220218 22:10:58                      
     3728660352         10 TABLE ACCESS                   BY GLOBAL INDEX ROWID     tableA                          10          5 20220218 22:10:58                      
     3728660352         11 INDEX                          RANGE SCAN                PK_tableA_NEW                   11          4 20220218 22:10:58                      
     3728660352         12 PARTITION HASH                 ITERATOR                                                          4          2 20220218 22:10:58                      
     3728660352         13 INDEX                          UNIQUE SCAN               PK_tableA_NEW                    5          2 20220218 22:10:58                      

已选择 14 行。

查看sql执行历史的统计信息(平均执行时间,cpu,io,get等)

COL inst FOR 9
SET LINESIZE 340
SET PAGESIZE 10000
COL plan_value FOR 9999999999
COL module FOR a35
COL get_per FOR 99999999
COL read_per FOR 99999999
COL rows_per FOR 99999999
COL elas_per FOR 9999999999
COL cpu_per FOR 99999999
COL IO_per FOR 99999999
COL clu_per FOR 99999999
COL app_per FOR 99999999
COL concur_per FOR 99999999

SELECT *
  FROM (  SELECT TO_CHAR (begin_interval_time, 'yyyy-mm-dd hh24:mi:ss')
                    begin_time,
                    TO_CHAR (end_interval_time, 'yyyy-mm-dd hh24:mi:ss')
                    end_time,
                 a.instance_number inst,
                 a.module,
                 plan_hash_value plan_value,
                 a.sql_id,
                 EXECUTIONS_DELTA exec,
                 ROUND (a.ELAPSED_TIME_DELTA / 1000, 2) total_elastime_ms,
                 ROUND (A.CPU_TIME_DELTA / 1000, 2) total_cputime_ms,
                 ROUND (a.IOWAIT_DELTA / 1000, 2) total_iotime_ms,
                 A.BUFFER_GETS_DELTA total_buffer_get,
                 a.DISK_READS_DELTA total_disk_read,
                 DECODE (EXECUTIONS_DELTA,
                         0, buffer_gets_deltA,
                         ROUND (BUFFER_GETS_DELTA / EXECUTIONS_DELTA))
                    get_per,
                 DECODE (EXECUTIONS_DELTA,
                         0, DISK_READS_DELTA,
                         ROUND (DISK_READS_DELTA / EXECUTIONS_DELTA))
                    read_per,
                 DECODE (EXECUTIONS_DELTA,
                         0, ROWS_PROCESSED_DELTA,
                         ROUND (ROWS_PROCESSED_DELTA / EXECUTIONS_DELTA))
                    rows_per,
                 DECODE (
                    EXECUTIONS_DELTA,
                    0, ELAPSED_TIME_DELTA,
                    ROUND (ELAPSED_TIME_DELTA / EXECUTIONS_DELTA / 1000, 2))
                    elas_per_ms,
                 DECODE (EXECUTIONS_DELTA,
                         0, CPU_TIME_DELTA,
                         ROUND (CPU_TIME_DELTA / EXECUTIONS_DELTA / 1000, 2))
                    cpu_per_ms,
                 DECODE (EXECUTIONS_DELTA,
                         0, IOWAIT_DELTA,
                         ROUND (IOWAIT_DELTA / EXECUTIONS_DELTA / 1000, 2))
                    IO_per_ms,
                 DECODE (EXECUTIONS_DELTA,
                         0, CLWAIT_DELTA,
                         ROUND (CLWAIT_DELTA / EXECUTIONS_DELTA / 1000, 2))
                    clu_per_ms,
                 DECODE (EXECUTIONS_DELTA,
                         0, APWAIT_DELTA,
                         ROUND (APWAIT_DELTA / EXECUTIONS_DELTA / 1000, 2))
                    app_per_ms,
                 DECODE (EXECUTIONS_DELTA,
                         0, CCWAIT_DELTA,
                         ROUND (CCWAIT_DELTA / EXECUTIONS_DELTA / 1000, 2))
                    concur_per_ms,
                 DECODE (EXECUTIONS_DELTA,
                         0, LOADS_DELTA,
                         ROUND (LOADS_DELTA / EXECUTIONS_DELTA, 2))
                    per_loads
            FROM dba_hist_sqlstat a, DBA_HIST_SNAPSHOT b
           WHERE     a.snap_id = b.snap_id
                 AND a.instance_number = b.instance_number
                 AND a.sql_id = 'dxxxh350mfyvj'
        ORDER BY 1 DESC)
 WHERE ROWNUM < 100;

BEGIN_TIME          END_TIME            INST MODULE                               PLAN_VALUE SQL_ID              EXEC TOTAL_ELASTIME_MS TOTAL_CPUTIME_MS TOTAL_IOTIME_MS TOTAL_BUFFER_GET TOTAL_DISK_READ   GET_PER  READ_PER  ROWS_PER ELAS_PER_MS CPU_PER_MS  IO_PER_MS CLU_PER_MS APP_PER_MS CONCUR_PER_MS  PER_LOADS
------------------- ------------------- ---- ----------------------------------- ----------- ------------- ---------- ----------------- ---------------- --------------- ---------------- --------------- --------- --------- --------- ----------- ---------- ---------- ---------- ---------- ------------- ----------
2022-04-16 15:30:42 2022-04-16 16:02:56    1 vivoTestStand20211107154927.exe      3728660352 dxxxh350mfyvj     154010        81884356.4         12639.58          619.62          1720577             280        11         0         1      531.68        .08          0          0        .13        456.95          0
2022-04-14 14:00:33 2022-04-14 14:30:41    1                                               0 dxxxh350mfyvj                     21918.64          9919.31                                                                                                                                                                
2022-04-14 13:30:24 2022-04-14 14:00:33    1                                               0 dxxxh350mfyvj                     19395.85          8990.93                                                                                                                                                                
2022-04-14 13:00:15 2022-04-14 13:30:24    1                                               0 dxxxh350mfyvj                        15279          7269.61                                                                                                                                                                
2022-04-14 12:30:08 2022-04-14 13:00:15    1                                               0 dxxxh350mfyvj                     12710.42          6711.23                                                                                                                                                                
2022-04-14 12:00:01 2022-04-14 12:30:08    1                                               0 dxxxh350mfyvj                     14246.66          7469.19                                                                                                                                                                
2022-04-14 11:30:54 2022-04-14 12:00:01    1                                               0 dxxxh350mfyvj                     17609.83          8973.17                                                                                                                                                                
2022-04-14 11:00:46 2022-04-14 11:30:54    1                                               0 dxxxh350mfyvj                     19390.37          9988.51                                                                                                                                                                
2022-04-14 10:30:38 2022-04-14 11:00:46    1                                               0 dxxxh350mfyvj                     20320.79         10569.63                                                                                                                                                                
2022-04-14 10:00:30 2022-04-14 10:30:38    1                                               0 dxxxh350mfyvj                      21101.4         10743.94                                                                                                                                                                

查询该sql的历史执行情况

col snap_id for 99999999                                                                                   
col date_time for a30                                                                                      
col plan_hash for 9999999999                                                                               
col executions for 99999999                                                                                
col avg_etime_s heading 'etime/exec' for 9999999.99                                                        
col avg_lio heading 'buffer/exec' for 99999999999                                                          
col avg_pio heading 'diskread/exec' for 99999999999                                                        
col avg_cputime_s heading 'cputim/exec' for 9999999.99                                                     
col avg_row heading 'rows/exec' for 9999999                                                                
select * from(                                                                                             
select distinct                                                                                            
s.snap_id,                                                                                                 
to_char(s.begin_interval_time,'mm/dd/yy_hh24mi') || to_char(s.end_interval_time,'_hh24mi') date_time,      
sql.plan_hash_value plan_hash,                                                                             
sql.executions_delta executions,                                                                           
(sql.elapsed_time_delta/1000000)/decode(sql.executions_delta,null,1,0,1,sql.executions_delta) avg_etime_s, 
sql.buffer_gets_delta/decode(sql.executions_delta,null,1,0,1,sql.executions_delta) avg_lio,                
sql.disk_reads_delta/decode(sql.executions_delta,null,1,0,1,sql.executions_delta) avg_pio,                 
(sql.cpu_time_delta/1000000)/decode(sql.executions_delta,null,1,0,1,sql.executions_delta) avg_cputime_s,   
sql.rows_processed_total/decode(sql.executions_delta,null,1,0,1,sql.executions_delta) avg_row              
from dba_hist_sqlstat sql, dba_hist_snapshot s                                                             
where sql.instance_number =(select instance_number from v$instance)                                        
and sql.dbid =(select dbid from v$database)                                                                
and s.snap_id = sql.snap_id                                                                                
and sql_id = trim('&sql_id') order by s.snap_id desc)                                                      
where rownum <= 100; 

查看sql执行统计信息


SELECT INST_ID,
       sql_text,
       address,
       HASH_VALUE,
       sql_id,
       CHILD_NUMBER,
       CPU_TIME,
       ELAPSED_TIME,
       FIRST_LOAD_TIME,
       LAST_LOAD_TIME,
       last_active_time,
       SHARABLE_MEM,
       SORTS,
       EXECUTIONS,
       DISK_READS,
       BUFFER_GETS,
       CONCURRENCY_WAIT_TIME,
       CLUSTER_WAIT_TIME,
       USER_IO_WAIT_TIME,
       sql_fulltext
  FROM gv$sql
 WHERE sql_id = 'abanhaqjghq3h';

查看某个sql的内存中的执行计划

set linesize 200
select * from table(DBMS_XPLAN.DISPLAY_CURSOR('abanhaqjghq3h',1));  

11g,flush某个sql的执行计划

--exec sys.dbms_shared_pool.purge('ADDRESS,HASH_VALUE','c');
exec sys.dbms_shared_pool.purge('C000000F75648040,2734184560','c');
exec sys.dbms_shared_pool.purge('C000000F73273450,426993161','c');
exec sys.dbms_shared_pool.purge('C000000A73C19CF8,2734184560','c');
exec sys.dbms_shared_pool.purge('C000000F75648040,2734184560','c');
目录
相关文章
|
2月前
|
存储 SQL 关系型数据库
【MySQL调优】如何进行MySQL调优?从参数、数据建模、索引、SQL语句等方向,三万字详细解读MySQL的性能优化方案(2024版)
MySQL调优主要分为三个步骤:监控报警、排查慢SQL、MySQL调优。 排查慢SQL:开启慢查询日志 、找出最慢的几条SQL、分析查询计划 。 MySQL调优: 基础优化:缓存优化、硬件优化、参数优化、定期清理垃圾、使用合适的存储引擎、读写分离、分库分表; 表设计优化:数据类型优化、冷热数据分表等。 索引优化:考虑索引失效的11个场景、遵循索引设计原则、连接查询优化、排序优化、深分页查询优化、覆盖索引、索引下推、用普通索引等。 SQL优化。
536 15
【MySQL调优】如何进行MySQL调优?从参数、数据建模、索引、SQL语句等方向,三万字详细解读MySQL的性能优化方案(2024版)
|
1月前
|
SQL 监控 Oracle
Oracle SQL性能优化全面指南
在数据库管理领域,Oracle SQL性能优化是确保数据库高效运行和数据查询速度的关键
|
1月前
|
SQL 存储 Oracle
Oracle数据库SQL语句详解与应用指南
在数字化时代,数据库已成为各类企业和组织不可或缺的核心组件。Oracle数据库作为业界领先的数据库管理系统之一,广泛应用于各种业务场景。掌握Oracle数据库的SQL语句是数据库管理员、开发人员及运维人员的基本技能。本文将详细介绍Oracle数据库SQL语句的基本概念、语法、应用及最佳实践。一、Or
52 3
|
1月前
|
SQL Oracle 关系型数据库
Oracle SQL:了解执行计划和性能调优
Oracle SQL:了解执行计划和性能调优
51 1
|
2月前
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
378 2
|
2月前
|
SQL 分布式计算 数据库
SQL调优总结
数据库表的规范化和反规范化设计,设计合适的字段数据类型……
44 8
|
3月前
|
Java XML Maven
跨越时代的飞跃:Struts 2 升级秘籍——从旧版本无缝迁移到最新版,焕发应用新生!
【8月更文挑战第31天】随着软件技术的发展,Struts 2 框架也在不断更新。本文通过具体案例指导开发者如何从旧版平滑升级到 Struts 2.6.x。首先更新 `pom.xml` 中的依赖版本,并执行 `mvn clean install`。接着检查 `struts.xml` 配置,确保符合新版本要求,调整包扫描器等设置。审查 Action 类及其注解,检查配置文件中的弃用项及插件。更新自定义拦截器实现,并验证日志配置。最后,通过一系列测试确保升级后的系统正常运行。通过这些步骤,可以顺利完成 Struts 2 的版本升级,提升应用的安全性和性能。
202 0
|
3月前
|
Java 开发者 前端开发
Struts 2、Spring MVC、Play Framework 上演巅峰之战,Web 开发的未来何去何从?
【8月更文挑战第31天】在Web应用开发中,Struts 2框架因强大功能和灵活配置备受青睐,但开发者常遇配置错误、类型转换失败、标签属性设置不当及异常处理等问题。本文通过实例解析常见难题与解决方案,如配置文件中遗漏`result`元素致页面跳转失败、日期格式不匹配需自定义转换器、`&lt;s:checkbox&gt;`标签缺少`label`属性致显示不全及Action中未捕获异常影响用户体验等,助您有效应对挑战。
88 0
|
3月前
|
SQL 监控 关系型数据库
SQL性能监控与调优工具的神奇之处:如何用最佳实践选择最适合你的那一个,让你的数据库飞起来?
【8月更文挑战第31天】在现代软件开发中,数据库性能监控与调优对应用稳定性至关重要。本文对比了数据库内置工具、第三方工具及云服务工具等几种常用SQL性能监控与调优工具,并通过示例代码展示了如何利用MySQL的EXPLAIN功能分析查询性能。选择最适合的工具需综合考虑功能需求、数据库类型及成本预算等因素。遵循了解工具功能、试用工具及定期维护工具等最佳实践,可帮助开发者更高效地管理和优化数据库性能,迎接未来软件开发中的挑战与机遇。
53 0
|
3月前
|
SQL 关系型数据库 MySQL
SQL性能调优的神奇之处:如何用优化技巧让你的数据库查询飞起来,实现秒级响应?
【8月更文挑战第31天】在现代软件开发中,数据库性能至关重要。本文通过一个实战案例,展示了从慢查询到秒级响应的全过程。通过对查询的详细分析与优化,包括创建索引、改进查询语句及数据类型选择等措施,最终显著提升了性能。文章还提供了示例代码及最佳实践建议,帮助读者掌握SQL性能调优的核心技巧。
210 0

推荐镜像

更多