HarmonyOS RdbStore 中 batchInsert 需要增加 ConflictResolution 参数

需要往数据库中批量插入文件信息:

public 
async 
insert(files: 
FileMeta[] 
) 
{ 
  let t = Date.now(); 
  // 这里如果用foreach, SQL执行失败不会异常退出,而是继续执行下一个SQL,不符合预期 
  for (let i = 0; i < files.length; ++i) { 
    const file = files[i]; 
    const bucket: ValuesBucket = { 
      'fid': file.fid, 
      'from_diff': file.isFromDiff, 
      'base_path': file.serverPath, 
      'file_name': file.fileName, 
      'file_size': file.fileSize, 
      // other file properties ... 
    }; 
    await this.dbStore.insert(FileMetaDao.FILE_META_TABLE, bucket, rdb.ConflictResolution.ON_CONFLICT_REPLACE); 
  } 
  t = Date.now() - t; 
  Logger.debug(LOG_TAG, 'insert %{public}d rows cost time: %{public}d ms', files.length, t); 
} 
 
// insert files ... 
this.dbStore.beginTransaction(); 
try { 
  await this.insert(files); 
  this.dbStore.commit(); 
} catch (err) { 
  Logger.warn(LOG_TAG, 'transaction failed: %{public}s', JSON.stringify(err)); 
  this.dbStore.rollBack(); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

上边业务中, 启动一个事务, 循环调用RdbStore的insert方法插入文件信息,并且指定冲突策略是 rdb.ConflictResolution.ON_CONFLICT_REPLACE, 使用这种方式, 插入400条信息大概需要 700ms左右。

在RdbStore中与一个批量插入API: batchInsert但是这个无法指定冲突策略, 因此无法用来优化上边业务场景。

期望:

1, 如何优化写入事务效率?

2, batchInsert 接口增加 ConflictResolution参数, 避免中间某个sql执行失败。

HarmonyOS
2024-11-22 10:18:17
326浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

可以使用insertSync,参考如下示例:

public 
insert(files: 
FileMeta[] 
) 
{ 
  let t = Date.now(); 
  // 这里如果用foreach, SQL执行失败不会异常退出,而是继续执行下一个SQL,不符合预期 
  for (let i = 0; i < files.length; ++i) { 
    const file = files[i]; 
    const bucket: ValuesBucket = { 
      'fid': file.fid, 
      'from_diff': file.isFromDiff, 
      'base_path': file.serverPath, 
      'file_name': file.fileName, 
      'file_size': file.fileSize, 
      // other file properties ... 
    }; 
    this.dbStore.insertSync(FileMetaDao.FILE_META_TABLE, bucket, rdb.ConflictResolution.ON_CONFLICT_REPLACE); 
  } 
  t = Date.now() - t; 
  Logger.debug(LOG_TAG, 'insert %{public}d rows cost time: %{public}d ms', files.length, t); 
} 
 
// insert files ... 
this.dbStore.beginTransaction(); 
try { 
  this.insert(files); 
  this.dbStore.commit(); 
} catch (err) { 
  Logger.warn(LOG_TAG, 'transaction failed: %{public}s', JSON.stringify(err)); 
  this.dbStore.rollBack(); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
分享
微博
QQ
微信
回复
2024-11-22 15:53:22


相关问题
HarmonyOS faultLogger.query接口增加参数
321浏览 • 1回复 待解决
HarmonyOS Text,如何增加Image
481浏览 • 1回复 待解决
HarmonyOS 在应用增加子进程UIAbility
539浏览 • 1回复 待解决
HarmonyOS 关于RdbStore的操作的一些疑问
622浏览 • 1回复 待解决
HarmonyOS web组件移除增加问题
260浏览 • 1回复 待解决
如何在HarmonyOS添加编译参数
1125浏览 • 1回复 待解决
关系型数据库RdbStore执行sql语句失败
2165浏览 • 1回复 待解决
HarmonyOS Web组件如何增加UserAgent
541浏览 • 1回复 待解决
HarmonyOS Toolbar如何增加底部阴影
381浏览 • 1回复 待解决
HarmonyOS如何增加控件点击热区?
958浏览 • 1回复 待解决
HarmonyOS 如何给图片增加文字水印
424浏览 • 1回复 待解决
HarmonyOS 图片增加自定义水印
336浏览 • 1回复 待解决