# MongoDB教育訓練-07

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

[![image-1639709435352.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/wWPaTmNWtK43dfFl-image-1639709435352.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/wWPaTmNWtK43dfFl-image-1639709435352.png)

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

[![image-1639709740460.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/BNDz6MjJGH4XQVMy-image-1639709740460.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/BNDz6MjJGH4XQVMy-image-1639709740460.png)

- 索引增加讀的效能，減低寫的效能
- 建議每張表的索引&lt; 10
- 新增資料會做索引異動，更新不會(但更新欄位會)

[![image-1639710154313.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/p9ksssT9foFnXp7U-image-1639710154313.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/p9ksssT9foFnXp7U-image-1639710154313.png)

索引越大，記憶體越大

[![image-1639710316926.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/4zOuCSYrDtGgfnf8-image-1639710316926.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/4zOuCSYrDtGgfnf8-image-1639710316926.png)

### 前綴壓縮

- <span style="color: #ff0000;">App</span> le
- <span style="color: #ff0000;">App</span> le iphon
- <span style="color: #ff0000;">App</span> le store

[![image-1639710384084.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/rrhpi4p8v1TQjucP-image-1639710384084.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/rrhpi4p8v1TQjucP-image-1639710384084.png)

### Multikey indexs

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

[![image-1639710538365.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/4jvxLsF5Zv6B6JrQ-image-1639710538365.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/4jvxLsF5Zv6B6JrQ-image-1639710538365.png)

[![image-1639710636236.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/xbwrPXNGHcrCzpeC-image-1639710636236.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/xbwrPXNGHcrCzpeC-image-1639710636236.png)

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

[![image-1639710866463.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/kQG5DV39z1YWECuY-image-1639710866463.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/kQG5DV39z1YWECuY-image-1639710866463.png)

[![image-1639711096453.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/EMgtxyUFyCFNrcC0-image-1639711096453.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/EMgtxyUFyCFNrcC0-image-1639711096453.png)

### Compound符合索引

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

[![image-1639711185981.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/1fStpr6XArU4nsF0-image-1639711185981.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/1fStpr6XArU4nsF0-image-1639711185981.png)

### Compound Indexes 複合索引

- MongoDB 通常使用<span style="color: #ff0000;">一個</span>索引作查詢
- 最多可以做32個
- 順序很重要

[![image-1639711281834.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/bFZZMWa76Rs0PgBT-image-1639711281834.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/bFZZMWa76Rs0PgBT-image-1639711281834.png)

[![image-1639711459539.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/gWtjM19sgBjXu1vs-image-1639711459539.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/gWtjM19sgBjXu1vs-image-1639711459539.png)

欄位順序必須相同

[![image-1639711603003.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/FPft7ie7e0KvO9md-image-1639711603003.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/FPft7ie7e0KvO9md-image-1639711603003.png)

[![image-1639711744724.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/EbJ0MfYoAaSPFIRg-image-1639711744724.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/EbJ0MfYoAaSPFIRg-image-1639711744724.png)

[![image-1639711763345.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/V53vAeoi5saMlNUs-image-1639711763345.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/V53vAeoi5saMlNUs-image-1639711763345.png)

[![image-1639711833183.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/NXkdKdxjZW2po5PQ-image-1639711833183.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/NXkdKdxjZW2po5PQ-image-1639711833183.png)

[![image-1639711867625.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/MiTyDRZgKHj8WhZM-image-1639711867625.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/MiTyDRZgKHj8WhZM-image-1639711867625.png)

[![image-1639711980197.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/PO3Xs0ElcRvrZxDH-image-1639711980197.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/PO3Xs0ElcRvrZxDH-image-1639711980197.png)

[![image-1639712284049.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/omFQETIWY1NDNVfY-image-1639712284049.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/omFQETIWY1NDNVfY-image-1639712284049.png)

[![image-1639712401606.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/PjRvnyiSx4W82DeT-image-1639712401606.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/PjRvnyiSx4W82DeT-image-1639712401606.png)

### ＥＳＲ原則

等值 排序 範圍

10000以內不算數據量大

[![image-1639712527794.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/X2qvWrX7SXv1CsYz-image-1639712527794.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/X2qvWrX7SXv1CsYz-image-1639712527794.png)

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

[![image-1639712629336.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/7gfZXdRRZGTKJXQj-image-1639712629336.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/7gfZXdRRZGTKJXQj-image-1639712629336.png)

###  

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

\_id 不要出現, multikey 不適用

[![image-1639712718185.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/U5rt5J6oLLv4cPWC-image-1639712718185.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/U5rt5J6oLLv4cPWC-image-1639712718185.png)

[![image-1639712827734.png](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/scaled-1680-/QwW6VDciHMXheDrv-image-1639712827734.png)](https://bookstack.treemanou.com/uploads/images/gallery/2021-12/QwW6VDciHMXheDrv-image-1639712827734.png)

```shell
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})
```