Tables with Zero Rows but Allocated Blocks (dstatzero.sql)
What This Script Does
Section titled “What This Script Does”This script provides Oracle database administration functionality via the dstatzero.sql script.
The Script
Section titled “The Script”rem dstatzero.sqlremttitle 'Table Statistics - No Rows'remcol owner format a8 heading 'OWNER'col table_name format a26 heading 'TABLE NAME'col num_rows format 999999990 heading 'ROWS'col chain_cnt format 9990 heading 'CHAIN|COUNT'col avg_row_len format 9990 heading 'AVG|ROW|SIZE'col blocks format 999990 heading 'USED|BLOCKS'col empty_blocks format 999990 heading 'EMPTY|BLOCKS'col avg_space format 9990 heading 'AVG|FREE|SPACE'rembreak on report on owner skip 1compute sum of num_rows blocks empty_blocks on report ownerremselect owner, table_name, num_rows, chain_cnt, avg_row_len, blocks, empty_blocks, avg_space from sys.dba_tables where owner like upper('&owner') and table_name like upper('&table') and blocks <> 0 and num_rows = 0 order by owner, table_name;
What This Script Does
Section titled “What This Script Does”This script identifies tables that show zero rows in their statistics but have allocated database blocks, which can indicate space waste, stale statistics, or tables that have been truncated but not deallocated. It’s essential for space management and identifying potential storage optimization opportunities.
Key Features
Section titled “Key Features”- Space Anomaly Detection: Finds tables consuming space with zero rows
- Statistics Analysis: Shows various table statistics for comparison
- Storage Metrics: Displays block usage and space information
- Flexible Filtering: Filter by owner and table name patterns
- Summary Reporting: Provides totals grouped by owner
Run the script and provide filter criteria when prompted:
@dstatzero.sql
Input Parameters:
- Owner: Schema owner name or pattern (use % for all owners)
- Table: Table name or pattern (use % for all tables)
Required Privileges
Section titled “Required Privileges”SELECT on SYS.DBA_TABLES
Sample Output
Section titled “Sample Output” Table Statistics - No Rows
OWNER TABLE NAME ROWS CHAIN AVG USED EMPTY AVG COUNT ROW BLOCKS BLOCKS FREE SIZE SPACE-------- -------------------------- ---- ----- ---- ------ ------ ----HR EMPLOYEE_BACKUP 0 0 0 25 0 0 TEMP_EMPLOYEE_DATA 0 0 0 45 0 0 OLD_SALARY_HISTORY 0 0 0 18 0 0 ---- ----- ------ ------sum 0 0 88 0
SALES CUSTOMER_ARCHIVE 0 0 0 156 0 0 MONTHLY_TEMP_DATA 0 0 0 67 0 0 ORDER_STAGING 0 0 0 23 0 0 QUARTERLY_BACKUP 0 0 0 89 0 0 ---- ----- ------ ------sum 0 0 335 0
FINANCE BUDGET_TEMP 0 0 0 12 0 0 RECONCILIATION_STAGE 0 0 0 34 0 0 YEAR_END_BACKUP 0 0 0 78 0 0 ---- ----- ------ ------sum 0 0 124 0
==== ===== ====== ======sum 0 0 547 0
Key Output Columns
Section titled “Key Output Columns”- OWNER: Schema that owns the table
- TABLE NAME: Name of the table
- ROWS: Number of rows according to statistics (always 0 in this report)
- CHAIN COUNT: Number of chained rows
- AVG ROW SIZE: Average row length in bytes
- USED BLOCKS: Number of blocks allocated to the table
- EMPTY BLOCKS: Number of empty blocks in the table
- AVG FREE SPACE: Average free space per block
Understanding Zero-Row Tables with Blocks
Section titled “Understanding Zero-Row Tables with Blocks”Possible Causes
Section titled “Possible Causes”Stale Statistics
Section titled “Stale Statistics”- Outdated information: Statistics not updated after data deletion
- Truncate operations: Table truncated but statistics not refreshed
- Bulk delete: Large delete operations without statistics update
Space Management Issues
Section titled “Space Management Issues”- High Water Mark: Blocks allocated but not deallocated after truncate
- Deferred segment creation: 11g+ feature may show unusual patterns
- Table reorganization: Incomplete reorganization operations
Temporary or Staging Tables
Section titled “Temporary or Staging Tables”- ETL processes: Tables used for data loading and processing
- Backup tables: Tables created for backup purposes
- Temporary storage: Tables used for temporary data processing
Block Allocation Patterns
Section titled “Block Allocation Patterns”Small Block Counts (< 50 blocks)
Section titled “Small Block Counts (< 50 blocks)”- Initial extent: Tables with initial extent allocation
- Minimal impact: Low priority for space reclamation
- Common pattern: Normal for many tables
Large Block Counts (> 100 blocks)
Section titled “Large Block Counts (> 100 blocks)”- Significant space: Potential for meaningful space reclamation
- Investigation needed: Determine if space can be reclaimed
- High priority: Should be addressed for space optimization
Common Use Cases
Section titled “Common Use Cases”-
Space Optimization
- Identify tables consuming unnecessary space
- Find candidates for space reclamation
- Plan storage cleanup activities
-
Statistics Management
- Find tables with stale statistics
- Identify tables needing statistics refresh
- Plan statistics gathering schedules
-
Database Cleanup
- Locate abandoned temporary tables
- Find unused backup tables
- Identify orphaned staging tables
-
Capacity Planning
- Understand wasted space patterns
- Plan for space reclamation
- Optimize storage allocation
Analysis and Resolution
Section titled “Analysis and Resolution”Statistics Refresh
Section titled “Statistics Refresh”-- Update statistics for identified tables:EXEC DBMS_STATS.GATHER_TABLE_STATS('HR', 'EMPLOYEE_BACKUP');EXEC DBMS_STATS.GATHER_TABLE_STATS('SALES', 'CUSTOMER_ARCHIVE');
Space Reclamation
Section titled “Space Reclamation”-- For tables that are truly empty:-- Option 1: Truncate and deallocate spaceTRUNCATE TABLE hr.employee_backup DROP STORAGE;
-- Option 2: Move table to reclaim spaceALTER TABLE sales.customer_archive MOVE;
-- Option 3: Shrink table (if applicable)ALTER TABLE finance.budget_temp ENABLE ROW MOVEMENT;ALTER TABLE finance.budget_temp SHRINK SPACE;
Table Analysis
Section titled “Table Analysis”-- Verify if table is actually empty:SELECT COUNT(*) FROM hr.employee_backup;
-- Check actual space usage:SELECT table_name, num_rows, blocks, empty_blocksFROM dba_tablesWHERE owner = 'HR' AND table_name = 'EMPLOYEE_BACKUP';
Advanced Analysis
Section titled “Advanced Analysis”Actual Row Count Verification
Section titled “Actual Row Count Verification”-- For suspicious tables, verify actual row count:-- This may take time for large tablesSELECT 'HR.EMPLOYEE_BACKUP' table_name, COUNT(*) actual_rowsFROM hr.employee_backupUNION ALLSELECT 'SALES.CUSTOMER_ARCHIVE', COUNT(*)FROM sales.customer_archive;
Space Calculation
Section titled “Space Calculation”-- Calculate potential space savings:SELECT owner, SUM(blocks * 8192) / 1024 / 1024 potential_mb_savingsFROM dba_tablesWHERE blocks > 0 AND num_rows = 0GROUP BY ownerORDER BY potential_mb_savings DESC;
High Water Mark Analysis
Section titled “High Water Mark Analysis”-- Check for high water mark issues:SELECT owner, table_name, blocks, empty_blocks, blocks - NVL(empty_blocks,0) as hwm_blocksFROM dba_tablesWHERE blocks > 0 AND num_rows = 0AND blocks > 50 -- Focus on larger tablesORDER BY hwm_blocks DESC;
Filter Examples
Section titled “Filter Examples”Check Specific Schema
Section titled “Check Specific Schema”Enter value for owner: HREnter value for table: %
Find Large Zero-Row Tables
Section titled “Find Large Zero-Row Tables”-- After running, focus on tables with USED BLOCKS > 100-- These represent the most significant space savings opportunities
Check Temporary Tables
Section titled “Check Temporary Tables”Enter value for owner: %Enter value for table: %TEMP%
Troubleshooting and Investigation
Section titled “Troubleshooting and Investigation”Verify Table Emptiness
Section titled “Verify Table Emptiness”-- Before taking action, verify tables are actually empty:-- 1. Check actual row count with SELECT COUNT(*)-- 2. Review table usage patterns-- 3. Check for recent data loading activities-- 4. Verify with application teams before cleanup
Statistics Issues
Section titled “Statistics Issues”-- Common statistics problems:-- 1. Statistics gathered before data deletion-- 2. Manual statistics updates with incorrect values-- 3. Locked statistics preventing updates-- 4. Incremental statistics issues
Space Management
Section titled “Space Management”-- Understanding space allocation:-- 1. Check tablespace free space-- 2. Review table creation patterns-- 3. Analyze extent allocation-- 4. Consider deferred segment creation effects
Best Practices
Section titled “Best Practices”Regular Monitoring
Section titled “Regular Monitoring”- Monthly reviews: Run during maintenance windows
- Threshold monitoring: Focus on tables > 50 blocks
- Trend analysis: Track space waste over time
- Documentation: Record cleanup activities
Statistics Management
Section titled “Statistics Management”- Regular updates: Schedule statistics gathering
- Post-maintenance: Update statistics after bulk operations
- Monitoring: Check for stale statistics regularly
- Automation: Use automated statistics gathering where possible
Space Optimization
Section titled “Space Optimization”- Impact assessment: Calculate space savings potential
- Application coordination: Verify with application teams
- Testing: Test space reclamation in development
- Scheduling: Plan cleanup during maintenance windows
Related Scripts
Section titled “Related Scripts”- dstatbig.sql - Large table statistics analysis
- dtable.sql - Table storage analysis
- dsegbig.sql - Large segment analysis
- dcontig.sql - Free space analysis
Integration with Maintenance
Section titled “Integration with Maintenance”Space Management Process
Section titled “Space Management Process”- Run dstatzero.sql to identify candidates
- Verify tables are actually empty
- Check with application teams
- Plan and execute space reclamation
- Update statistics and re-verify
Statistics Maintenance
Section titled “Statistics Maintenance”- Include identified tables in regular statistics gathering
- Monitor for recurring zero-row situations
- Investigate patterns in space allocation
- Optimize table creation and management practices