[211119] MongoDB#索引详解

[211119] MongoDB#索引详解

image-20211221141259293

索引的相关术语

什么是查询覆盖(covered query)?

:所需字段都在索引中,无需额外字段的,直接在索引表中就返回数据。

什么是索引扫描(IXSCAN)?

:扫描索引表。(性能更高)

什么是集合扫描(COLLSCAN)?
扫描整个集合。

什么是时间复杂度?

IMG_D8917723C8FD-1

什么是查询形状(Query Shape)?
要查询的哪些字段。

什么是索引前缀(Index Prefix)?

IMG_7335C3292C6E-1

什么是过滤性(Selectivity)

要选择过滤性最强的字段做索引,比如:

假设集合有1万条数据
a字段做索引,过滤得10条。
b字段做索引,过滤的4000条。
那么,若只许一个做索引,则优先a字段。

【记住】过滤性越强,越要优先考虑:

image-20210610125524385

什么是B树结构(一种算法数据结构)?

索引背后是B-树。理解B-树,对正确使用索引有帮助。

什么是执行计划?

执行计划用于检查查询语句的性能,包括索引是否生效,查询扫表情况,执行耗时等。

比如,MongoDB的执行计划可通过 explain() 函数进行查看执行预期

db.collection.explain().<method(...)>
// explain入参:"queryPlanner" (Default) | "executionStats"  |  "allPlansExecution"

索引的几种类型 *

单键索引、组合索引、多值索引、地理位置索引全文索引、TTL索引、部分索引、哈希索引。

索引的设计原则(ESR)

设置组合索引的最佳方式:ESR原则 (必须按此顺序设计组合索引)

  1. 精确(Equal)匹配的字段放最前(考虑过滤性强不强)
  2. 排序(Sort)条件放中间
  3. 范围(Range)匹配的字段放最后

【说明】如果凑不齐ESR原则,ES、ER也可。

复合索引

image-20210325104441735

索引的基本操作

创建、查看、删除、看索引大小,参考这篇

查看索引使用情况

db.文档名.aggregate( [ { $indexStats: { } }, { $sort: { name: 1 } } ] )

【例】使用 {#indexStats:{}} 查看 violation 的索引使用情况,重点关注 accesses.ops即可,该字段能够说明每个索引的使用情况,。(👉🏻 详解 $indexStats):

db.violation.aggregate([{
    $indexStats: {}
}, {
    $sort: {
        name: 1
    }
}])

结果:

// 1
{
    "name": "_id_",
    "key": {
        "_id": NumberInt("1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
      	// 从2021年11月10日 13:37:41到现在,当前索引生效了5千万次左右
        "ops": NumberLong("59562398"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "_id": NumberInt("1")
        },
        "name": "_id_",
        "ns": "governance-center.violation"
    }
}

// 2
{
    "name": "compound_index_desc",
    "key": {
        "carNumCol": NumberInt("-1"),
        "startTime": NumberInt("-1"),
        "workOrderId": NumberInt("1"),
        "violationTypeNo": NumberInt("-1"),
        "companyId": NumberInt("-1"),
        "organizationId": NumberInt("-1"),
        "violationStatusPriority": NumberInt("1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
        "ops": NumberLong("328634"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "carNumCol": NumberInt("-1"),
            "startTime": NumberInt("-1"),
            "workOrderId": NumberInt("1"),
            "violationTypeNo": NumberInt("-1"),
            "companyId": NumberInt("-1"),
            "organizationId": NumberInt("-1"),
            "violationStatusPriority": NumberInt("1")
        },
        "name": "compound_index_desc",
        "ns": "governance-center.violation"
    }
}

// 3
{
    "name": "compound_index_page",
    "key": {
        "startTime": NumberInt("-1"),
        "violationStatusPriority": NumberInt("1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
        "ops": NumberLong("1098"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "startTime": NumberInt("-1"),
            "violationStatusPriority": NumberInt("1")
        },
        "name": "compound_index_page",
        "ns": "governance-center.violation"
    }
}

// 4
{
    "name": "flagStatus_desc",
    "key": {
        "flagStatus": NumberInt("-1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
        "ops": NumberLong("234"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "flagStatus": NumberInt("-1")
        },
        "name": "flagStatus_desc",
        "ns": "governance-center.violation"
    }
}

// 5
{
    "name": "operationType_desc",
    "key": {
        "operationType": NumberInt("-1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
        "ops": NumberLong("0"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "operationType": NumberInt("-1")
        },
        "name": "operationType_desc",
        "ns": "governance-center.violation"
    }
}

// 6
{
    "name": "startTime",
    "key": {
        "startTime": NumberInt("-1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
        "ops": NumberLong("408494"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "startTime": NumberInt("-1")
        },
        "name": "startTime",
        "ns": "governance-center.violation"
    }
}

// 7
{
    "name": "tradeType_desc",
    "key": {
        "tradeType": NumberInt("-1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
        "ops": NumberLong("0"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "tradeType": NumberInt("-1")
        },
        "name": "tradeType_desc",
        "ns": "governance-center.violation"
    }
}

// 8
{
    "name": "violationType_desc",
    "key": {
        "violationType": NumberInt("-1")
    },
    "host": "a4718a113fc2:27017",
    "accesses": {
        "ops": NumberLong("11"),
        "since": ISODate("2021-11-10T05:37:41.52Z")
    },
    "spec": {
        "v": NumberInt("2"),
        "key": {
            "violationType": NumberInt("-1")
        },
        "name": "violationType_desc",
        "ns": "governance-center.violation"
    }
}

不当操作对索引的影响 *

可能导致索引失效

$group对索引的影响

image-20210623172901137

$nin对索引的影响

image-20210621154401597

$nin会发起全表扫描,因此,建议使用$ne替代 $nin

$regex对索引的影响

image-20210617172854769

可以使用前缀表达式可以触发索引。

$or对索引的影响

image-20210617174635363

必须为$or中所有的字段建立索引,否则$or将使索引失效

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值