跳到主內容

MongoDB教育訓練-06

 

image-1639705033770.png

image-1639705137852.png

mongoDB 使用B-tree索引

image-1639705482607.png

查詢至少要用到索引,不要full table scan(所以要至少建立可用索引)

image-1639705535799.png

image-1639705770711.png

檢查執行計畫快取 -> (無) 有可能優化的索引找出來,同時跑看誰的效率快,加入快取 

更新快取時機:

  • 當前索引變慢(系統判斷)
  • 當新的索引被創建
  • server 重啟(memory 清除)

image-1639705807311.png

  • 需要指定欄位
  • 欄位可以是任何類型

image-1639706094542.png

image-1639706346750.png

Explain 執行計畫

  • queryPlanner : 只顯示執行計畫
  • executionStats(最常用): 會把最優執行計畫執行一遍,並收集相關資訊
  • allPlansExecution:把所有執行計畫執行一遍,並收集相關資訊

image-1639706656145.png

  • nReturn: 返回數量
  • totalKeyExamined :找到索引
  • totalDocExamined : 查找document數量
  • stage:COLLSAN => 無index
  • totalKeyExamined / nReturn 最好小於1000

image-1639706909824.png

number_of_reviews: 1 (1: 升冪,-1:降冪)

image-1639707147638.png

通過index 取得資訊:FETCH

image-1639707345619.png

  • nReturn:totalKeyExamined:totalDocExamined 1:1:1 最佳情況

 

image-1639707445597.png

 

image-1639707511649.png

 

image-1639707565143.png

取得所有索引

image-1639707652623.png

刪除索引

image-1639707687385.png

image-1639707697169.png

 

需要放得下index的記憶體(最低要求)

image-1639707720558.png

 

db.tablename.stats(檔案除以?(1024 => KB,102481024 => MB)).indexSizes

image-1639707935930.png

 

image-1639708000892.png

image-1639708353823.png