diff --git a/Server-Side Components/Background Scripts/Table Growth Analysis/README.md b/Server-Side Components/Background Scripts/Table Growth Analysis/README.md new file mode 100644 index 0000000000..c9dd0efdb0 --- /dev/null +++ b/Server-Side Components/Background Scripts/Table Growth Analysis/README.md @@ -0,0 +1,18 @@ +# Table Size Analysis Script + +This script checks the number of records in selected ServiceNow tables and shows how many were created in the last 30 days. + +## Tables Checked +- `task` +- `cmdb_ci` +- `sc_cat_item` + +## What It Does +- Logs the start of the analysis. +- Counts total records in each table. +- Counts records created in the last 30 days. +- Logs both counts to the system log. + +## How to Use +1. Add or remove table names in the `tablesToCheck` list. +2. Run the script in a background script or scheduled job. diff --git a/Server-Side Components/Background Scripts/Table Growth Analysis/tableGrowthAnalysis.js b/Server-Side Components/Background Scripts/Table Growth Analysis/tableGrowthAnalysis.js new file mode 100644 index 0000000000..f313596910 --- /dev/null +++ b/Server-Side Components/Background Scripts/Table Growth Analysis/tableGrowthAnalysis.js @@ -0,0 +1,34 @@ +// === TABLE SIZE ANALYSIS SCRIPT === +// Log the start of the analysis +gs.info('=== TABLE SIZE ANALYSIS ==='); + +// Define the list of tables to analyze +var tablesToCheck = ['task', 'cmdb_ci', 'sc_cat_item']; + +// Loop through each table in the list +for (var i = 0; i < tablesToCheck.length; i++) { + var tableName = tablesToCheck[i]; // Get current table name + + // Create a GlideAggregate object to count total records in the table + var grCount = new GlideAggregate(tableName); + grCount.addAggregate('COUNT'); // Add COUNT aggregate + grCount.query(); // Execute the query + + // If the query returns a result + if (grCount.next()) { + var recordCount = grCount.getAggregate('COUNT'); // Get total record count + gs.info('Table: ' + tableName + ' | Record count: ' + recordCount); // Log the count + + // Optional: Analyze growth by checking records created in the last 30 days + var grRecent = new GlideAggregate(tableName); + grRecent.addAggregate('COUNT'); // Add COUNT aggregate + grRecent.addQuery('sys_created_on', '>=', gs.daysAgo(30)); // Filter records created in last 30 days + grRecent.query(); // Execute the query + + // If the query returns a result + if (grRecent.next()) { + var recentCount = grRecent.getAggregate('COUNT'); // Get count of recent records + gs.info(' - Records created last 30 days: ' + recentCount); // Log the recent count + } + } +}