dbStats

On this page本页内容

Definition定义

dbStats

The dbStats command returns storage statistics for a given database. dbStats命令返回给定数据库的存储统计信息。The command has the following syntax:该命令具有以下语法:

db.runCommand( {
   dbStats: 1,
   scale: <number>,
   freeStorage: 0
} )

The dbStats command takes the following fields:dbStats命令接受以下字段:

Fields领域Description描述
dbStats1
scale

Optional. 可选。The scale factor for the various size data. 各种大小数据的比例因子。The scale defaults to 1 to return size data in bytes. scale默认为1,以字节为单位返回大小数据。To display kilobytes rather than bytes, specify a scale value of 1024.要显示千字节而不是字节,请指定1024scale值。

If you specify a non-integer scale factor, MongoDB uses the integer part of the specified factor. 如果指定非整数比例因子,MongoDB将使用指定因子的整数部分。For example, if you specify a scale factor of 1023.999, MongoDB uses 1023 as the scale factor.例如,如果指定比例因子1023.999,MongoDB将使用1023作为比例因子。

Starting in version 4.2, the output includes the scaleFactor used to scale the size values.从4.2版开始,输出包括用于缩放大小值的scaleFactor

freeStorage

Optional. 可选。To return details on free space allocated to collections, set freeStorage to 1.要返回分配给集合的可用空间的详细信息,请将freeStorage设置为1。

If the instance has a large number of collections or indexes, obtaining free space usage data may cause processing delays. 如果实例具有大量集合或索引,则获取可用空间使用数据可能会导致处理延迟。To gather dbStats information without free space details, either set freeStorage to 0 or do not include the field.要集合没有可用空间详细信息的dbStats信息,请将freeStorage设置为0或不包含该字段。

In mongosh, the db.stats() function provides a wrapper around dbStats.mongosh中,db.stats()函数提供了dbStats的包装器。

Behavior行为

The time required to run the command depends on the total size of the database. 运行该命令所需的时间取决于数据库的总大小。Because the command must touch all data files, the command may take several seconds to run.由于该命令必须接触所有数据文件,因此该命令可能需要几秒钟才能运行。

Accuracy after Unexpected Shutdown意外停机后的精度

After an unclean shutdown of a mongod using the Wired Tiger storage engine, count and size statistics reported by dbStats may be inaccurate.在使用Wired Tiger存储引擎的mongod不干净地关闭后,dbStats报告的计数和大小统计数据可能不准确。

The amount of drift depends on the number of insert, update, or delete operations performed between the last checkpoint and the unclean shutdown. 漂移量取决于在最后一个检查点和不干净关闭之间执行的插入、更新或删除操作的数量。Checkpoints usually occur every 60 seconds. 检查点通常每60秒出现一次。However, mongod instances running with non-default --syncdelay settings may have more or less frequent checkpoints.然而,使用非默认--syncdelay设置运行的mongod实例可能或多或少有检查点。

Run validate on each collection on the mongod to restore statistics after an unclean shutdown.mongod上的每个集合上运行validate,以在不干净的关闭后恢复统计数据。

After an unclean shutdown:不干净停机后:

Replica Set Member State Restriction副本集成员状态限制

Starting in MongoDB 4.4, to run on a replica set member, dbStats operations require the member to be in PRIMARY or SECONDARY state. 从MongoDB 4.4开始,要在副本集成员上运行,dbStats操作要求成员处于PRIMARYSECONDARY状态。If the member is in another state, such as STARTUP2, the operation errors.如果成员处于其他状态,如STARTUP2,则操作将出错。

In previous versions, the operations can also be run when the member is in STARTUP2. 在以前的版本中,也可以在成员处于STARTUP2时运行操作。However, the operations wait until the member transitions to RECOVERING.但是,操作将等待成员转换到RECOVERING

Output输出

dbStats.db

Name of the database.数据库的名称。

dbStats.collections

Number of collections in the database.数据库中的集合数。

dbStats.views

Number of views in the database.数据库中的视图数。

dbStats.objects

Number of objects (specifically, documents) in the database across all collections.所有集合中数据库中的对象(特别是文档)数量。

dbStats.avgObjSize

Average size of each document in bytes. 每个文档的平均大小(字节)。This is the dataSize divided by the number of documents. 这是dataSize除以文档数。The scale argument does not affect the avgObjSize value.scale参数不影响avgObjSize值。

dbStats.dataSize

Total size of the uncompressed data held in the database. 数据库中保存的未压缩数据的总大小。The dataSize decreases when you remove documents.删除文档时,dataSize会减小。

For databases using the WiredTiger storage engine, dataSize may be larger than storageSize if compression is enabled. 对于使用WiredTiger存储引擎的数据库,如果启用压缩,dataSize可能大于storageSizeThe dataSize decreases when documents shrink.文档收缩时,dataSize会减小。

dbStats.storageSize

Sum of the space allocated to all collections in the database for document storage, including free space.分配给数据库中用于文档存储的所有集合的空间总和,包括可用空间。

The storageSize does not decrease as you remove or shrink documents. storageSize不会随着删除或收缩文档而减小。This value may be smaller than dataSize for databases using the WiredTiger storage engine with compression enabled.对于使用启用压缩的WiredTiger存储引擎的数据库,此值可能小于dataSize

