On this page本页内容
This document addresses common questions regarding MongoDB's storage system.本文档介绍了有关MongoDB存储系统的常见问题。
A storage engine is the part of a database that is responsible for managing how data is stored, both in memory and on disk. Many databases support multiple storage engines, where different engines perform better for specific workloads. 存储引擎是数据库的一部分,负责管理数据在内存和磁盘中的存储方式。许多数据库支持多个存储引擎,其中不同的引擎对于特定的工作负载表现更好。For example, one storage engine might offer better performance for read-heavy workloads, and another might support a higher throughput for write operations.例如,一个存储引擎可能为读取繁重的工作负载提供更好的性能,而另一个可能为写入操作提供更高的吞吐量。
Yes. You can have replica set members that use different storage engines (WiredTiger and in-memory)对。您可以拥有使用不同存储引擎(WiredTiger和内存中)的副本集成员
Starting in version 4.2, MongoDB removes the deprecated MMAPv1 storage engine.从版本4.2开始,MongoDB删除了不推荐使用的MMAPv1存储引擎。
Yes. See:对。参阅:
The ratio of compressed data to uncompressed data depends on your data and the compression library used. 压缩数据与未压缩数据的比率取决于您的数据和使用的压缩库。By default, collection data in WiredTiger use Snappy block compression; zlib and zstd compression is also available. 默认情况下,WiredTiger中的采集数据使用Snappy块压缩;zlib和zstd压缩也可用。Index data use prefix compression by default.默认情况下,索引数据使用前缀压缩。
With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.使用WiredTiger,MongoDB利用WiredTier内部缓存和文件系统缓存。
Starting in MongoDB 3.4, the default WiredTiger internal cache size is the larger of either:从MongoDB 3.4开始,默认WiredTiger内部缓存大小为以下两者中的较大值:
For example, on a system with a total of 4GB of RAM the WiredTiger cache will use 1.5GB of RAM (例如,在总共有4GB RAM的系统上,WiredTiger缓存将使用1.5GB的0.5 * (4 GB - 1 GB) = 1.5 GB
). 0.5 * (4 GB - 1 GB) = 1.5 GB
。Conversely, a system with a total of 1.25 GB of RAM will allocate 256 MB to the WiredTiger cache because that is more than half of the total RAM minus one gigabyte (相反,总RAM为1.25GB的系统将为WiredTiger缓存分配256MB,因为这是总RAM的一半以上减去1GB(0.5 * (1.25 GB - 1 GB) = 128 MB < 256 MB
).0.5 * (1.25 GB - 1 GB) = 128 MB < 256 MB
)。
In some instances, such as when running in a container, the database can have memory constraints that are lower than the total system memory. 在某些情况下,例如在容器中运行时,数据库的内存约束可能低于系统总内存。In such instances, this memory limit, rather than the total system memory, is used as the maximum RAM available.在这种情况下,此内存限制而不是整个系统内存被用作可用的最大RAM。
To see the memory limit, see 要查看内存限制,请参阅hostInfo.system.memLimitMB
.hostInfo.system.memLimitMB
。
By default, WiredTiger uses Snappy block compression for all collections and prefix compression for all indexes. 默认情况下,WiredTiger对所有集合使用快照块压缩,对所有索引使用前缀压缩。Compression defaults are configurable at a global level and can also be set on a per-collection and per-index basis during collection and index creation.压缩默认值可以在全局级别配置,也可以在集合和索引创建期间按每个集合和每个索引设置。
Different representations are used for data in the WiredTiger internal cache versus the on-disk format:WiredTiger内部缓存中的数据与磁盘格式中的数据使用不同的表示方式:
Via the filesystem cache, MongoDB automatically uses all free memory that is not used by the WiredTiger cache or by other processes.通过文件系统缓存,MongoDB自动使用WiredTiger缓存或其他进程未使用的所有可用内存。
To adjust the size of the WiredTiger internal cache, see 要调整WiredTiger内部缓存的大小,请参阅storage.wiredTiger.engineConfig.cacheSizeGB
and --wiredTigerCacheSizeGB
. storage.wiredTiger.engineConfig.cacheSizeGB
和--wiredTigerCacheSizeGB
。Avoid increasing the WiredTiger internal cache size above its default value.避免将WiredTiger内部缓存大小增加到其默认值以上。
The storage.wiredTiger.engineConfig.cacheSizeGB
limits the size of the WiredTiger internal cache. storage.wiredTiger.engineConfig.cacheSizeGB
限制WiredTiger内部缓存的大小。The operating system will use the available free memory for filesystem cache, which allows the compressed MongoDB data files to stay in memory. 操作系统将使用可用的空闲内存进行文件系统缓存,这允许压缩的MongoDB数据文件保留在内存中。In addition, the operating system will use any free RAM to buffer file system blocks and file system cache.此外,操作系统将使用任何空闲RAM来缓冲文件系统块和文件系统缓存。
To accommodate the additional consumers of RAM, you may have to decrease WiredTiger internal cache size.为了适应RAM的额外消耗,您可能必须减小WiredTiger内部缓存大小。
The default WiredTiger internal cache size value assumes that there is a single 默认的WiredTiger内部缓存大小值假定每台机器有一个mongod
instance per machine. mongod
实例。If a single machine contains multiple MongoDB instances, then you should decrease the setting to accommodate the other 如果一台机器包含多个MongoDB实例,则应减少设置以容纳其他mongod
instances.mongod
实例。
If you run 如果您在无法访问系统中所有可用RAM的容器(如mongod
in a container (e.g. lxc
, cgroups
, Docker, etc.) that does not have access to all of the RAM available in a system, you must set storage.wiredTiger.engineConfig.cacheSizeGB
to a value less than the amount of RAM available in the container. lxc
、cgroups
、Docker等)中运行mongod
,则必须将storage.wiredTiger.engineConfig.cacheSizeGB
设置为小于容器中可用RAM数量的值。The exact amount depends on the other processes running in the container. 确切数量取决于容器中运行的其他进程。See 请参阅memLimitMB
.memLimitMB
。
To view statistics on the cache and eviction rate, see the 要查看缓存和收回率的统计信息,请参阅wiredTiger.cache
field returned from the serverStatus
command.serverStatus
命令返回的wiredTiger.cache
字段。
For replica set members (primary and secondary members),对于副本集成员(主要和次要成员),
If there are operations waiting for oplog entries. 如果有操作等待oplog条目。Operations that can wait for oplog entries include:可以等待oplog条目的操作包括:
If a write operation includes or implies a write concern of 如果写入操作包含或暗示j: true
.j: true
的写入关注。
Write concern 如果"majority"
implies j: true
if the writeConcernMajorityJournalDefault
is true.writeConcernMajorityJournalDefault
为true
,则写关注"majority"
意味着j:true
。
storage.journal.commitIntervalMs
).storage.journal.commitIntervalMs
)。The WiredTiger storage engine maintains lists of empty records in data files as it deletes documents. WiredTiger存储引擎在删除文档时维护数据文件中的空记录列表。This space can be reused by WiredTiger, but will not be returned to the operating system unless under very specific circumstances.WiredTiger可以重用此空间,但除非在非常特殊的情况下,否则不会将其返回到操作系统。
The amount of empty space available for reuse by WiredTiger is reflected in the output of WiredTiger可重用的空空间量反映在标题db.collection.stats()
under the heading wiredTiger.block-manager.file bytes available for reuse
.wiredTiger.block-manager.file bytes available for reuse
下的db.collection.stats()
输出中。
To allow the WiredTiger storage engine to release this empty space to the operating system, you can de-fragment your data file. 要允许WiredTiger存储引擎将此空白空间释放给操作系统,您可以对数据文件进行分段。This can be achieved using the 这可以使用compact
command. compact
命令实现。For more information on its behavior and other considerations, see 有关其行为和其他注意事项的更多信息,请参阅compact
.compact
。
To view the statistics for a collection, including the data size, use the 要查看集合的统计信息,包括数据大小,请使用db.collection.stats()
method from within mongosh
. mongosh
中的db.collection.stats()
方法。The following example issues 以下示例为db.collection.stats()
for the orders
collection:orders
集合发出db.collection.stats()
:
db.orders.stats();
MongoDB also provides the following methods to return specific sizes for the collection:MongoDB还提供了以下方法来返回集合的特定大小:
db.collection.dataSize()
db.collection.storageSize()
default for WiredTiger
), the storage size reflects the compressed size and may be smaller than the value returned by db.collection.dataSize()
.WiredTiger
的默认值),则存储大小反映了压缩的大小,可能小于db.collection.dataSize()
返回的值。db.collection.totalIndexSize()
default for WiredTiger
), the returned size reflects the compressed size.WiredTiger
的默认值),则返回的大小反映压缩的大小。The following script prints the statistics for each database:以下脚本打印每个数据库的统计信息:
db.adminCommand("listDatabases").databases.forEach(function (d) { mdb = db.getSiblingDB(d.name); printjson(mdb.stats()); })
The following script prints the statistics for each collection in each database:以下脚本打印每个数据库中每个集合的统计信息:
db.adminCommand("listDatabases").databases.forEach(function (d) { mdb = db.getSiblingDB(d.name); mdb.getCollectionNames().forEach(function(c) { s = mdb[c].stats(); printjson(s); }) })
To view the size of the data allocated for each index, use the 要查看为每个索引分配的数据大小,请使用db.collection.stats()
method and check the indexSizes
field in the returned document.db.collection.stats()
方法并检查返回文档中的indexSizes
字段。
If an index uses prefix compression (which is the 如果索引使用前缀压缩(这是WiredTiger的默认值),则该索引返回的大小将反映压缩的大小。default for WiredTiger
), the returned size for that index reflects the compressed size.
The db.stats()
method in mongosh
returns the current state of the "active" database. mongosh
中的db.stats()
方法返回“活动”数据库的当前状态。For the description of the returned fields, see dbStats Output.有关返回字段的说明,请参阅dbStats输出。