跳到主內容

MongoDB教育訓練-07

  • 只能保證分片唯一,不能保證全局唯一
  • sparse index : 欄位可能不存在document中使用
  • partial index : 依據欄位的值決定要不要放入索引(刪除的不放)

 

image-1639709435352.png

  • hashed indexes : md5 取前20個byte,並不可作為唯一索引,使用時機:控制索引大小(如url)
  • 關於時間不適合,會造成分散在所有分片

 

image-1639709740460.png

  • 索引增加讀的效能,減低寫的效能
  • 建議每張表的索引< 10
  • 新增資料會做索引異動,更新不會(但更新欄位會)

image-1639710154313.png

索引越大,記憶體越大

image-1639710316926.png

前綴壓縮

  • App le
  • App le iphon
  • App le store

image-1639710384084.png

Multikey indexs

建在array上的index (是不是由系統判斷)

image-1639710538365.png

image-1639710636236.png

通常使用"comments.rating" : 1 這種索引

image-1639710866463.png

image-1639711096453.png

Compound符合索引

多欄位索引 (欄位A &  欄位B 查詢適用 )

image-1639711185981.png

Compound Indexes 複合索引

  • MongoDB 通常使用一個索引作查詢
  • 最多可以做32個
  • 順序很重要

image-1639711281834.png

image-1639711459539.png

欄位順序必須相同

image-1639711603003.png

image-1639711744724.png

image-1639711763345.png

image-1639711833183.png

image-1639711867625.png

image-1639711980197.png

 

image-1639712284049.png

 

image-1639712401606.png

ESR原則

等值 排序 範圍

10000以內不算數據量大

image-1639712527794.png

兩欄位都是陣列,禁止使用,因為所有數量是陣列的乘積

image-1639712629336.png

 

index 完全覆蓋查詢(資料都從index返回,不用直接查詢document)

_id 不要出現, multikey 不適用

image-1639712718185.png

image-1639712827734.png

db.listingsAndReviews.find({amenities:"Waterfront","bed_type":{$in:["Futon","Real Bed"]}})
.projection({bedrooms:1, price:1,_id:0,"address.country":1}).explain("executionStats")


db.listingsAndReviews.createIndex({amenities:1, bedrooms:-1,price:11,bed_type:1,first_review:1,last_review:1})