mongodb应用心得

基于springboot做mysql业务基础数据分析到mongodb文档库

索引分析

查看当前集合索引:db.collection.getIndexes()

explain 方法查看是如何执行的:db.users.find({ name: "John" }).sort({ age: -1 }).explain("executionStats")

参数指标:

queryPlanner:显示查询优化器的选择。

serverInfo:提供服务器信息。

executionStats:提供详细的执行统计信息。

关键字段:

winningPlan:显示实际使用的查询计划。

rejectedPlans:显示被拒绝的查询计划。

nReturned:返回的文档数量。

executionTimeMillis:查询执行时间(毫秒)。

totalKeysExamined:扫描的索引键数量。

totalDocsExamined:扫描的文档数量。

nYields:查询过程中让步的次数。

stage:查询执行的不同阶段。

typescript 复制代码
{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "mydatabase.users",
    "indexFilterSet": false,
    "parsedQuery": {
      "name": {
        "$eq": "John"
      }
    },
    "winningPlan": {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": {
          "name": 1,
          "age": -1
        },
        "indexName": "idx_name_age",
        "isMultiKey": false,
        "multiKeyPaths": {
          "name": [],
          "age": []
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": 2,
        "direction": "forward",
        "indexBounds": {
          "name": [
            "[\"John\", \"John\"]"
          ],
          "age": [
            "[MaxKey, MinKey]"
          ]
        }
      }
    },
    "rejectedPlans": []
  },
  "executionStats": {
    "executionSuccess": true,
    "nReturned": 10,
    "executionTimeMillis": 1,
    "totalKeysExamined": 10,
    "totalDocsExamined": 10,
    "executionStages": {
      "stage": "FETCH",
      "nReturned": 10,
      "executionTimeMillisEstimate": 0,
      "works": 11,
      "advanced": 10,
      "needTime": 0,
      "needYield": 0,
      "saveState": 0,
      "restoreState": 0,
      "isEOF": 1,
      "invalidates": 0,
      "docsExamined": 10,
      "alreadyHasObj": 0,
      "inputStage": {
        "stage": "IXSCAN",
        "nReturned": 10,
        "executionTimeMillisEstimate": 0,
        "works": 11,
        "advanced": 10,
        "needTime": 0,
        "needYield": 0,
        "saveState": 0,
        "restoreState": 0,
        "isEOF": 1,
        "invalidates": 0,
        "keyPattern": {
          "name": 1,
          "age": -1
        },
        "indexName": "idx_name_age",
        "isMultiKey": false,
        "multiKeyPaths": {
          "name": [],
          "age": []
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": 2,
        "direction": "forward",
        "indexBounds": {
          "name": [
            "[\"John\", \"John\"]"
          ],
          "age": [
            "[MaxKey, MinKey]"
          ]
        },
        "keysExamined": 10,
        "dupsTested": 0,
        "dupsDropped": 0,
        "seenInvalidated": 0
      }
    }
  },
  "serverInfo": {
    "host": "localhost",
    "port": 27017,
    "version": "4.4.6",
    "gitVersion": "72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7"
  },
  "ok": 1
}

