nk.storageRead performance compare

We understand that nk.storageRead supports multiple read requests simultaneously. However, if we split each request and invoke nk.storageRead separately, how significantly would performance be impacted? If there is a substantial performance difference, what factors contribute to it?”

for example

let userId = '4ec4f126-3f9d-11e7-84ef-b7c182b36521';

let objectIds: nkruntime.StorageReadRequest[] = [
  { collection: 'save', key: 'save1', userId: userId },
  { collection: 'save', key: 'save2', userId },
  { collection: 'save', key: 'save3', userId },
];

let results: nkruntime.StorageObject[] = [];

try {
    results = nk.storageRead(objectIds);
} catch (error) {
    

compare with

let userId = '4ec4f126-3f9d-11e7-84ef-b7c182b36521';

let objectId1: nkruntime.StorageReadRequest[] = [
  { collection: 'save', key: 'save1', userId: userId },
];


let objectId2: nkruntime.StorageReadRequest[] = [
  { collection: 'save', key: 'save2', userId },
];


let objectId3: nkruntime.StorageReadRequest[] = [
  { collection: 'save', key: 'save3', userId },
];

let results1: nkruntime.StorageObject[] = [];
let results2: nkruntime.StorageObject[] = [];
let results3: nkruntime.StorageObject[] = [];
try {
    results1 = nk.storageRead(objectIds1);
    results2 = nk.storageRead(objectIds2);
    results3 = nk.storageRead(objectIds3);
} catch (error) {
    

Hello @mengxin,

The difference is that each nk.storageRead performs a separate SQL query to the database, whilst all the reads in the same nk.storageRead are a single query.

Each separate call incurs in a round-trip to the database and another lookup. The performance impact depends on your DB hardware, load and other factors.

Essentially, it’s ok to split your reads across multiple calls if your logic so requires, but you should batch them within the same call whenever possible as it is more efficient.

Hope this clarifies.

1 Like