Mar 05, 2020 04:49 PM
Here is a script that will count records per table and output the result as a little CSV output.
It is a little quick and dirty and will take ‘some time’ to run if you have large record counts.
Hope it is useful to someone.
let tableCount = base.tables.length;
let totalRecords = 0;
for(let i = tableCount - 1; i >= 0; i--){
let name = base.tables[i].name;
let table = base.getTable(name);
let result = await table.selectRecordsAsync();
totalRecords += result.records.length;
output.text(name + ' , ' + result.records.length);
}
output.text('Total Records in ' + base.name + ', ' + totalRecords);
Mar 05, 2020 06:14 PM
It will be near instant if you build the name/length list in a string and output the results outside the loop.
Mar 05, 2020 06:21 PM
Most of the time is taken up with this line.
There isn’t a way to get a count without selecting the records and any table with thousands of records will take a little time to select the records so that you can return the length.
Mar 05, 2020 08:39 PM
Ahhhh, yes - you’re right. The only way to speed this up is to use promise.all() and remove the awaits. This would allow multiple record selects to run simultaneously.
Mar 05, 2020 09:28 PM
You can also speed up the process by limiting the fields that you are requesting. Tell selectRecordsAsync()
that you only want the primary field.
Mar 06, 2020 05:41 AM
How [exactly] do you do that?
Mar 06, 2020 06:48 AM
const table = await input.tableAsync('Pick a table');
const primaryFieldId = table.fields[0].id;
const queryResult = await table.selectRecordsAsync({fields: [primaryFieldId]});
const recordCount = queryResult.records.length;
output.text(`Table [${table.name}] has ${recordCount} record(s).`);
I’m not setup to do speed tests on tables with multiple thousands of records, so I cannot give you specific statistics on how much faster this would be. The difference in speed would depend on the number of fields and the amount of data in those fields.
If either of you are setup for speed tests of this sort, would you mind reporting back the results?
Mar 06, 2020 06:53 AM
Of course, ergo - the most performant approach is to call selectRecordsAsync() without any fields list.
Mar 06, 2020 07:06 AM
You’re right. No fields is better than even one field. Here is a better version that calls selectRecordsAsync()
and returns no fields.
const table = await input.tableAsync('Pick a table');
const queryResult = await table.selectRecordsAsync({fields: []});
const recordCount = queryResult.records.length;
output.text(`Table [${table.name}] has ${recordCount} record(s).`);
If you do not set the fields for selectRecordsAsync()
, which was the case in the original example in this thread, the API returns all the fields. It is the same as with the Standard API.
Mar 06, 2020 07:14 AM
Hmmm, that’s not my experience, what am I missing?
// Load all of the records in the table
let processTable = base.getTable("Process Status & Duration");
let queryResults = await processTable.selectRecordsAsync();
output.inspect(queryResults);
From a performance perspective, this behaviour is ideal; I needn’t tell the method to give me less; it seems to do that implicitly.