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 Java
使用java在未知表字段情况下通过sql查询信息
使用java在未知表字段情况下通过sql查询信息
42 8
|
2月前
|
SQL 监控 关系型数据库
SQL语句当前及历史信息查询-performance schema的使用
本文介绍了如何使用MySQL的Performance Schema来获取SQL语句的当前和历史执行信息。Performance Schema默认在MySQL 8.0中启用,可以通过查询相关表来获取详细的SQL执行信息,包括当前执行的SQL、历史执行记录和统计汇总信息,从而快速定位和解决性能瓶颈。
|
3月前
|
SQL 存储 数据可视化
手机短信SQL分析技巧与方法
在手机短信应用中,SQL分析扮演着至关重要的角色
|
3月前
|
SQL 监控 Oracle
Oracle SQL性能优化全面指南
在数据库管理领域,Oracle SQL性能优化是确保数据库高效运行和数据查询速度的关键
|
3月前
|
SQL 存储 Oracle
Oracle数据库SQL语句详解与应用指南
在数字化时代,数据库已成为各类企业和组织不可或缺的核心组件。Oracle数据库作为业界领先的数据库管理系统之一,广泛应用于各种业务场景。掌握Oracle数据库的SQL语句是数据库管理员、开发人员及运维人员的基本技能。本文将详细介绍Oracle数据库SQL语句的基本概念、语法、应用及最佳实践。一、Or
102 3
|
3月前
|
SQL Oracle 关系型数据库
Oracle SQL:了解执行计划和性能调优
Oracle SQL:了解执行计划和性能调优
94 1
|
4月前
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
783 2
|
4月前
|
SQL Java
使用java在未知表字段情况下通过sql查询信息
使用java在未知表字段情况下通过sql查询信息
42 1
|
5月前
|
前端开发 Java JSON
Struts 2携手AngularJS与React:探索企业级后端与现代前端框架的完美融合之道
【8月更文挑战第31天】随着Web应用复杂性的提升,前端技术日新月异。AngularJS和React作为主流前端框架,凭借强大的数据绑定和组件化能力,显著提升了开发动态及交互式Web应用的效率。同时,Struts 2 以其出色的性能和丰富的功能,成为众多Java开发者构建企业级应用的首选后端框架。本文探讨了如何将 Struts 2 与 AngularJS 和 React 整合,以充分发挥前后端各自优势,构建更强大、灵活的 Web 应用。
69 0
|
5月前
|
SQL 数据采集 数据挖掘
为什么要使用 SQL 函数?详尽分析
【8月更文挑战第31天】
70 0

推荐镜像

更多