mongoDB获取数据内存大小
版本 mongodb_windows_64_4.4.6
需要获取mongDB一个查询结果的内存大小。
stats() 函数
mongoDB自带stats()函数,返回有关集合的统计信息。
db.stats() // 执行stats() 函数,获取数据库的集合统计信息
{
"db" : "ate", // 库名
"collections" : 32, // 集合数量
"views" : 0,
"objects" : 868897, // 数据量
"avgObjSize" : 484.1188736984936, // 每条数据平均大小
"dataSize" : 410790.4658203125, // 数据大小
"storageSize" : 91412, // 数据使用磁盘内存大小
"numExtents" : 0,
"indexes" : 32, // 索引
"indexSize" : 10688,
"scaleFactor" : 1024,
"fsUsedSize" : 3467329144,
"fsTotalSize" : 8634469312,
"ok" : 1,
}
stats() 参数
stats() 有四个参数:
参数 | 类型 | 描述 |
---|---|---|
scale | 数值 | 定义数据大小单位,默认为bytes,1024为kb以此类推 |
indexDetails | 布尔 | 除集合统计信息外,还返回index details,默认false |
indexDetailsKey | 数字/字符 | 通常与indexDetails搭配使用,是其索引 |
indexDetailsName | 字符串 | 通常与indexDetails搭配使用,也可以作其索引 |
通常只使用scale参数
stats(1024) // 以kb为单位
stats(1048576) // 以mb为单位
stats(1073741824) // 以gb为单位
获取集合数据内存大小
db[‘集合名’].stats() 或 db.集合名.stats()
db['data'].stats() // 获取data表的集合统计信息
{
"ns" : "ate.data", // 集合名
"size" : 301195747,
"count" : 823570,
"avgObjSize" : 365, // 每条数据平均大小
"storageSize" : 62935746, // 数据使用磁盘内存大小
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
},
"creationString" : "allocation_size=4KB,app_metadata=(formatVersion=1),block_allocation=best,block_compressor=snappy,cache_resident=0,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=0,extractor=,format=btree,huffman_key=,huffman_value=,immutable=0,internal_item_max=0,internal_key_max=0,internal_key_truncate=,internal_page_max=4KB,key_format=q,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=64MB,log=(enabled=),lsm=(auto_throttle=,bloom=,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=0,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=0,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u",
"type" : "file",
"uri" : "statistics:table:collection-2-7253336746667145592",
"LSM" : {
"bloom filters in the LSM tree" : 0,
"bloom filter false positives" : 0,
"bloom filter hits" : 0,
"bloom filter misses" : 0,
"bloom filter pages evicted from cache" : 0,
"bloom filter pages read into cache" : 0,
"total size of bloom filters" : 0,
"sleep for LSM checkpoint throttle" : 0,
"chunks in the LSM tree" : 0,
"highest merge generation in the LSM tree" : 0,
"queries that could have benefited from a Bloom filter that did not exist" : 0,
"sleep for LSM merge throttle" : 0
},
"block-manager" : {
"file allocation unit size" : 4096,
"blocks allocated" : 338,
"checkpoint size" : 4096000,
"allocations requiring file extension" : 338,
"blocks freed" : 0,
"file magic number" : 120897,
"file major version number" : 1,
"minor version number" : 0,
"file bytes available for reuse" : 0,
"file size in bytes" : 4104192
},
"btree" : {
"btree checkpoint generation" : 15,
"column-store variable-size deleted values" : 0,
"column-store fixed-size leaf pages" : 0,
"column-store internal pages" : 0,
"column-store variable-size leaf pages" : 0,
"pages rewritten by compaction" : 0,
"number of key/value pairs" : 0,
"fixed-record size" : 0,
"maximum tree depth" : 3,
"maximum internal page key size" : 368,
"maximum internal page size" : 4096,
"maximum leaf page key size" : 3276,
"maximum leaf page size" : 32768,
"maximum leaf page value size" : 67108864,
"overflow pages" : 0,
"row-store internal pages" : 0,
"row-store leaf pages" : 0
},
"cache" : {
"bytes read into cache" : 9309503,
"bytes written from cache" : 10817368,
"checkpoint blocked page eviction" : 0,
"unmodified pages evicted" : 0,
"page split during eviction deepened the tree" : 0,
"modified pages evicted" : 1,
"data source pages selected for eviction unable to be evicted" : 0,
"hazard pointer blocked page eviction" : 0,
"internal pages evicted" : 0,
"pages split during eviction" : 1,
"in-memory page splits" : 1,
"overflow values cached in memory" : 0,
"pages read into cache" : 287,
"overflow pages read into cache" : 0,
"pages written from cache" : 337
},
"compression" : {
"raw compression call failed, no additional data available" : 0,
"raw compression call failed, additional data available" : 0,
"raw compression call succeeded" : 0,
"compressed pages read" : 287,
"compressed pages written" : 333,
"page written failed to compress" : 0,
"page written was too small to compress" : 4
},
"cursor" : {
"create calls" : 1,
"insert calls" : 25359,
"bulk-loaded cursor-insert calls" : 0,
"cursor-insert key and value bytes inserted" : 10697901,
"next calls" : 76085,
"prev calls" : 1,
"remove calls" : 0,
"cursor-remove key bytes removed" : 0,
"reset calls" : 25959,
"restarted searches" : 0,
"search calls" : 0,
"search near calls" : 594,
"update calls" : 0,
"cursor-update value bytes updated" : 0
},
"reconciliation" : {
"dictionary matches" : 0,
"internal page multi-block writes" : 1,
"leaf page multi-block writes" : 2,
"maximum blocks required for a page" : 47,
"internal-page overflow keys" : 0,
"leaf-page overflow keys" : 0,
"overflow values written" : 0,
"pages deleted" : 0,
"page checksum matches" : 0,
"page reconciliation calls" : 4,
"page reconciliation calls for eviction" : 1,
"leaf page key bytes discarded using prefix compression" : 0,
"internal page key bytes discarded using suffix compression" : 333
},
"session" : {
"object compaction" : 0,
"open cursor count" : 1
},
"transaction" : {
"update conflicts" : 0
}
},
"nindexes" : 4,
"totalIndexSize" : 626688,
"indexSizes" : {
"_id_" : 217088,
"borough_1_cuisine_1" : 139264,
"cuisine_1" : 131072,
"borough_1_address.zipcode_1" : 139264
},
"ok" : 1
}
获取查询结果数据内存大小(获取一个用户的数据大小)
从上述可以得出,stats()只能用于集合,那么获取一个用户的数据只需要创建一个临时集合就可以实现。
db.temp.insert(db.test_plan_c.find(version: 1)); // 创建temp临时集合
db['temp'].stats(1024); // 获取集合统计信息(以kb为单位)
show collections; // 显示所有集合
db.temp.drop(); // 删除临时集合
show collections;