【发布时间】:2022-01-06 00:11:54
【问题描述】:
我正在尝试使用沙发底座上的 UPDATE 查询语句更新文档。
前任)
更新用户 SET cityIndex = 1 其中 Users.city= "NewYork";
有太多数据,我想将 3,000 划分为 4,000 并继续进行更新。我应该如何进行? 有初级索引。
【问题讨论】:
我正在尝试使用沙发底座上的 UPDATE 查询语句更新文档。
前任)
更新用户 SET cityIndex = 1 其中 Users.city= "NewYork";
有太多数据,我想将 3,000 划分为 4,000 并继续进行更新。我应该如何进行? 有初级索引。
【问题讨论】:
选项 1)
您可以使用 couchbase 事件 https://docs.couchbase.com/server/current/eventing/eventing-example-data-enrichment.html的案例2
https://docs.couchbase.com/server/current/eventing/eventing-examples.html
选项 2)
CREATE INDEX ix1 ON Users (city, cityIndex);
UPDATE Users AS u
SET u.cityIndex = 1
WHERE u.city = "NewYork" AND u.cityIndex != 1
LIMIT 4000;
【讨论】:
使用主索引,您可以对一个(可能是稳定的主索引)发出多个查询并对其进行迭代。稍微复杂一点,但一般化。
rq 是桶,s 是范围,t1 是集合。
创建集合 rq.s.t1; 在 rq.s.t1 上创建主索引;
第一个查询:
更新 rq.s.t1 使用密钥 [( 选择元().id 从 rq.s.t1 ORDER BY META().id 限制 10)] SET x = 1 返回 MAX(META().id);
第二次到 N 次查询,直到您完成(没有返回任何内容): 从上一个查询中获取 meta().id 的最大值(参见 WHERE 子句)
更新 rq.s.t1 使用密钥 [( 选择 RAW META().id 从 rq.s.t1 META().id > "007dd444-fa39-498f-b070-6cd0d41abe3d" ORDER BY META().id 限制 10)] SET x = 1 返回 META().id;
您可以通过设置初始 meta().id 与 "" 进行比较来优化此循环。
【讨论】:
vsr 提到的 Eventing Function 方法也非常简单(7 行无 cmets),您可以将其作为一次性工具运行,从 Everything 部署它。请注意,此功能不需要任何索引。
// To run configure the settings for this Function, UpdateAllCityIndex, as follows:
//
// Version 7.0+
// "Listen to Location"
// bulk.data.yourcollection
// "Eventing Storage"
// rr100.eventing.metadata
// Binding(s)
// 1. "binding type", "alias name...", "bucket.scope.collection", "Access"
// ---------------------------------------------------------------------------
// "bucket alias", "src_col", "bulk.data.Users", "read and write"
//
// Version 6.X
// "Source Bucket"
// yourbucket
// "MetaData Bucket"
// metadata
// Binding(s)
// 1. "binding type", "alias name...", "bucket", "Access"
// ---------------------------------------------------------------------------
// "bucket alias", "src_col", "Users", "read and write"
//
// For more performance set the workers to the number of physical cores
function OnUpdate(doc, meta) {
// only process documents with the city field
if (!doc.city) return;
// only update New York if cityIndex isn't already 1 or does not exist
if ( doc.city === "NewYork" && (!doc.cityIndex || doc.cityIndex !== 1 )) {
doc.cityIndex = 1;
// write back the updated doc via the alias
src_col[meta.id] = doc;
}
}
【讨论】: