[英]Strange performance problem with UPDATE in ArangoDB
我正在创建一个使用 ArangoDB 作为数据存储的 Node.js 应用程序。 基本上,我拥有的数据结构是两个表,一个用于管理所谓的instances
,另一个用于entities
。 我做的是以下内容:
instance
的instances
集合中都有一个文档。entities
集合添加实体时,我还想跟踪属于特定实例的实体。instance
文档都有一个用于entities
的数组字段,我将实体的 ID 推送到该数组中。下面的代码显示了大纲:
// Connect to ArangoDB.
db = new Database(...);
db.useBasicAuth(user, password);
// Use the database.
await db.createDatabase(database);
db.useDatabase(database);
// Create the instance collection.
instanceCollection = db.collection(`instances-${uuid()}`);
await instanceCollection.create();
// Create the entities collection.
entityCollection = db.collection(`entities-${uuid()}`);
await entityCollection.create();
// Setup an instance.
instance = {
id: uuid(),
entities: []
};
// Create the instance in the database.
await db.query(aql`
INSERT ${instance} INTO ${instanceCollection}
`);
// Add lots of entities.
for (let i = 0; i < scale; i++) {
// Setup an entity.
const entity = {
id: uuid()
};
// Update the instance.
instance.entities.push(entity);
// Insert the entity in the database.
await db.query(aql`
INSERT ${entity} INTO ${entityCollection}
`);
// Update the instance in the database.
await db.query(aql`
FOR i IN ${instanceCollection}
FILTER i.id == ${instance.id}
UPDATE i WITH ${instance} IN ${instanceCollection} OPTIONS { mergeObjects: false }
`);
}
现在的问题是,我添加的实体越多,这会变得非常慢。 它基本上呈指数增长,尽管我预计会呈线性增长:
Running benchmark 'add and update'
100 Entities: 348.80ms [+0.00%]
1000 Entities: 3113.55ms [-10.74%]
10000 Entities: 90180.18ms [+158.54%]
添加索引会产生影响,但不会对整体问题产生任何影响:
Running benchmark 'add and update with index'
100 Entities: 194.30ms [+0.00%]
1000 Entities: 2090.15ms [+7.57%]
10000 Entities: 89673.52ms [+361.52%]
问题可以追溯到UPDATE
语句。 如果您忽略它而只使用数据库的INSERT
语句,则事情会线性扩展。 因此,更新本身似乎有问题。 但是,我不明白问题出在哪里。
这就是我想理解的:为什么UPDATE
语句随着时间的推移变得显着变慢? 我用错了吗? 这是 ArangoDB 中的已知问题吗? ……?
我不感兴趣的是讨论这种方法:请按照给定的方式进行。 让我们关注UPDATE
语句的性能。 有任何想法吗?
正如评论中所要求的,这里有一些关于系统设置的信息:
您是否尝试过解释或分析查询?
Arango 的解释计划描述非常好。 您可以使用内置的 Aardvark Web 管理界面或使用db._explain(query)
访问explain
。 这是你的样子:
Execution plan:
Id NodeType Est. Comment
1 SingletonNode 1 * ROOT
5 CalculationNode 1 - LET #5 = { "_key" : "123", "_id" : "collection/123", "_rev" : "_aQcjewq---", ...instance } /* json expression */ /* const assignment */
2 EnumerateCollectionNode 2 - FOR i IN collection /* full collection scan, projections: `_key`, `id` */ FILTER (i.`id` == "1") /* early pruning */
6 UpdateNode 0 - UPDATE i WITH #5 IN pickups
Indexes used:
By Name Type Collection Unique Sparse Selectivity Fields Ranges
6 primary primary pickups true false 100.00 % [ `_key` ] i
计划中的关键部分是- FOR i IN collection /*
full collection scan
完全集合扫描将......很慢。 它应该随着您收藏的大小线性增长。 因此, for
scale
迭代的for
循环,这绝对意味着集合大小呈指数增长。
索引id
应该会有所帮助,但我认为这取决于您如何创建索引。
使用_key
而不是 index 更改计划以显示primary
- FOR i IN pickups /* primary index scan, index only, projections: `_key` */
这应该是恒定时间,所以for
scale
迭代的for
循环,这应该意味着线性时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.