a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

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

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

2020-10-29 18:46:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

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

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

Memeory-Mapped Files

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

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

MongoDB的存儲(chǔ)模型

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

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

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

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