You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArangoDB JS事务性能劣化咨询:为何未实现性能提升

ArangoDB服务器端JS事务性能劣化问题

我编写了一段在ArangoDB服务器上执行的JS事务,原本期望通过减少客户端与服务器的通信次数来提升性能,但测试后发现事务执行的耗时反而比手动逐个执行查询和插入操作更长。

待更新的JSON数据

{
  "col1": {"name": "abc", "creator": "xyz", "desc": "this is obj"},
  "col2": [{"id": 1, "type": "t1", "data": []}, {"id": 2, "type": "t2", "data": []}]
}

其中col1、col2、col3对应不同的集合,值为需要插入的数据。对于col2和col3,需要检查是否存在相同id的数据,若存在则先删除再重新插入,保证数据库中是最新的对象。

事务Action函数实现

const action = String(function(params) {
  const col1 = params.col1;
  const col2 = params.col2;

  const { db, aql } = require('@arangodb'); 
  // 插入col1文档
  let q = aql`INSERT ${col1}
              IN col1
              OPTIONS {overwrite: true}
              RETURN {doc: NEW, type: OLD ? 'update' : 'insert'}`;
  let cursor = db._query(q);
  const objSaved = cursor.next();

  // 处理col2的对象
  for(let item in col2) {
    // 删除集合中已存在的项
    let q = aql`for p in col2
                  FILTER p.id == ${item.id}
                  REMOVE p in col2
                  return OLD`;
    db._query(q);

    q = aql`INSERT ${item}
            IN col2
            OPTIONS {overwrite:true}`;
    db._query(q);
  }

});

const obj = {
  "col1": {"name": "abc", "creator": "xyz", "desc": "this is obj"},
  "col2": [{"id": 1, "type": "t1", "data": []}, {"id": 2, "type": "t2", "data": []}],
  "col3": [{"id": 11, "type": "m1", "data": []}, {"id": 22, "type": "m1", "data": []}]
}

db.executeTransaction({'write': ['col1', 'col2']}, action, {'params': obj});

测试性能对比

总事务数独立执行耗时使用事务耗时
28012.6 s15.4 s
200080.4 s112.3 s
200084.6 s102 s
200081.1 s105 s

注:我需要向数据库添加数千个对象,每个事务都包含上述action中的所有步骤。

问题

为什么在ArangoDB服务器端直接运行查询组成的事务会导致性能劣化,且没有实现预期的性能提升?

内容的提问来源于stack exchange,提问作者Ronak SHAH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 04:40:25