2026-07-11 16:10:12
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
-- ============================================================================
|
||||
-- awrsqlidtabstat2.sql
|
||||
-- ----------------------------------------------------------------------------
|
||||
-- Reference:
|
||||
-- https://blogs.oracle.com/optimizer/post/check-sql-stale-statistics
|
||||
-- Updated in 2026 july from Grand Canaria ;)
|
||||
-- ============================================================================
|
||||
|
||||
SET PAGESIZE 100
|
||||
SET LINESIZE 150
|
||||
SET TRIMS OFF
|
||||
SET TAB OFF
|
||||
SET VERIFY OFF
|
||||
|
||||
COLUMN table_name FORMAT a25
|
||||
COLUMN index_name FORMAT a25
|
||||
COLUMN object_type FORMAT a20
|
||||
COLUMN owner FORMAT a25
|
||||
COLUMN object_owner FORMAT a25
|
||||
COLUMN object_name FORMAT a25
|
||||
COLUMN stale_stats FORMAT a30
|
||||
COLUMN num_rows FORMAT 999,999,999,999
|
||||
COLUMN visibility FORMAT a9
|
||||
COLUMN status FORMAT a8
|
||||
|
||||
PROMPT
|
||||
PROMPT ==================================
|
||||
PROMPT Tables with Statistics Information
|
||||
PROMPT ==================================
|
||||
PROMPT
|
||||
|
||||
WITH plan_tables AS (
|
||||
SELECT DISTINCT
|
||||
object_name,
|
||||
object_owner,
|
||||
object_type
|
||||
FROM dba_hist_sql_plan
|
||||
WHERE object_type LIKE 'TABLE%'
|
||||
AND sql_id = '&1'
|
||||
AND plan_hash_value = '&2'
|
||||
)
|
||||
SELECT
|
||||
t.object_owner AS owner,
|
||||
t.object_name AS table_name,
|
||||
t.object_type AS object_type,
|
||||
tab.num_rows,
|
||||
DECODE(
|
||||
stale_stats,
|
||||
'NO', 'OK',
|
||||
NULL, 'NO STATS!',
|
||||
'STALE!'
|
||||
) AS staleness
|
||||
FROM dba_tab_statistics s
|
||||
JOIN plan_tables t
|
||||
ON s.table_name = t.object_name
|
||||
AND s.owner = t.object_owner
|
||||
JOIN dba_tables tab
|
||||
ON t.object_owner = tab.owner
|
||||
AND t.object_name = tab.table_name
|
||||
WHERE s.partition_name IS NULL
|
||||
AND s.subpartition_name IS NULL
|
||||
ORDER BY t.object_owner, t.object_name;
|
||||
|
||||
|
||||
PROMPT
|
||||
PROMPT ===================================
|
||||
PROMPT Indexes with Statistics Information
|
||||
PROMPT ===================================
|
||||
PROMPT
|
||||
|
||||
WITH plan_indexes AS (
|
||||
SELECT DISTINCT
|
||||
object_name,
|
||||
object_owner,
|
||||
object_type
|
||||
FROM dba_hist_sql_plan
|
||||
WHERE object_type LIKE 'INDEX%'
|
||||
AND sql_id = '&1'
|
||||
AND plan_hash_value = '&2'
|
||||
)
|
||||
SELECT
|
||||
i.object_owner AS owner,
|
||||
i.object_name AS index_name,
|
||||
i.object_type AS object_type,
|
||||
idx.status,
|
||||
idx.num_rows,
|
||||
idx.visibility,
|
||||
DECODE(
|
||||
stale_stats,
|
||||
'NO', 'OK',
|
||||
NULL, 'NO STATS',
|
||||
'STALE!'
|
||||
) AS staleness
|
||||
FROM dba_ind_statistics s
|
||||
JOIN plan_indexes i
|
||||
ON s.index_name = i.object_name
|
||||
AND s.owner = i.object_owner
|
||||
JOIN dba_indexes idx
|
||||
ON s.owner = idx.owner
|
||||
AND s.index_name = idx.index_name
|
||||
WHERE s.partition_name IS NULL
|
||||
AND s.subpartition_name IS NULL
|
||||
ORDER BY i.object_owner, i.object_name;
|
||||
|
||||
|
||||
PROMPT
|
||||
PROMPT ===============
|
||||
PROMPT Missing Indexes
|
||||
PROMPT ===============
|
||||
PROMPT
|
||||
|
||||
WITH plan_indexes AS (
|
||||
SELECT DISTINCT
|
||||
object_name,
|
||||
object_owner,
|
||||
object_type
|
||||
FROM dba_hist_sql_plan
|
||||
WHERE object_type LIKE 'INDEX%'
|
||||
AND sql_id = '&1'
|
||||
AND plan_hash_value = '&2'
|
||||
)
|
||||
SELECT
|
||||
object_owner,
|
||||
object_name
|
||||
FROM plan_indexes
|
||||
WHERE object_type = 'INDEX'
|
||||
MINUS
|
||||
SELECT
|
||||
owner,
|
||||
index_name
|
||||
FROM dba_indexes;
|
||||
Reference in New Issue
Block a user