【问题标题】:mongodb schema design queries to include lookupsmongodb 模式设计查询以包括查找
【发布时间】:2023-03-18 10:58:01
【问题描述】:

我在一次采访中被问到这个问题,但不知道答案。 “您正在设计一个 monogdb 架构来支持包含查找的查询。您应该怎么做?”

选项是

Create a multicolumn index on 
    a)  key value used as primary and foreign key
   b) foreign key and most unique column
[ b was my answer because i thought it would avoid collisions. Intereviewer disagreed, didn't tell me the answer.]
OR-
c) Create an index on the key value used as the foreign key
d) Create an index on the key value used as primary key

面试官不会告诉我更多信息。关于分贝。就像他在看书一样。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    我应该选择“c”。不太确定,但让我分享一下我的想法。

    假设你有两个集合,你会在 collection2 的 customer_id 上查找 collection1 的 customer_id,MongoDB 会在后端做什么?

    对于collection1 中的每个customer_id,它会尝试在collection2 中找到匹配项,它可以扫描collection2 中的每个文档或使用包含所需customer_id 的文档的记忆图。如果 MongoDB 确切知道在哪里可以找到等效的 customer_id,它就不需要扫描任何文档。

    但是为什么不在主键字段上创建索引呢?因为它不会被使用,因为 MongoDB 无论如何都会遍历集合 1 中的每个文档。

    这些链接可能会给你一些方向:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 2023-03-09
      • 2018-06-07
      • 1970-01-01
      • 2021-11-19
      • 2015-06-24
      相关资源
      最近更新 更多