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
目录
相关文章
|
2月前
|
存储 SQL 关系型数据库
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
|
18天前
|
SQL Web App开发 安全
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
49 0
SQL Server 2025 年 9 月更新 - 修复 CVE-2025-47997 SQL Server 信息泄露漏洞
|
5月前
|
SQL 存储 关系型数据库
SQL优化策略与实践:组合索引与最左前缀原则详解
本文介绍了SQL优化的多种方式,包括优化查询语句(避免使用SELECT *、减少数据处理量)、使用索引(创建合适索引类型)、查询缓存、优化表结构、使用存储过程和触发器、批量处理以及分析和监控数据库性能。同时,文章详细讲解了组合索引的概念及其最左前缀原则,即MySQL从索引的最左列开始匹配条件,若跳过最左列,则索引失效。通过示例代码,展示了如何在实际场景中应用这些优化策略,以提高数据库查询效率和系统响应速度。
184 10
|
2月前
|
SQL Web App开发 安全
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
205 0
SQL Server 2025年7月更新 - 修复 CVE-2025-49718 Microsoft SQL Server 信息泄露漏洞
|
5月前
|
SQL Oracle 关系型数据库
解决大小写、保留字与特殊字符问题!Oracle双引号在SQL中的特殊应用
在Oracle数据库开发中,双引号的使用是一个重要但易被忽视的细节。本文全面解析了双引号在SQL中的特殊应用场景,包括解决标识符与保留字冲突、强制保留大小写、支持特殊字符和数字开头标识符等。同时提供了最佳实践建议,帮助开发者规避常见错误,提高代码可维护性和效率。
218 6
|
6月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法
|
6月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】yashandb执行包含带oracle dblink表的sql时性能差
【YashanDB知识库】yashandb执行包含带oracle dblink表的sql时性能差
|
6月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法
本文来自YashanDB官网,介绍如何处理Oracle客户端sql*plus中使用@@调用同级目录SQL脚本的场景。崖山数据库23.2.x.100已支持@@用法,但旧版本可通过Python脚本批量重写SQL文件,将@@替换为绝对路径。文章通过Oracle示例展示了具体用法,并提供Python脚本实现自动化处理,最后调整批处理脚本以适配YashanDB运行环境。
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
434 13

推荐镜像

更多