这是我一直在考虑但尚未进行基准测试的想法。我希望它在某些情况下会很棒:
- 插入吞吐量必须很高
- 不需要按键获取单个文档
- 所有数据都是通过视图获取的(可能与接收插入的机器不同)
计划
插入成批的文档,并使用视图将它们很好地序列化。
示例
考虑一个带有简单时间戳和消息字符串的日志文件。
0.001 Start
0.123 This could be any message
0.500 Half a second later!
1.000 One second has gone by
2.000 Two seconds has gone by
[...]
1000.000 One thousand seconds has gone by
您可以在每个文档中插入一条消息,例如:
{ "_id": "f30d09ef6a9e405994f13a38a44ee4a1",
"_rev": "1-764efa883dda1e11db47671c4a3bbd9e",
"timestamp": 0.123,
"message": "This could be any message"
}
标准批量文档优化
第一个优化是使用_bulk_docs 插入,如the CouchDB bulk-docs documentation。
二次批量插入优化
但是,第二 优化是将日志预批处理到一个更大的 Couch 文档中。例如,4 个批次(在现实世界中这会更高):
{ "_id": "f30d09ef6a9e405994f13a38a44ee4a1",
"_rev": "1-764efa883dda1e11db47671c4a3bbd9e",
"logs": [
{"timestamp": 0.001, "message": "Start"},
{"timestamp": 0.123, "message": "This could be any message"},
{"timestamp": 0.500, "message": "Half a second later!"},
{"timestamp": 1.000, "message": "One second has gone by"}
]
}
{ "_id": "74f615379d98d3c3d4b3f3d4ddce82f8",
"_rev": "1-ea4f43014d555add711ea006efe782da",
"logs": [
{"timestamp": 2.000, "message": "Two seconds has gone by"},
{"timestamp": 3.000, "message": "Three seconds has gone by"},
{"timestamp": 4.000, "message": "Four seconds has gone by"},
{"timestamp": 5.000, "message": "Five seconds has gone by"},
]
}
当然,您也可以通过_bulk_docs 插入这些数据,有效地插入成批的数据。
查看还是很容易的
将日志序列化回视图仍然很容易:
// map
function(doc) {
if(doc.logs) {
// Just unroll the log batches!
for (var i in doc.logs) {
var log = doc.logs[i];
emit(log.timestamp, log.message);
}
}
}
然后很容易获取时间戳在startkey、endkey 之间的日志,或者您有任何其他需求。
结论
这仍然没有进行基准测试,但我希望,对于某些类型的数据,批处理成块将减少内部 B-tree 写入。结合_bulk_docs,我希望看到插入吞吐量达到磁盘的硬件速度。