storageSize does not include space allocated to indexes. 不包括分配给索引的空间。See indexSize for the total index size.有关总索引大小,请参阅indexSize

dbStats.freeStorageSize

Sum of the free space allocated to all collections in the database for document storage. 分配给数据库中用于文档存储的所有集合的可用空间的总和。Free database storage space is allocated to the collection but does not contain data.已将可用数据inventory储空间分配给集合,但不包含数据。

freeStorageSize does not include free space allocated to indexes. 不包括分配给索引的可用空间。See indexFreeStorageSize for the total free index size.请参阅indexFreeStorageSize以了解总可用索引大小。

To include this value in the dbStats output, set freeStorage to 1.要在dbStats输出中包含此值,请将freeStorage设置为1。

Updated in version 5.3.0, 5.2.1, and 5.0.6.在版本5.3.0、5.2.1和5.0.6中更新。

dbStats.indexes

Total number of indexes across all collections in the database.数据库中所有集合的索引总数。

dbStats.indexSize

Sum of the space allocated to all indexes in the database, including free index space.分配给数据库中所有索引的空间总和,包括可用索引空间。

dbStats.indexFreeStorageSize

Sum of the free space allocated to all indexes in the database. 分配给数据库中所有索引的可用空间的总和。Free database storage space is allocated to the index but does not contain data.可用数据inventory储空间分配给索引,但不包含数据。

indexFreeStorageSize does not include free space allocated to document storage. 不包括分配给文档存储的可用空间。See freeStorageSize for the total free document storage size.请参阅freeStorageSize以了解可用文档存储的总大小。

To include this value in the dbStats output, set freeStorage to 1.要在dbStats输出中包含此值,请将freeStorage设置为1。

Updated in version 5.3.0, 5.2.1, and 5.0.6.在版本5.3.0、5.2.1和5.0.6中更新。

dbStats.totalSize

Sum of the space allocated for both documents and indexes in all collections in the database. 为数据库中所有集合中的文档和索引分配的空间总和。Includes used and free storage space. 包括已用和可用存储空间。This is the sum of storageSize and indexSize.这是storageSizeindexSize的总和。

New in version 4.4.在版本4.4中新增

dbStats.totalFreeStorageSize

Sum of the free storage space allocated for both documents and indexes in all collections in the database. 为数据库中所有集合中的文档和索引分配的可用存储空间的总和。This is the sum of freeStorageSize and indexFreeStorageSize.这是freeStorageSizeindexFreeStorageSize的总和。

To include this value in the dbStats output, set freeStorage to 1.要在dbStats输出中包含此值,请将freeStorage设置为1。

Updated in version 5.3.0, 5.2.1, and 5.0.6.在版本5.3.0、5.2.1和5.0.6中更新。

dbStats.scaleFactor

scale value used by the command.命令使用的值。

If you specified a non-integer scale factor, MongoDB uses the integer part of the specified factor. 如果指定了非整数比例因子,MongoDB将使用指定因子的整数部分。For example, if you specify a scale factor of 1023.999, MongoDB uses 1023 as the scale factor.例如,如果指定比例因子1023.999,MongoDB将使用1023作为比例因子。

New in version 4.2.在版本4.2中新增

dbStats.fsUsedSize

Total size of all disk space in use on the filesystem where MongoDB stores data.MongoDB存储数据的文件系统上使用的所有磁盘空间的总大小。

Tip提示
See also: 参阅:
dbStats.fsTotalSize

Total size of all disk capacity on the filesystem where MongoDB stores data.MongoDB存储数据的文件系统上所有磁盘容量的总大小。

Examples示例

The following examples demonstrate dbStats usage.以下示例演示了dbStats的用法。

Limit Data Returned返回的限制数据

To limit the data returned to a single field, append the field name to the dbStats command. 要将返回的数据限制为单个字段,请将字段名附加到dbStats命令。This example returns the indexSize value:此示例返回indexSize值:

db.runCommand( { dbStats: 1 } ).indexSize

View Free Space Allocated to Collections查看分配给集合的可用空间

To view free storage usage, set freeStorage to 1.要查看可用存储使用情况,请将freeStorage设置为1。

db.runCommand( { dbStats: 1, scale: 1024, freeStorage: 1 } )

Example output:示例输出:

{
  db: 'test',
  collections: 2,
  views: 0,
  objects: 1689,
  avgObjSize: 52.56542332741267,
  dataSize: 86.7021484375,
  storageSize: 100,
  freeStorageSize: 32,
  indexes: 2,
  indexSize: 116,
  indexFreeStorageSize: 36,
  totalSize: 216,
  totalFreeStorageSize: 68,
  scaleFactor: 1024,
  fsUsedSize: 60155820,
  fsTotalSize: 61255492,
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1646085664, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1646085664, i: 1 })
}

The freeStorage field enables the collection and display of the highlighted metrics.freeStorage字段用于集合和显示突出显示的度量。

The scale field sets the displayed values to kilobytes.scale字段将显示的值设置为千字节。

←  dbHashdiagLogging →