首页 > 数据库 >给定SQL_ID,查看访问的表以及其统计信息

给定SQL_ID,查看访问的表以及其统计信息

时间:2023-11-19 22:24:25浏览次数:43  
标签:partition name SQL diag object 给定 sql ID pack


-- https://github.com/jkstill/oracle-script-lib/blob/master/sql/stats-sqlid.sql
-- stats-sqlid.sql
-- get stats info for all objects used in a SQL_ID
-- Jared Still -  2017
--  [email protected]
--
-- the plan_hash_values are shown as an aggregate.
-- this may be misleading, as the object on that line may not actually appear in that plan.
-- I chose succinctness with a slight loss of accuracy
-- as this report becomes much busier and hard to read if PHV is matched exactly
--
-- initial testing with 12.2 SYS SQL_ID cyw0c6qyrvsdd
-- 
-- call with SQL_ID and status of Diagnostic Pack licensing
-- do not use AWR
--    @stats-sqlid.sql cyw0c6qyrvsdd N
-- use AWR
--    @stats-sqlid.sql cyw0c6qyrvsdd Y


set pause off
set echo off
set timing off
set trimspool on
set feed on
set term on
set verify off
set linesize 200
set pagesize 0
set head on

clear col
clear break
clear computes

btitle ''
ttitle ''

btitle off
ttitle off

set newpage 1

set tab off


ttitle off
btitle off

col s_diag_pack new_value s_diag_pack noprint
col s_sql_id new_value s_sql_id noprint
var v_sql_id varchar2(13)

prompt
prompt SQL_ID? : 
prompt

set feed off term off 
select '&1' s_sql_id from dual;

set term on

whenever sqlerror exit 128

begin 
	:v_sql_id := '&s_sql_id';
	if 
		length(:v_sql_id) < 1
		or 
		:v_sql_id is null
	then
		raise value_error;
	end if;
end;
/

whenever sqlerror continue
set feed on

prompt
prompt Diag Pack (Y/N)? : 
prompt

set feed off term off 
select decode(upper('&2'),'Y','','--') s_diag_pack from dual;
set feed on term on


set pagesize 100
set linesize 300 trimspool on

col partition_start format a6 head 'PSTART'
col sql_id format a13
col partition_stop format a6 head 'PSTOP'
col owner format a20
col table_name format a30
col index_name format a40
col phv format a43 wrap
col last_analyzed format a19
col stale_stats format a3 head 'STL'
col num_rows format 99,999,999,999
col blocks format 9,99,999,999
col partition_position format 999999 head 'PP'

break on sql_id skip 1

spool stats-sqlid.txt

with objects as (
	-- extra inline view is to eliminate duplicates in listagg()
	select 
		sql_id
		, listagg(phv,',') within group(order by phv)  phv
		, object_owner
		, object_name
		, object_type
		, partition_start
		, partition_stop
	from (
		select distinct
			sql_id
			, phv
			, object_owner
			, object_name
			, object_type
			, partition_start
			, partition_stop
		from (
			select 
				sql_id
				, plan_hash_value phv
				, object_owner
				, object_name
				, object_type
				, case partition_start
					when 'ROW LOCATION' then 'ROWID'
					else partition_start
				end partition_start
				, case partition_stop
					when 'ROW LOCATION' then 'ROWID'
					else partition_stop
				end partition_stop
			from v$sql_plan
			where sql_id = :v_sql_id
			and object_owner is not null
			and object_type in ('TABLE','INDEX','INDEX (UNIQUE)','INDEX (CLUSTER)','CLUSTER','TABLE (FIXED)')
			&s_diag_pack union all
			&s_diag_pack select 
				&s_diag_pack sql_id
				&s_diag_pack , plan_hash_value phv
				&s_diag_pack , object_owner
				&s_diag_pack , object_name
				&s_diag_pack , object_type
				&s_diag_pack , case partition_start
					&s_diag_pack when 'ROW LOCATION' then 'ROWID'
					&s_diag_pack else partition_start
				&s_diag_pack end partition_start
				&s_diag_pack , case partition_stop
					&s_diag_pack when 'ROW LOCATION' then 'ROWID'
					&s_diag_pack else partition_stop
				&s_diag_pack end partition_stop
			&s_diag_pack from dba_hist_sql_plan
			&s_diag_pack where sql_id = :v_sql_id
			&s_diag_pack and object_owner is not null
			&s_diag_pack and object_type in ('TABLE','INDEX','INDEX (UNIQUE)','INDEX (CLUSTER)','CLUSTER','TABLE (FIXED)')
		)
	)
	group by
		sql_id
		, object_owner
		, object_name
		, object_type
		, partition_start
		, partition_stop
),
indexes as (
	select * from objects where object_type in ('INDEX','INDEX (UNIQUE)','INDEX (CLUSTER)')
),
tables as (
	select * from objects where object_type in ('TABLE','CLUSTER','TABLE (FIXED)')
)
select 
	sql_id
	, phv
	, owner
	, table_name
		|| decode(s.partition_name, null,'','.' || s.partition_name)
		as table_name
	, null index_name
	, partition_position
	, t.partition_start
	, t.partition_stop
	, num_rows
	, blocks
	, to_char(last_analyzed,'yyyy-mm-dd hh24:mi:ss') last_analyzed
	, stale_stats
