【问题标题】:How should I structure my MongoDB compond index?我应该如何构建我的 MongoDB 复合索引?
【发布时间】:2022-01-17 11:24:35
【问题描述】:

我有一个 mongo 图像元数据集合,包含以下字段:camera_name(str)、photographer_name(str)、resolution(str)、image_size(int in MB, rounded) 和 timestamp(10 digit UNIX timestamp)

我只想运行 2 个查询:

  1. 给定 camera_name,返回时间戳
  2. 给定 camera_name、photographer_name、resolution、image_size 和 timestamp,我想检索记录,按输入的时间戳降序排列。

我创建了 2 个索引:

  1. { "camera_name": 1, "timestamp": -1 }
  2. { "camera_name": 1, "photographer_name": 1, "resolution": 1, "image_size": 1, "timestamp": -1}

第一个索引有效,但是当我对第二个索引运行查询时,没有返回任何记录。我确信集合中存在记录,并且我希望在运行第二个查询时至少获得 10 条记录,但它返回一个空列表。

索引的配置方式有问题吗?谢谢

这里是示例数据:

{"camera_name": "Nikon", "photographer_name": "Aaron", "resolution": "1920x1080", "image_size": "3", "timestamp": 1397232415}
{"camera_name": "Nikon", "photographer_name": "Paul", "resolution": "1920x1080", "image_size": "4", "timestamp": 1717286853}
{"camera_name": "Nikon", "photographer_name": "Beth", "resolution": "720x480", "image_size": "1", "timestamp": 1503582086}
{"camera_name": "Nikon", "photographer_name": "Aaron", "resolution": "1920x1080", "image_size": "4", "timestamp": 1500628458}
{"camera_name": "Nikon", "photographer_name": "Paul", "resolution": "1920x1080", "image_size": "6", "timestamp": 1407580951}
{"camera_name": "Canon", "photographer_name": "Beth", "resolution": "1920x1080", "image_size": "5", "timestamp": 1166049453}
{"camera_name": "Canon", "photographer_name": "Paul", "resolution": "720x480", "image_size": "2", "timestamp": 1086317569}
{"camera_name": "Canon", "photographer_name": "Beth", "resolution": "720x480", "image_size": "1", "timestamp": 1400638926}
{"camera_name": "Canon", "photographer_name": "Aaron", "resolution": "720x480", "image_size": "1", "timestamp": 1345248762}
{"camera_name": "Canon", "photographer_name": "Paul", "resolution": "1920x1080", "image_size": "5", "timestamp": 1462360853}
{"camera_name": "Fuji", "photographer_name": "Beth", "resolution": "720x480", "image_size": "2", "timestamp": 1815298047}
{"camera_name": "Fuji", "photographer_name": "Shane", "resolution": "720x480", "image_size": "3", "timestamp": 1666493455}
{"camera_name": "Fuji", "photographer_name": "Beth", "resolution": "1920x1080", "image_size": "5", "timestamp": 1846677247}
{"camera_name": "Fuji", "photographer_name": "Beth", "resolution": "1920x1080", "image_size": "5", "timestamp": 1630996389}
{"camera_name": "Fuji", "photographer_name": "Shane", "resolution": "720x480", "image_size": "2", "timestamp": 1816829362}

我执行的查询:

  1. camera_name=Nikon 和时间戳
  2. camera_name='Fuji' ,photographer_name='Beth', resolution='1920x1080', image_size='5' and timestamp

【问题讨论】:

  • 索引不会阻止结果。索引提高了查询的速度。如果您没有得到任何结果,则要么数据不符合预期,要么查询不符合预期。此条件与索引规范无关。
  • 由于您没有提供示例数据,也没有提供查询,我无法帮助您解决结果问题。
  • @barrypicker 我已经编辑了问题并添加了示例数据和查询及其预期输出
  • 感谢您提供示例数据。这有点帮助。您尚未提供查询。您已经提供了查询的目的。有时实施是问题所在。我同意您的结果与您的意图不符。请提供实际查询。

标签: mongodb pymongo mongodb-indexes compound-index


【解决方案1】:

索引不会“过滤”结果,它们允许您通过扫描索引树而不是扫描原始文档来更快地访问数据。

这意味着如果第二个查询“不返回任何内容”,则它与您构建的任何索引都无关,但您使用的实际查询与数据库中的任何文档都不匹配。

我还要提到您的第二个索引可能会更小(取决于某些假设,例如规模和数据分布),这可以帮助更新/插入性能,同时额外减少存储大小。但是,从原始数据的外观来看,我认为这些并不是您的紧迫考虑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2015-02-10
    • 2021-10-18
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多