1

観察してください:

MongoDB shell version: 2.4.1
connecting to: test
> use dummy
switched to db dummy
> db.invoices.find({'items.nameTags': /^z/}, {_id: 1}).explain()
{
        "cursor" : "BtreeCursor items.nameTags_1_created_1_special_1__id_1_items.qty_1_items.total_1 multi",
        "isMultiKey" : true,
        "n" : 55849,
        "nscannedObjects" : 223568,
        "nscanned" : 223568,
        "nscannedObjectsAllPlans" : 223568,
        "nscannedAllPlans" : 223568,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 86,
        "nChunkSkips" : 0,
        "millis" : 88864,
        "indexBounds" : {
                "items.nameTags" : [
                        [
                                "z",
                                "{"
                        ],
                        [
                                /^z/,
                                /^z/
                        ]
                ],
                "created" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ],
                "special" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ],
                "_id" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ],
                "items.qty" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ],
                "items.total" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ]
        },
        "server" : "IL-Mark-LT:27017"
}
>

インデックスの定義は次のとおりです。

> db.system.indexes.find({name : 'items.nameTags_1_created_1_special_1__id_1_items.qty_1_items.total_1'}).pretty()
{
        "v" : 1,
        "key" : {
                "items.nameTags" : 1,
                "created" : 1,
                "special" : 1,
                "_id" : 1,
                "items.qty" : 1,
                "items.total" : 1
        },
        "ns" : "dummy.invoices",
        "name" : "items.nameTags_1_created_1_special_1__id_1_items.qty_1_items.total_1"
}
>

最後に、請求書ドキュメントの例を次に示します (項目は 2 つだけ):

> db.invoices.findOne({itemCount: 2})
{
        "_id" : "85923",
        "customer" : "Wgtd Fm 91",
        "businessNo" : "314227928",
        "billTo_name" : "Wgtd Fm 91",
        "billTo_addressLine1" : "3839 Ross Street",
        "billTo_addressLine2" : "Kingston, ON",
        "billTo_postalCode" : "K7L 4V4",
        "purchaseOrderNo" : "boi",
        "terms" : "COD",
        "shipDate" : "2013-07-10",
        "shipVia" : "Moses Transportation Inc.",
        "rep" : "Snowhite",
        "items" : [
                {
                        "qty" : 4,
                        "name" : "CA 7789",
                        "desc" : "3 pc. Coffee Table set (Silver)",
                        "price" : 222.3,
                        "total" : 889.2,
                        "nameTags" : [
                                "ca 7789",
                                "a 7789",
                                " 7789",
                                "7789",
                                "789",
                                "89",
                                "9"
                        ],
                        "descTags" : [
                                "3",
                                "pc",
                                "c",
                                "coffee",
                                "offee",
                                "ffee",
                                "fee",
                                "ee",
                                "e",
                                "table",
                                "able",
                                "ble",
                                "le",
                                "e",
                                "set",
                                "et",
                                "t",
                                "silver",
                                "ilver",
                                "lver",
                                "ver",
                                "er",
                                "r"
                        ]
                },
                {
                        "qty" : 4,
                        "name" : "QP 8681",
                        "desc" : "Ottoman Bed",
                        "price" : 1179.1,
                        "total" : 4716.4,
                        "nameTags" : [
                                "qp 8681",
                                "p 8681",
                                " 8681",
                                "8681",
                                "681",
                                "81",
                                "1"
                        ],
                        "descTags" : [
                                "ottoman",
                                "ttoman",
                                "toman",
                                "oman",
                                "man",
                                "an",
                                "n",
                                "bed",
                                "ed",
                                "d"
                        ]
                }
        ],
        "itemCount" : 2,
        "discount" : "10%",
        "delivery" : 250,
        "hstPercents" : 13,
        "subTotal" : 5605.6,
        "totalBeforeHST" : 5295.04,
        "total" : 5983.4,
        "totalDiscount" : 560.56,
        "hst" : 688.36,
        "modified" : "2012-10-08",
        "created" : "2014-06-25",
        "version" : 0
}
>

explain()私の問題は、前述の出力によると、mongodb がインデックスのみを使用しないことです。なんで?結局のところ_id、インデックスの一部であるフィールドのみを要求します。

一般的に、私は何か非常に間違ったことをしていると感じています。私の請求書コレクションには 65,000 件の請求書があり、合計 3,291,092 項目があります。explain()クエリに約 89 秒かかりました。

私は何を間違っていますか?

4

1 に答える 1