from dba_tab_statistics s
 join tables t on t.object_owner = s.owner
	and t.object_name = s.table_name
union all
select 
	sql_id
	, phv
	, owner
	, table_name 
	, index_name
		|| decode(s.partition_name, null,'','.' || s.partition_name)
		as index_name
	, partition_position
	, i.partition_start
	, i.partition_stop
	, num_rows
	, leaf_blocks blocks
	, to_char(last_analyzed,'yyyy-mm-dd hh24:mi:ss') last_analyzed
	, stale_stats
from dba_ind_statistics s
 join indexes i on i.object_owner = s.owner
	and i.object_name = s.index_name
order by sql_id
	, owner
	, table_name
	, index_name nulls first
	, partition_position nulls first
/

spool off

ed stats-sqlid.txt

undef 1 2

标签:partition,name,SQL,diag,object,给定,sql,ID,pack
From: https://www.cnblogs.com/Jeona/p/17842812.html

相关文章

  • GUI-Guider 生成打印机模板并在 ESP32-S3 上面运行
    原文:https://www.jianshu.com/p/51fc4c1d1e66目录目录ESP32-S3移植GUI-Guider的打印机例程前提准备1.GUIGuider生成工程根据屏幕参数新建工程2.移植代码到lvgl例程里将生成的代码作为组件使用与参考链接中的不同调用生成的代码ESP32-S3移植GUI-Guid......
  • mysql巡检脚本sql补充
    --没有主键索引mysql[localhost:8028]{root}(test)>SELECTt.table_schema,t.table_name,t.engine->FROMinformation_schema.tablest->JOINinformation_schema.columnsc->ONt.table_schema=c.table_schema->ANDt.table_name=c.......
  • INFINI Labs 产品更新 | 发布 Easysearch Java 客户端,Console 支持 SQL 查询等功能
    近年来,日志管理平台越来越流行。使用日志管理平台可以实时地、统一地、方便地管理和查看日志,挖掘日志数据价值,驱动运维、运营,提升服务管理效率。方案架构Beats是轻量级采集器,包括Filebeat、Metricbeat等。Easysearch是个分布式搜索引擎,提供搜集、分析、存储数据等主要功能。Con......
  • INFINI Labs 产品更新 | 发布 Easysearch Java 客户端,Console 支持 SQL 查询等功能
    近年来,日志管理平台越来越流行。使用日志管理平台可以实时地、统一地、方便地管理和查看日志,挖掘日志数据价值,驱动运维、运营,提升服务管理效率。方案架构Beats是轻量级采集器,包括Filebeat、Metricbeat等。Easysearch是个分布式搜索引擎,提供搜集、分析、存储数据等主要功......
  • 查看SQLServer平均最耗资源时间的SQL语句
    SELECT(total_elapsed_time/execution_count)/1000N'平均时间ms',total_elapsed_time/1000N'总花费时间ms',total_worker_time/1000N'所用的CPU总时间ms',total_physical_reads......
  • jsoup根据id 选择器来获取具有特定ID的元素
      importorg.jsoup.Jsoup;importorg.jsoup.nodes.Document;importorg.jsoup.nodes.Element;publicclassMain{publicstaticvoidmain(String[]args){Stringhtml="<html><body><divid='myId'>我是ID为myId的di......
  • Linux 系统systemd(pid=1)占用80端口导致web程序无法启动
    注意务必组好快照备份后再操作。 做好备份后将/usr/lib/systemd/system内http相关的配置文件重命名后重启主机恢复正常。......
  • 从0写Android:一 安装&运行
    基本信息1.操作系统:windows2.功能:模拟操作其它手机软件3.本次完成:安装到运行第一个模板工程安装&启动1.下载地址:https://developer.android.google.cn/studio?hl=zh-cn下载下来直接无脑安装即可2.打开软件如果出现add-onlist报错,直接点击cancel即可,不要配置如果不......
  • Linux安装MySQL
    本文使用的Linux发行版本为AlmaLinux9.264位(CentOS停止更新后的完美替代发行版本)。本文安装的MySQL版本为8.1.0,其他版本方法类似。MySQL源码编译时间太长了,需要3到4小时,使用官网编译好的rpm更简单快捷。操作步骤更新系统。dnf-yupdate查看是否已经有安装了的mysql......
  • 从 Mysql 架构上如何查询 sql 的执行过程?
    MySQL是一种流行的关系型数据库管理系统(RDBMS),由瑞典MySQLAB公司开发,目前属于甲骨文公司(Oracle)旗下。MySQL是基于SQL语言的一个开源数据库,可以用于存储、管理、检索数据。它支持大量的并发用户连接,并且提供了灵活的数据类型和索引机制。MySQL有多个版本,包括社区版和商业版,提供了不......