关键分析:
winningPlan.stage: FETCH 表示查询从索引中获取文档。
winningPlan.inputStage.stage: IXSCAN 表示使用了索引扫描。
winningPlan.inputStage.indexName: idx_name_age 表示使用的索引名称。
executionStats.totalKeysExamined: 10 表示扫描了10个索引键。
executionStats.totalDocsExamined: 10 表示扫描了10个文档。
executionStats.executionTimeMillis: 1 表示查询执行时间为1毫秒。
typescript 复制代码
indexStats 命令:
 indexStats 命令可以提供关于索引使用情况的统计信息,帮助你识别哪些索引被频繁使用,哪些索引几乎未被使用
 db.users.aggregate([{ $indexStats: {} }])
 [
  {
    "name": "_id_",
    "key": {
      "_id": 1
    },
    "host": "localhost:27017",
    "accesses": {
      "ops": 1000,
      "since": ISODate("2023-10-01T00:00:00Z")
    }
  },
  {
    "name": "idx_name_age",
    "key": {
      "name": 1,
      "age": -1
    },
    "host": "localhost:27017",
    "accesses": {
      "ops": 500,
      "since": ISODate("2023-10-01T00:00:00Z")
    }
  }
]
关键分析:
name: 索引名称。
key: 索引的键模式。
accesses.ops: 自上次重启以来对该索引的访问次数。
accesses.since: 上次重启的时间。
typescript 复制代码
db.collection.stats()
 stats 方法提供集合的统计信息,包括索引的大小和使用情况。
{
  "ns": "mydatabase.users",
  "size": 1048576,
  "count": 1000,
  "avgObjSize": 1048,
  "storageSize": 2097152,
  "capped": false,
  "nindexes": 2,
  "indexSizes": {
    "_id_": 26112,
    "idx_name_age": 26112
  },
  "totalIndexSize": 52224,
  "indexBuilds": {},
  "ok": 1
}
关键点分析
size: 集合的总大小。
count: 文档数量。
avgObjSize: 平均文档大小。
storageSize: 存储大小。
nindexes: 索引数量。
indexSizes: 每个索引的大小。
totalIndexSize: 所有索引的总大小。

mongodb分析步奏

分析步骤:

  1. 查看现有索引: db.collection.getIndexes() 使用 explain 方法:
  2. db.collection.find({ query }).explain("executionStats") 使用
  3. indexStats 命令: db.collection.aggregate({ $indexStats: {} })
  4. 使用 db.currentOp() 监控当前操作: db.currentOp() 使用 mongostat 和
  5. mongotop 监控工具: mongostat、mongotop 使用 db.collection.validate()
  6. 验证集合完整性: db.collection.validate() 使用 db.collection.stats()
  7. 获取集合统计信息: db.collection.stats() 使用 db.collection.dataSize()
  8. 获取数据大小: db.collection.dataSize() 使用
  9. db.collection.totalIndexSize()
    10.获取索引总大小: db.collection.totalIndexSize()
重建索引

重建集合中所有索引

db.集合名.reIndex()

删除指定索引:db.users.dropIndex("indexName")

重建指定索引:db.users.dropIndex("idx_name")

创建索引

1表示升序,-1表示降序

单字段索引

db.users.createIndex({ name: 1 })

复合索引

db.users.createIndex({ name: 1, age: -1 })

相关推荐
全栈前端老曹4 分钟前
【MongoDB】安全与权限管理 —— 用户认证、角色权限、SSL 加密
前端·javascript·数据库·安全·mongodb·nosql·ssl
怕孤单的草丛23 分钟前
缓存管理面临的主要问题
java·数据库·缓存
我会尽全力 乐观而坚强1 小时前
MySQL零基础入门(二)
数据库·mysql·adb
kali-Myon2 小时前
某校园门禁系统高危 SQL 注入漏洞挖掘复盘
数据库·sql·安全·web安全
程序员在囧途3 小时前
likeadmin-api API 中转站怎么做统一报价?从 /pricing、/user/balance 到 task_id 回执的落地方法
运维·服务器·数据库·likeadmin-api·api中转站·token计费
龙石数据4 小时前
MySQL 全量同步到 Hive 怎么做?三步配置教程
数据库·hive·mysql·数据治理·数据中台
程序员在囧途5 小时前
likeadmin-api API 算力超市怎么做供应商切换?统一鉴权、task_id 和 callback_url 才能稳交付
java·服务器·数据库·开放api·likeadmin-api·api算力超市
数据工匠老o5 小时前
连接池配置实战——从“连不上“到“连太多“的完整排查指南
数据库
一只专注api接口开发的技术猿6 小时前
电商评论自动化监控与情感数据分析完整落地教程(附可直接运行 Python 代码)
大数据·数据库·python·数据分析·自动化
hh真是个慢性子6 小时前
GaussDB Inside 2.23.01.280 集中式一主一备安装
数据库·database·gaussdb·国产数据库·高斯