AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 341332
Accepted
DaPeda
DaPeda
Asked: 2024-07-31 22:29:38 +0800 CST2024-07-31 22:29:38 +0800 CST 2024-07-31 22:29:38 +0800 CST

总计解除后的索引使用情况

  • 772

我们的 MongoDB 中有一个集合,由于用例的改变,到目前为止,一个小查询现在真的很慢。所讨论的聚合有点像这样:

db.collection.aggregate([
  {
    $match: {
      "_id": {
        $in: [ list of 1000 ids ]
      }
    }
  },
  {
    $unwind: "array_field"
  },
  {
    $match: {
      "array_field.sub_field": 123456789
    }
  }
])

{"_id": 1, "array_field.sub_field": 1}我现在的问题是,这里是否会考虑类似的索引,或者是否有必要让开发人员将第二个$match索引移到第一个索引?

遗憾的是,在可预见的未来,数据模型的重新设计还未实现。

index
  • 1 1 个回答
  • 14 Views

1 个回答

  • Voted
  1. Best Answer
    ray
    2024-08-01T01:26:57+08:002024-08-01T01:26:57+08:00

    您可以构建一个小型 PoC 来验证索引的使用情况。该索引将得到利用,因为它是按照 ESR 规则构建的。

    解释输出:

    {
      "$clusterTime": {
        "clusterTime": Timestamp(1722446531, 2),
        "signature": {
          "hash": BinData(0, "1UsZLM1VfzhxUTxn/sF3YXXOCh8="),
          "keyId": NumberLong(7360702457824935938)
        }
      },
      "command": {
        "$db": "f75abdd274be932b64a3fd04bcb14d27",
        "aggregate": "collection",
        "cursor": {
          "batchSize": 1000
        },
        "maxTimeMS": NumberLong(20000),
        "pipeline": [
          {
            "$match": {
              "_id": {
                "$in": [
                  0,
                  1,
                  2,
                  3,
                  4,
                  5,
                  6,
                  7,
                  8,
                  9
                ]
              }
            }
          },
          {
            "$unwind": "$array_field"
          },
          {
            "$match": {
              "array_field.sub_field": "a"
            }
          }
        ]
      },
      "explainVersion": "1",
      "operationTime": Timestamp(1722446531, 2),
      "serverParameters": {
        "internalDocumentSourceGroupMaxMemoryBytes": 104857600,
        "internalDocumentSourceSetWindowFieldsMaxMemoryBytes": 104857600,
        "internalLookupStageIntermediateDocumentMaxSizeBytes": 104857600,
        "internalQueryFacetBufferSizeBytes": 104857600,
        "internalQueryFacetMaxOutputDocSizeBytes": 104857600,
        "internalQueryMaxAddToSetBytes": 104857600,
        "internalQueryMaxBlockingSortMemoryUsageBytes": 104857600,
        "internalQueryProhibitBlockingMergeOnMongoS": 0
      },
      "stages": [
        {
          "$cursor": {
            "executionStats": {
              "executionStages": {
                "advanced": 10,
                "alreadyHasObj": 0,
                "docsExamined": 10,
                "executionTimeMillisEstimate": 0,
                "inputStage": {
                  "advanced": 10,
                  "direction": "forward",
                  "dupsDropped": 0,
                  "dupsTested": 0,
                  "executionTimeMillisEstimate": 0,
                  "indexBounds": {
                    "_id": [
                      "[0.0, 0.0]",
                      "[1.0, 1.0]",
                      "[2.0, 2.0]",
                      "[3.0, 3.0]",
                      "[4.0, 4.0]",
                      "[5.0, 5.0]",
                      "[6.0, 6.0]",
                      "[7.0, 7.0]",
                      "[8.0, 8.0]",
                      "[9.0, 9.0]"
                    ]
                  },
                  "indexName": "_id_",
                  "indexVersion": 2,
                  "isEOF": 1,
                  "isMultiKey": false,
                  "isPartial": false,
                  "isSparse": false,
                  "isUnique": true,
                  "keyPattern": {
                    "_id": 1
                  },
                  "keysExamined": 10,
                  "multiKeyPaths": {
                    "_id": []
                  },
                  "nReturned": 10,
                  "needTime": 0,
                  "needYield": 0,
                  "restoreState": 1,
                  "saveState": 1,
                  "seeks": 1,
                  "stage": "IXSCAN",
                  "works": 11
                },
                "isEOF": 1,
                "nReturned": 10,
                "needTime": 0,
                "needYield": 0,
                "restoreState": 1,
                "saveState": 1,
                "stage": "FETCH",
                "works": 12
              },
              "executionSuccess": true,
              "executionTimeMillis": 0,
              "nReturned": 10,
              "totalDocsExamined": 10,
              "totalKeysExamined": 10
            },
            "queryPlanner": {
              "indexFilterSet": false,
              "maxIndexedAndSolutionsReached": false,
              "maxIndexedOrSolutionsReached": false,
              "maxScansToExplodeReached": false,
              "namespace": "f75abdd274be932b64a3fd04bcb14d27.collection",
              "parsedQuery": {
                "_id": {
                  "$in": [
                    0,
                    1,
                    2,
                    3,
                    4,
                    5,
                    6,
                    7,
                    8,
                    9
                  ]
                }
              },
              "planCacheKey": "D97631C1",
              "queryHash": "772EC04C",
              "rejectedPlans": [
                {
                  "inputStage": {
                    "direction": "forward",
                    "indexBounds": {
                      "_id": [
                        "[0.0, 0.0]",
                        "[1.0, 1.0]",
                        "[2.0, 2.0]",
                        "[3.0, 3.0]",
                        "[4.0, 4.0]",
                        "[5.0, 5.0]",
                        "[6.0, 6.0]",
                        "[7.0, 7.0]",
                        "[8.0, 8.0]",
                        "[9.0, 9.0]"
                      ],
                      "array_field.sub_field": [
                        "[MinKey, MaxKey]"
                      ]
                    },
                    "indexName": "ix",
                    "indexVersion": 2,
                    "isMultiKey": true,
                    "isPartial": false,
                    "isSparse": false,
                    "isUnique": false,
                    "keyPattern": {
                      "_id": 1,
                      "array_field.sub_field": 1
                    },
                    "multiKeyPaths": {
                      "_id": [],
                      "array_field.sub_field": [
                        "array_field"
                      ]
                    },
                    "stage": "IXSCAN"
                  },
                  "stage": "FETCH"
                }
              ],
              "winningPlan": {
                "inputStage": {
                  "direction": "forward",
                  "indexBounds": {
                    "_id": [
                      "[0.0, 0.0]",
                      "[1.0, 1.0]",
                      "[2.0, 2.0]",
                      "[3.0, 3.0]",
                      "[4.0, 4.0]",
                      "[5.0, 5.0]",
                      "[6.0, 6.0]",
                      "[7.0, 7.0]",
                      "[8.0, 8.0]",
                      "[9.0, 9.0]"
                    ]
                  },
                  "indexName": "_id_",
                  "indexVersion": 2,
                  "isMultiKey": false,
                  "isPartial": false,
                  "isSparse": false,
                  "isUnique": true,
                  "keyPattern": {
                    "_id": 1
                  },
                  "multiKeyPaths": {
                    "_id": []
                  },
                  "stage": "IXSCAN"
                },
                "stage": "FETCH"
              }
            }
          },
          "executionTimeMillisEstimate": NumberLong(0),
          "nReturned": NumberLong(10)
        },
        {
          "$unwind": {
            "path": "$array_field"
          },
          "executionTimeMillisEstimate": NumberLong(0),
          "nReturned": NumberLong(20)
        },
        {
          "$match": {
            "array_field.sub_field": {
              "$eq": "a"
            }
          },
          "executionTimeMillisEstimate": NumberLong(0),
          "nReturned": NumberLong(5)
        }
      ]
    }
    

    您可以看到IXSCAN的存在,这意味着该指数是有杠杆的。

    蒙戈游乐场


    不过,仍然建议开发人员$match尽早移动该阶段,因为这有助于最小化中间结果的大小,从而提高性能。

    db.collection.aggregate([
      {
        $match: {
          "_id": {
            $in: [
              0,
              1,
              2,
              3,
              4,
              5,
              6,
              7,
              8,
              9
            ]
          },
          "array_field.sub_field": "a"
        }
      },
      {
        $unwind: "$array_field"
      }
    ]).explain("executionStats")
    

    Mongo 游乐场

    • 0

相关问题

  • 如何根据一行的字段在索引上创建多个条目?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 我在索引上放了多少“填充”?

  • RDBMS 上的“索引”是什么意思?[关闭]

  • 如何在 MySQL 中创建条件索引?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve