oracle-sql-根据sql_id查看sql中所涉及的表和索引的统计信息

简介:

根据sql_id查看所涉及的表和索引的统计信息

REM col child_number new_value child_number noprint 
REM /* and p.child_number = nvl(&child_number,0) */

PROMPT 
PROMPT 查询SQL_ID对应的表的统计信息
set define on verify off feedback on
define sql_id=&sql_id
col sql_id new_value sql_id noprint

set line 200
set pagesize 30
col owner for a10
col table_name for a30
col num_rows for 9999999999
col blocks for 9999999999
col empty_blocks for 99999999
col avg_row_len for 99999
col est_M for 999999
col partitioned for a3
col degree for a10
col last_analyzed for a22

with tmp_tab as 
(select distinct s.owner,s.table_name from dba_tables s, gv$sql_plan g
where s.owner=g.object_owner and s.table_name=g.object_name
and g.sql_id =trim('&sql_id') and (g.object_type like 'TABLE%' OR g.object_type like '%VIEW%')
union
select distinct i.table_owner owner,i.table_name table_name from dba_indexes i , gv$sql_plan g
where i.owner=g.object_owner and i.index_name=g.object_name
and g.sql_id = trim('&sql_id') and g.object_type like 'INDEX%')
select s.owner,s.table_name,s.num_rows,s.blocks,s.empty_blocks,s.avg_row_len,round(s.num_rows * s.avg_row_len / 1024 /1024 / 0.9) est_M,s.partitioned,s.degree,to_char(s.last_analyzed,'yyyymmdd hh24:mi:ss') as last_analyzed
from tmp_tab t,dba_tables s
where t.owner=s.owner and t.table_name=s.table_name
order by s.owner,s.table_name,s.num_rows desc;

PROMPT
PROMPT 查询SQL_ID的对应的表的列统计信息
set line 200
set pagesize 30
col owner for a10
col table_name for a30
col index_name for a30
col column_name for a24
col num_distinct for 9999999999
col num_nulls for 999999999
col histogram for a15
col last_analyzed for a22
col column_position for 99

with tmp_col as 
  (select /*+ rule */ distinct s.owner,s.table_name,s.column_name,s.num_distinct,s.num_nulls,s.histogram,s.last_analyzed 
from dba_tab_col_statistics s,gv$sql_plan g
where s.table_name = g.object_name and s.owner = g.object_owner
and g.sql_id =trim('&sql_id')
union
  select /*+ rule */ distinct s.owner,s.table_name,s.column_name,s.num_distinct,s.num_nulls,s.histogram,s.last_analyzed 
from dba_tab_col_statistics s 
where (s.owner ,s.table_name) in 
  (select i.table_owner,i.table_name 
     from dba_indexes i,gv$sql_plan g 
     where i.index_name = g.object_name and i.owner = g.object_owner
     and g.sql_id =trim('&sql_id')))
select distinct t.owner,t.table_name,t.column_name,t.num_distinct,t.num_nulls,t.histogram,t.last_analyzed,i.index_name,i.column_position
from tmp_col t,gv$sql_plan g,dba_ind_columns i
where g.sql_id =trim('&sql_id') and g.child_number = (select max(child_number) from gv$sql_plan p where p.sql_id =trim('&sql_id'))
and (instr(g.access_predicates,to_char('"'||t.column_name||'"')) > 0 or instr(g.filter_predicates,to_char('"'||t.column_name||'"'))>0)
and t.owner = i.table_owner(+) and t.table_name = i.table_name(+) and t.column_name = i.column_name(+)
order by t.owner,t.table_name,t.num_distinct desc;

PROMPT
PROMPT 查询SQL_ID对应的表上的索引信息
col table_owner for a10
col table_name for a30
col index_name for a30
col column_name for a30
col column_position for 99

with tmp_idx as
  (select distinct s.owner,s.table_name from dba_tables s, gv$sql_plan g
  where s.owner=g.object_owner and s.table_name=g.object_name
  and g.sql_id =trim('&sql_id') and (g.object_type like 'TABLE%' OR g.object_type like '%VIEW%')
union
  select distinct i.table_owner owner,i.table_name table_name from dba_indexes i , gv$sql_plan g
  where i.owner=g.object_owner and i.index_name=g.object_name
  and g.sql_id = trim('&sql_id') and g.object_type like 'INDEX%')
select i.table_owner,i.table_name,i.index_name,i.column_name,i.column_position
from tmp_idx t,dba_ind_columns i
where t.owner=i.table_owner and t.table_name=i.table_name
order by i.table_owner,i.table_name,i.index_name,i.column_position;


REM undefine sql_id
undefine sql_id
目录
相关文章
|
27天前
|
SQL Java
使用java在未知表字段情况下通过sql查询信息
使用java在未知表字段情况下通过sql查询信息
36 8
|
1月前
|
SQL Oracle 关系型数据库
[Oracle]索引
本文介绍了数据库索引的基本概念、优化查询的原理及分类。索引是一种数据结构(如B树或B+树),通过排序后的`rowid`来优化查询性能。文章详细解释了索引的构建过程、B+树的特点及其优势,并介绍了五种常见的索引类型:唯一索引、组合索引、反向键索引、位图索引和基于函数的索引。每种索引都有其适用场景和限制,帮助读者更好地理解和应用索引技术。
65 1
[Oracle]索引
|
1月前
|
SQL 监控 关系型数据库
SQL语句当前及历史信息查询-performance schema的使用
本文介绍了如何使用MySQL的Performance Schema来获取SQL语句的当前和历史执行信息。Performance Schema默认在MySQL 8.0中启用,可以通过查询相关表来获取详细的SQL执行信息,包括当前执行的SQL、历史执行记录和统计汇总信息,从而快速定位和解决性能瓶颈。
|
1月前
|
SQL
开启慢SQL设置long_query_time=0.1为啥会统计的sql却存在小于100毫秒的sql
开启慢SQL设置long_query_time=0.1为啥会统计的sql却存在小于100毫秒的sql
34 1
|
2月前
|
SQL 监控 Oracle
Oracle SQL性能优化全面指南
在数据库管理领域,Oracle SQL性能优化是确保数据库高效运行和数据查询速度的关键
|
2月前
|
SQL 存储 Oracle
Oracle数据库SQL语句详解与应用指南
在数字化时代,数据库已成为各类企业和组织不可或缺的核心组件。Oracle数据库作为业界领先的数据库管理系统之一,广泛应用于各种业务场景。掌握Oracle数据库的SQL语句是数据库管理员、开发人员及运维人员的基本技能。本文将详细介绍Oracle数据库SQL语句的基本概念、语法、应用及最佳实践。一、Or
86 3
|
2月前
|
SQL Oracle 关系型数据库
Oracle SQL:了解执行计划和性能调优
Oracle SQL:了解执行计划和性能调优
72 1
|
3月前
|
SQL Java
使用java在未知表字段情况下通过sql查询信息
使用java在未知表字段情况下通过sql查询信息
38 1
|
2月前
|
SQL 存储 关系型数据库
mysql 数据库空间统计sql
mysql 数据库空间统计sql
50 0
|
4月前
|
SQL 流计算
Flink SQL 在快手实践问题之由于meta信息变化导致的state向前兼容问题如何解决
Flink SQL 在快手实践问题之由于meta信息变化导致的state向前兼容问题如何解决
52 1

推荐镜像

更多