麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

深入了解MongoDB是如何存儲數(shù)據(jù)的

2020-10-29 18:46:18
字體:
供稿:網(wǎng)友

前言

本文主要介紹了關(guān)于MongoDB存儲數(shù)據(jù)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:

想要深入了解MongoDB如何存儲數(shù)據(jù)之前,有一個概念必須清楚,那就是Memeory-Mapped Files。

Memeory-Mapped Files

下圖展示了數(shù)據(jù)庫是如何跟底層系統(tǒng)打交道的。

  • 內(nèi)存映射文件是OS通過mmap在內(nèi)存中創(chuàng)建一個數(shù)據(jù)文件,這樣就把文件映射到一個虛擬內(nèi)存的區(qū)域。
  • 虛擬內(nèi)存對于進(jìn)程來說,是一個物理內(nèi)存的抽象,尋址空間大小為2^64
  • 操作系統(tǒng)通過mmap來把進(jìn)程所需的所有數(shù)據(jù)映射到這個地址空間(紅線),然后再把當(dāng)前需要處理的數(shù)據(jù)映射到物理內(nèi)存(灰線)
  • 當(dāng)進(jìn)程訪問某個數(shù)據(jù)時,如果數(shù)據(jù)不在虛擬內(nèi)存里,觸發(fā)page fault,然后OS從硬盤里把數(shù)據(jù)加載進(jìn)虛擬內(nèi)存和物理內(nèi)存
  • 如果物理內(nèi)存滿了,觸發(fā)swap-out操作,這時有些數(shù)據(jù)就需要寫回磁盤,如果是純粹的內(nèi)存數(shù)據(jù),寫回swap分區(qū),如果不是就寫回磁盤。

MongoDB的存儲模型

  • 有了內(nèi)存映射文件,要訪問的數(shù)據(jù)就好像都在內(nèi)存里面,簡單化了MongoDB訪問和修改數(shù)據(jù)的邏輯
  • MongoDB讀寫都只是和虛擬內(nèi)存打交道,剩下都交給OS打理
  • 虛擬內(nèi)存大小=所有文件大小+其他一些開銷(連接,堆棧)
  • 如果journal開啟,虛擬內(nèi)存大小差不多翻番
  • 使用MMF的好處1:不用自己管理內(nèi)存和磁盤調(diào)度2:LRU策略3:重啟過程中,Cache依然在。
  • 使用MMF的壞處1:RAM使用會受磁盤碎片的影響,高預(yù)讀也會影響2:無法自己優(yōu)化調(diào)度算法,只能使用LRU

  • 磁盤上的文件是有extent構(gòu)成,分配集合空間的時候也是以extent為單位進(jìn)行分配的
  • 一個集合有一個或者多個etent
  • ns文件里面命名空間記錄指向那個集合的第一個extent

數(shù)據(jù)文件與空間分配

當(dāng)創(chuàng)建數(shù)據(jù)庫時(其實MongoDB沒有顯式創(chuàng)建數(shù)據(jù)庫的方法,在向數(shù)據(jù)庫中的集合寫入數(shù)據(jù)時會自動創(chuàng)建該數(shù)據(jù)庫),MongoDB會在磁盤上分配一組數(shù)據(jù)文件,所有集合,索引和數(shù)據(jù)庫的其他元數(shù)據(jù)都保存在這些文件里。數(shù)據(jù)文件被放在啟動時指定的dbpath里,默認(rèn)放入/data/db下面。典型的一個文件組織結(jié)構(gòu)如下:

$ cat /data/db$ ls -al-rw------- 1 root root 16777216 09-18 00:54 local.ns-rw------- 1 root root 67108864 09-18 00:54 local.0-rw------- 1 root root 2146435072 09-18 00:55 local.1-rw------- 1 root root 2146435072 09-18 00:56 local.2-rw------- 1 root root 2146435072 09-18 00:57 local.3-rw------- 1 root root 2146435072 09-18 00:58 local.4-rw------- 1 root root 2146435072 09-18 00:59 local.5-rw------- 1 root root 2146435072 09-18 01:01 local.6-rw------- 1 root root 2146435072 09-18 01:02 local.7-rw------- 1 root root 2146435072 09-18 01:03 local.8-rw------- 1 root root 2146435072 09-18 01:04 local.9-rw------- 1 root root 2146435072 09-18 01:05 local.10-rw------- 1 root root 16777216 09-18 01:06 test.ns-rw------- 1 root root 67108864 09-18 01:06 test.0-rw------- 1 root root 134217728 09-18 01:06 test.1-rw------- 1 root root 268435456 09-18 01:06 test.2-rw------- 1 root root 536870912 09-18 01:06 test.3-rw------- 1 root root 1073741824 09-18 01:07 test.4-rw------- 1 root root 2146435072 09-18 01:07 test.5-rw------- 1 root root 2146435072 09-18 01:09 test.6-rw------- 1 root root 2146435072 09-18 01:11 test.7-rw------- 1 root root 2146435072 09-18 01:13 test.8...-rwxr-xr-x 1 root root  6 09-18 13:54 mongod.lockdrwxr-xr-x 2 root root 4096 11-13 18:39 journaldrwxr-xr-x 2 root root 4096 11-13 19:02 _tmp