自动压缩mongodb中已删除的空间? [英] Auto compact the deleted space in mongodb?

查看:143
本文介绍了自动压缩mongodb中已删除的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongodb文档说

The mongodb document says that

要压缩此空间,请从mongo shell运行db.repairDatabase()(请注意此操作将阻止并且运行缓慢).

To compact this space, run db.repairDatabase() from the mongo shell (note this operation will block and is slow).

http://www.mongodb.org/display/DOCS/Excessive+Disk+Space

我想知道如何使mongodb免费删除磁盘空间自动吗?

I wonder how to make the mongodb free deleted disk space automatically ?

p.s.我们在mongodb中存储了多达20GB的许多下载任务,并在半小时内完成了这些任务.

p.s. We stored many downloading task in mongodb, up to 20GB, and finished these in half an hour.

推荐答案

通常,如果您不需要收缩数据文件,则根本不应该收缩它们.这是因为在磁盘上增长"数据文件是一项相当昂贵的操作,而MongoDB可以在数据文件中分配的空间越大,碎片就越少.

In general if you don't need to shrink your datafiles you shouldn't shrink them at all. This is because "growing" your datafiles on disk is a fairly expensive operation and the more space that MongoDB can allocate in datafiles the less fragmentation you will have.

因此,您应该尝试为数据库提供尽可能多的磁盘空间.

So, you should try to provide as much disk-space as possible for the database.

但是如果必须缩小数据库,则应牢记两点.

However if you must shrink the database you should keep two things in mind.

  1. MongoDB通过以下方式扩展其数据文件: 加倍,因此数据文件可能是 64MB,然后是128MB,依此类推,最高可达2GB( 它停止加倍到 将文件保留到2GB.)

  1. MongoDB grows it's data files by doubling so the datafiles may be 64MB, then 128MB, etc up to 2GB (at which point it stops doubling to keep files until 2GB.)

与大多数任何数据库一样... 做收缩等操作 需要安排一个单独的工作来 这样做,没有自动收缩" MongoDB.实际上主要的noSQL数据库 (讨厌那个名字)只有里亚克 将自动收缩.因此,您需要 使用您的操作系统创建工作 调度程序运行缩水.您可以使用bash脚本,或者让工作运行php脚本等.

As with most any database ... to do operations like shrinking you'll need to schedule a separate job to do so, there is no "autoshrink" in MongoDB. In fact of the major noSQL databases (hate that name) only Riak will autoshrink. So, you'll need to create a job using your OS's scheduler to run a shrink. You could use an bash script, or have a job run a php script, etc.

服务器端Javascript

您可以使用服务器端Javascript进行收缩,并通过作业(例如cron或Windows调度服务)通过mongo的外壳在常规基础上运行JS ...

You can use server side Javascript to do the shrink and run that JS via mongo's shell on a regular bases via a job (like cron or the windows scheduling service) ...

假设一个名为 foo 的集合,您将下面的javascript保存到名为 bar.js 的文件中并运行...

Assuming a collection called foo you would save the javascript below into a file called bar.js and run ...

$ mongo foo bar.js

JavaScript文件看起来像...

The javascript file would look something like ...

// Get a the current collection size.
var storage = db.foo.storageSize();
var total = db.foo.totalSize();

print('Storage Size: ' + tojson(storage));

print('TotalSize: ' + tojson(total));

print('-----------------------');
print('Running db.repairDatabase()');
print('-----------------------');

// Run repair
db.repairDatabase()

// Get new collection sizes.
var storage_a = db.foo.storageSize();
var total_a = db.foo.totalSize();

print('Storage Size: ' + tojson(storage_a));
print('TotalSize: ' + tojson(total_a));

这将运行并返回类似...的内容

This will run and return something like ...

MongoDB shell version: 1.6.4
connecting to: foo
Storage Size: 51351
TotalSize: 79152
-----------------------
Running db.repairDatabase()
-----------------------
Storage Size: 40960
TotalSize: 65153

按计划运行(无高峰时段),您就可以参加了.

Run this on a schedule (during none peak hours) and you are good to go.

上限收藏集

不过,还有一个选择,有上限的收藏集.

However there is one other option, capped collections.

上限集合是固定大小的 具有很高的收藏 性能自动FIFO老化功能 (有效期取决于插入顺序). 它们有点像"RRD"概念 如果您熟悉的话.

Capped collections are fixed sized collections that have a very high performance auto-FIFO age-out feature (age out is based on insertion order). They are a bit like the "RRD" concept if you are familiar with that.

此外,收藏上限 自动,高性能 维持广告的插入顺序 集合中的对象;这是 在某些用例中非常强大 例如日志记录.

In addition, capped collections automatically, with high performance, maintain insertion order for the objects in the collection; this is very powerful for certain use cases such as logging.

基本上,您可以将集合的大小(或其中的文档数)限制为.. 20GB,一旦达到该限制,MongoDB将开始丢弃最旧的记录,并在出现新记录时将它们替换为新记录.

Basically you can limit the size of (or number of documents in ) a collection to say .. 20GB and once that limit is reached MongoDB will start to throw out the oldest records and replace them with newer entries as they come in.

这是保存大量数据,随着时间的流逝丢弃旧数据并保持相同数量的磁盘空间的好方法.

This is a great way to keep a large amount of data, discarding the older data as time goes by and keeping the same amount of disk-space used.

这篇关于自动压缩mongodb中已删除的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