Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Set the table you want to check
var tableName = 'sys_attachment_doc'; // You can change this to any table

// Try to get the table size from sys_physical_table_stats
var tableStats = new GlideRecord('sys_physical_table_stats');
tableStats.addQuery('table_name', tableName);
tableStats.setLimit(1);
tableStats.query();

var sizeGB = null;
var recordCount = null;

if (tableStats.next()) {
sizeGB = tableStats.getValue('table_size_in_gb');
}

// If the table size is not available, estimate based on record count
if (!sizeGB) {
var grCount = new GlideAggregate(tableName);
grCount.addAggregate('COUNT');
grCount.query();

if (grCount.next()) {
recordCount = parseInt(grCount.getAggregate('COUNT'), 10);
// Simple estimate: assume 1 KB per record
sizeGB = (recordCount / (1024 * 1024)).toFixed(2);
gs.info('Table [' + tableName + '] size not found in metadata. Estimated size: ' + sizeGB + ' GB');
} else {
gs.info('Table [' + tableName + '] not found or contains no records.');
}
} else {
gs.info('Table [' + tableName + '] size from metadata: ' + sizeGB + ' GB');
}

// Get record count properly
var grCountFinal = new GlideAggregate(tableName);
grCountFinal.addAggregate('COUNT');
grCountFinal.query();

recordCount = 0;
if (grCountFinal.next()) {
recordCount = parseInt(grCountFinal.getAggregate('COUNT'), 10);
}

gs.info('Table [' + tableName + '] contains ' + recordCount + ' records');
48 changes: 48 additions & 0 deletions Server-Side Components/Background Scripts/findTableSize/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Table Size Retriever – ServiceNow
Overview

This script retrieves the size of any table in your ServiceNow instance in gigabytes and provides a fallback estimate if metadata is not available. It also logs the total number of records in the table.

This is useful for instance management, capacity planning, and gaining insights into table sizes without manually inspecting each table.

Features

Retrieves the official table size from the sys_physical_table_stats table if available.

Falls back to a size estimate based on record count when metadata is missing.

Logs the number of records in the table.

Can be used with any table by changing the tableName variable.

Fully compatible with Background Scripts.

Configuration

Table Name: Set the tableName variable to the table you want to check.

var tableName = 'task'; // Replace with any table name

How It Works

Queries the sys_physical_table_stats table for the specified table name.

If the table exists in metadata, retrieves the size in gigabytes.

If metadata is not available, estimates the table size based on record count (assuming 1 KB per record).

Logs the table size (official or estimated) in GB.

Separately retrieves and logs the total number of records in the table.

Usage

Navigate to System Definition → Scripts – Background in your ServiceNow instance.

Copy and paste the script.

Update the tableName variable as needed.

Click Run Script.

Check the system logs to view the table size and record count.
Loading