【问题标题】:Couchdb view on large documents with strings as keys times out以字符串为键的大型文档的 Couchdb 视图超时
【发布时间】:2018-05-18 21:06:17
【问题描述】:

我正在尝试在 CouchDB 2.1 中为非常大的文档创建一个视图(每个文档大约 300k-900k 行,总共大约 15-20 个文档)。

文件如下所示:

{
"222123456": {
    "_id": "222123456",
    "type": "Order",
    "0300": {
        "51234567": {
            "_id": "51234567",
            "type": "Material",
            "DS": "M532F1234567",
            "HTZ": "M532-F1234-000-00",
            "A name for some material": {
                "_id": "A name for some material",
                "type": "Description",
                "0054": {
                    "600": {
                        "1": {
                            "_id": "1",
                            "type": "Amount",
                            "X": {
                                "11220": {
                                    "_id": "11220",
                                    "type": "row"
                                },
                                "_id": "X",
                                "type": "Bulk"
                            }
                        },
                        "_id": "600",
                        "type": "Site"
                    },
                    "_id": "0054",
                    "type": "Pos"
                }
            }
        },
        "51255111": {
            // And another material
            // ...
        },
        "_id": "0300",
        "type": "Process"
    }
    // + more orders with more items
},
"222555666": {
    // Another order with more processes which contain even more materials
    // ...
},
"_id": "FileImport_001",
"_rev": "1-2f77e699332bb7c76a137b86f83bbe91",
"type": "Machine"
}

每个文档有 1-n 个订单,每个订单有 1-n 个流程,每个流程包含 1-n 个我要查询的材料。我当前的视图使用 for 循环遍历所有订单、流程和材料。

这是我正在使用的视图:

function (doc) {
    var splitMsn = doc._id.split("_"); // Split _id into [FileImport, 001] array
    for (var key_order in doc) { // For every order in the document...
        if (typeof doc[key_order] == 'object' && doc[key_order] != '') { // where the value is an object and not empty...
            var order = doc[key_order]; // Save the order as a value
            for (var key_process in order) { // ...and search all processes in that order nr
                if (typeof order[key_process] == 'object' && order[key_process] != '') { // If process contains an object as value and it's not empty
                    var process = order[key_process]; // Save the process as a value
                    for (var key_matnr in process) { // For every material in the process
                        if (typeof process[key_matnr] == 'object' && process[key_matnr] != '') { // If material nr contains an object as value and not empty
                            var matnr = process[key_matnr]; // Save material nr as value
                            for (var key_matname in matnr) { // For every material name in the material number
                                if (typeof matnr[key_matname] == 'object' && matnr[key_matname] != '') { // Contains object and not empty
                                    var matname = matnr[key_matname]; // Save material name
                                    emit([splitMsn[1], key_order, key_process, key_matnr], matname); // emit [001, 222123456, 0300, 51234567], Material name
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

通过此视图,我可以查询特定的文件编号、订单、流程和物料编号。作为回报,我会得到带有数量的材料名称(例如我所追求的 1)。

当我使用一个文档时,索引创建得很好,但即使使用第二个文档(更不用说 15 或 20 个)CouchDB 在创建视图时也会显示“操作系统进程超时”。

我的问题:有没有更快和/或更优雅的方式来迭代所有这些步骤,最终得到我需要的深埋“数量”值?

非常感谢!

【问题讨论】:

    标签: javascript couchdb couchdb-futon


    【解决方案1】:

    系统正在保护自己免受您的侵害。

    一般来说,使用大型文档并没有达到 CouchDB 的最佳状态。再加上嵌套很深的结构和非常复杂的地图,你的情况就更糟了。

    我建议您重新考虑您的数据模型。使用(很多)较小的文档(例如,每种材料一个)。您的地图功能也会简单得多。

    【讨论】:

    • 感谢您的回答!我将数据模型更改为每个订单使用一个文档,第一个索引需要一段时间,但现在更新索引要快得多。
    猜你喜欢
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2020-10-25
    • 2011-12-01
    • 2013-05-06
    相关资源
    最近更新 更多