Skip to content

Commit 6d0f942

Browse files
Create Find Table Size.js
1 parent 0db26ee commit 6d0f942

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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');

0 commit comments

Comments
 (0)