簡體   English   中英

我應該如何構建我的 MongoDB 復合索引?

[英]How should I structure my MongoDB compond index?

我有一個由以下字段組成的 mongo 圖像元數據集合:camera_name(str)、photographer_name(str)、resolution(str)、image_size(int in MB, rounded) 和 timestamp(10 digit UNIX timestamp)

我只想運行 2 個查詢:

  1. 給定 camera_name,返回時間戳 <= 1639457261 的記錄(示例 UNIX 時間戳)。 記錄必須按降序排序
  2. 給定相機名稱、攝影師名稱、分辨率、圖像大小和時間戳,我想檢索記錄,按輸入的時間戳的降序排序。

我創建了 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 and timestamp<=1503582086 應該返回 4 條記錄
  2. camera_name='Fuji',photographer_name='Beth', resolution='1920x1080', image_size='5' and timestamp<=1900000000 應該返回 2 條記錄,但我得到 0 條記錄

索引不會“過濾”結果,它們允許您通過掃描索引樹而不是掃描原始文檔來更快地訪問數據。

這意味着如果第二個查詢“不返回任何內容”,它與您構建的任何索引都無關,但您使用的實際查詢與數據庫中的任何文檔都不匹配。

我還將提到您的第二個索引可能會更小(取決於某些假設,如規模和數據分布),這可以幫助更新/插入性能,同時額外減少存儲大小。 但是,從原始數據的外觀來看,我認為這些並不是您的緊迫考慮。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM