Show a Count of Analyzed Tables by Schema in Oracle Database
Show a Count of Analyzed Tables Broken Down by Schema in Oracle Database
Purpose:
This post explains how to use Oracle SQL to report the total number of tables and the number of analyzed tables for each schema in your Oracle Database. This is crucial for database administrators who want to monitor the health and performance of their database, ensure accurate statistics for the optimizer, and identify schemas where statistics may be outdated or missing.
SQL Code
1set pages 999 lines 100
2select a.owner
3, a.total_tables tables
4, nvl(b.analyzed_tables,0) analyzed
5from (select owner
6 , count(*) total_tables
7 from dba_tables
8 group by owner) a
9, (select owner
10 , count(last_analyzed) analyzed_tables
11 from dba_tables
12 where last_analyzed is not null
13 group by owner) b
14where a.owner = b.owner (+)
15and a.owner not in ('SYS', 'SYSTEM')
16order by a.total_tables - nvl(b.analyzed_tables,0) desc
17/
Breakdown and Explanation
dba_tables
: This data dictionary view contains metadata for all tables in the database, including their owner and when they were last analyzed.owner
: The schema name.total_tables
: The total number of tables owned by each schema.analyzed_tables
: The number of tables in each schema that have been analyzed (i.e., have non-nullLAST_ANALYZED
).nvl(b.analyzed_tables,0)
: Ensures schemas with zero analyzed tables show as0
rather thanNULL
.a.owner not in ('SYS', 'SYSTEM')
: Excludes internal Oracle schemas from the report.order by a.total_tables - nvl(b.analyzed_tables,0) desc
: Sorts results to highlight schemas with the most unanalyzed tables.
Key Points and Insights
Why Analyze Tables?
Oracle's query optimizer relies on up-to-date statistics to generate efficient execution plans. If tables are not analyzed, or their statistics are stale, query performance can degrade significantlyWhat Does
LAST_ANALYZED
Mean?
TheLAST_ANALYZED
column inDBA_TABLES
records when statistics were last gathered for each table. If it isNULL
, the table has never been analyzed or statistics have been deletedDifference Between Table Count and Analyzed Table Count:
- Total tables shows all tables in a schema.
- Analyzed tables shows only those with statistics.
A large gap indicates tables that may need to be analyzed to ensure optimal performance.
Why Exclude SYS and SYSTEM?
These are Oracle's internal schemas; their tables are managed by Oracle and generally not relevant to user/application performance tuning.
Best Practices
- Regularly Gather Statistics:
Schedule regular jobs usingDBMS_STATS
to keep statistics current, especially after large data loads or structural changes - Monitor for Unanalyzed Tables:
Use this report to identify schemas needing attention, and prioritize analyzing tables with missing or outdated statistics. - Understand the Impact:
Relying on dictionary statistics (NUM_ROWS
inDBA_TABLES
) is fast but may be outdated; for mission-critical queries, consider runningANALYZE
orDBMS_STATS
to refresh statisticsa
References
- For more on the impact of table statistics vs. direct row counts, see [Finding Row Counts for All Tables in Oracle Database](https://dincosman.com/2024/12/07/find-row-counts-oracle/).
- To check for stale statistics and last analyzed dates, see [How to find Stale object and last analyzed for tables/indexes](https://asrblogger.com/how-to-find-stale-object-and-last-analyzed-for-tables-indexes/).
- For best practices on stats gathering, see [Your Ultimate Guide to Effective Stats Gathering in Oracle](https://www.suretysystems.com/insights/your-ultimate-guide-to-effective-stats-gathering-in-oracle/).
Keep your Oracle Database running efficiently by ensuring all tables are regularly analyzed and statistics are up to date.