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 存储 数据库
sql事务、视图和索引
sql事务、视图和索引
14 0
|
1月前
|
SQL 存储 弹性计算
GaussDB SQL调优:建立合适的索引
GaussDB SQL调优:建立合适的索引
12 0
|
2月前
|
SQL 索引
SQL索引小结
SQL索引小结
18 0
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
99 1
|
1月前
|
存储 关系型数据库 MySQL
最全MySQL面试60题(含答案):存储引擎+数据库锁+索引+SQL优化等
最全MySQL面试60题(含答案):存储引擎+数据库锁+索引+SQL优化等
158 0
|
17天前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
13 0
|
1月前
|
SQL
现有用户成就统计需求,每个用户有多个成就,某一个成就会被多人拥有,写出数据表设计方案,用一条sql查出每个成就(B.ach_name)下的男生(sex=0)和女生(sex=1)分别有多少?
现有用户成就统计需求,每个用户有多个成就,某一个成就会被多人拥有,写出数据表设计方案,用一条sql查出每个成就(B.ach_name)下的男生(sex=0)和女生(sex=1)分别有多少?
41 0
|
3月前
|
SQL Oracle 关系型数据库
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
87 0
|
9天前
|
SQL 数据库 索引
SQL索引失效原因分析与解决方案
SQL索引失效原因分析与解决方案
18 0
|
1月前
|
SQL 关系型数据库 MySQL
[MySQL]SQL优化之索引的使用规则
[MySQL]SQL优化之索引的使用规则

推荐镜像

更多