File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Server-Side Components/Background Scripts/findTableSize Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ // Set the table you want to check
2+ var tableName = 'sys_attachment_doc' ; // You can change this to any table
3+
4+ // Try to get the table size from sys_physical_table_stats
5+ var tableStats = new GlideRecord ( 'sys_physical_table_stats' ) ;
6+ tableStats . addQuery ( 'table_name' , tableName ) ;
7+ tableStats . setLimit ( 1 ) ;
8+ tableStats . query ( ) ;
9+
10+ var sizeGB = null ;
11+ var recordCount = null ;
12+
13+ if ( tableStats . next ( ) ) {
14+ sizeGB = tableStats . getValue ( 'table_size_in_gb' ) ;
15+ }
16+
17+ // If the table size is not available, estimate based on record count
18+ if ( ! sizeGB ) {
19+ var grCount = new GlideAggregate ( tableName ) ;
20+ grCount . addAggregate ( 'COUNT' ) ;
21+ grCount . query ( ) ;
22+
23+ if ( grCount . next ( ) ) {
24+ recordCount = parseInt ( grCount . getAggregate ( 'COUNT' ) , 10 ) ;
25+ // Simple estimate: assume 1 KB per record
26+ sizeGB = ( recordCount / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
27+ gs . info ( 'Table [' + tableName + '] size not found in metadata. Estimated size: ' + sizeGB + ' GB' ) ;
28+ } else {
29+ gs . info ( 'Table [' + tableName + '] not found or contains no records.' ) ;
30+ }
31+ } else {
32+ gs . info ( 'Table [' + tableName + '] size from metadata: ' + sizeGB + ' GB' ) ;
33+ }
34+
35+ // Get record count properly
36+ var grCountFinal = new GlideAggregate ( tableName ) ;
37+ grCountFinal . addAggregate ( 'COUNT' ) ;
38+ grCountFinal . query ( ) ;
39+
40+ recordCount = 0 ;
41+ if ( grCountFinal . next ( ) ) {
42+ recordCount = parseInt ( grCountFinal . getAggregate ( 'COUNT' ) , 10 ) ;
43+ }
44+
45+ gs . info ( 'Table [' + tableName + '] contains ' + recordCount + ' records' ) ;
You can’t perform that action at this time.
0 commit comments