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});
测试性能对比
| 总事务数 | 独立执行耗时 | 使用事务耗时 |
|---|---|---|
| 280 | 12.6 s | 15.4 s |
| 2000 | 80.4 s | 112.3 s |
| 2000 | 84.6 s | 102 s |
| 2000 | 81.1 s | 105 s |
注:我需要向数据库添加数千个对象,每个事务都包含上述action中的所有步骤。
问题
为什么在ArangoDB服务器端直接运行查询组成的事务会导致性能劣化,且没有实现预期的性能提升?
内容的提问来源于stack exchange,提问作者Ronak SHAH
相关产品推荐
相关产品推荐

