diff --git a/Server-Side Components/Background Scripts/findTableSize/Find Table Size.js b/Server-Side Components/Background Scripts/findTableSize/Find Table Size.js new file mode 100644 index 0000000000..40b1adc7d4 --- /dev/null +++ b/Server-Side Components/Background Scripts/findTableSize/Find Table Size.js @@ -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'); diff --git a/Server-Side Components/Background Scripts/findTableSize/Readme.md b/Server-Side Components/Background Scripts/findTableSize/Readme.md new file mode 100644 index 0000000000..1b00004507 --- /dev/null +++ b/Server-Side Components/Background Scripts/findTableSize/Readme.md @@ -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.