Is it possible to find out how large (KB/MB/GB) a table is?

Meloper
Kilo Sage

1. Is it possible to find out how large (KB/MB/GB) a table is?
2. If so, can you also find out for a filtered table?
So you would have a table with 500K record, know the size, filter the table to 200k recrods. Can you then scale it or display it correctly?

2 ACCEPTED SOLUTIONS

Robert H
Mega Sage

Hello Meloper,

 

You can use the following script to calculate the size of the data in a table. Just change the table name as needed.

 

function getFieldNames(table) {
    var fieldNames = [];
    var gr = new GlideRecord(table);
    gr.initialize();
    for (var field in gr) {
        fieldNames.push(field);
    }
    return fieldNames;
}

var table = 'incident';
var size = 0, count = 0;
var fieldNames = getFieldNames(table);
var gr = new GlideRecord(table);
gr.setLimit(10000);
gr.query();
while (gr.next()) {
    fieldNames.forEach(field => {
		size += gr[field].toString().length;
    });
	count++;
}
gs.print(gs.getMessage('{0} records with a total size of {1} bytes', [count, size]));

 

Example output:

103.0 records with a total size of 52582.0 bytes

 

This calculation takes time so I have set a hard limit of 10k records to analyze.

 If your table has more than 10k records you need to multiply the result accordingly, e.g. if the script returns 5 MByte for 10k records but you have 500k total records you have to do 5 MByte * 50 = 250 MByte.

 

Please also note that the actual table size is probably a bit larger than that due to meta data and other overhead, like some unusual Unicode characters that can take up to 4 bytes per character.

 

Regards,

Robert

View solution in original post

Hello @Meloper 

 

For regular text that uses the Latin alphabet (like English) 1 character is indeed 1 byte. But more exotic Unicode characters can require up to 4 bytes:

RobertH_0-1744276260401.png

So the code that I provided is actually a bit simplified and assumes that 1 character = 1 byte, which should give you a rough idea.

 

For reference fields, the code uses the length of the sys_id (always 32 characters) and not the display value, because that display value is not actually stored in the table we are analyzing but the table that the reference field points to.

 

Regards,

Robert

View solution in original post

10 REPLIES 10

@Shivalika 

It would be great if you could share screenshot containing sample result for incident so that it helps members

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Prathamesh26
Tera Expert

Hello @Meloper 

 

Kindly go through the below link once that might help you.

Link - https://www.servicenow.com/community/architect-articles/servicenow-database-size-estimate-the-size-o...

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this question to solved bucket. 

 

Regards,

Prathamesh

Robert H
Mega Sage

Hello Meloper,

 

You can use the following script to calculate the size of the data in a table. Just change the table name as needed.

 

function getFieldNames(table) {
    var fieldNames = [];
    var gr = new GlideRecord(table);
    gr.initialize();
    for (var field in gr) {
        fieldNames.push(field);
    }
    return fieldNames;
}

var table = 'incident';
var size = 0, count = 0;
var fieldNames = getFieldNames(table);
var gr = new GlideRecord(table);
gr.setLimit(10000);
gr.query();
while (gr.next()) {
    fieldNames.forEach(field => {
		size += gr[field].toString().length;
    });
	count++;
}
gs.print(gs.getMessage('{0} records with a total size of {1} bytes', [count, size]));

 

Example output:

103.0 records with a total size of 52582.0 bytes

 

This calculation takes time so I have set a hard limit of 10k records to analyze.

 If your table has more than 10k records you need to multiply the result accordingly, e.g. if the script returns 5 MByte for 10k records but you have 500k total records you have to do 5 MByte * 50 = 250 MByte.

 

Please also note that the actual table size is probably a bit larger than that due to meta data and other overhead, like some unusual Unicode characters that can take up to 4 bytes per character.

 

Regards,

Robert

Wow, thanks, so 1 Char = 1 Byte?
is that true?  Does it count the Display Value or the SysID for Ref Fields, and is it true that 1 Char is on Byte?

Hello @Meloper 

 

For regular text that uses the Latin alphabet (like English) 1 character is indeed 1 byte. But more exotic Unicode characters can require up to 4 bytes:

RobertH_0-1744276260401.png

So the code that I provided is actually a bit simplified and assumes that 1 character = 1 byte, which should give you a rough idea.

 

For reference fields, the code uses the length of the sys_id (always 32 characters) and not the display value, because that display value is not actually stored in the table we are analyzing but the table that the reference field points to.

 

Regards,

Robert