如何在MongoDB中启用数据压缩

huangapple go评论74阅读模式
英文:

How to enable data compression in MongoDB

问题

在MongoDB中,WiredTiger提供了Zlib压缩选项。

要启用Zlib压缩选项,我使用以下代码创建了一个集合。

db.createCollection("questions", { storageEngine: {
                   wiredTiger: { configString: "blockCompressor=zlib" }}}) 

我运行了一些简单的测试来测量压缩性能,并使用以下数据集来存储字符串。

{
'_id': <ObjectID>,
'question_id': <五个字符的字符串>,
'question': <我的问题>
}

我使用以下代码创建了另一个集合,没有使用任何压缩选项。

db.createCollection("questions")

使用以下命令测量数据大小。

db.stats(1024*1024).dataSize + db.stats(1024*1024).indexSize

但我无法看到这两个集合之间的压缩差异。我参考了以下链接来完成我的过程。

https://www.mongodb.com/blog/post/new-compression-options-mongodb-30

https://scalegrid.io/blog/enabling-data-compression-in-mongodb-3-0/

英文:

In MongoDB, WiredTiger provides the Zlib compression option.

To enable the Zlib compression option, I created a collection using the following code.

db.createCollection( &quot;questions&quot;, { storageEngine: {
                   wiredTiger: { configString: &quot;blockCompressor=zlib&quot; }}})

I ran some simple tests to measure compression performance and used following data sets storing strings

{
&#39;_id&#39;: &lt;ObjectID&gt;,
&#39;question_id&#39;: &lt;Five character string&gt;,
&#39;question&#39;: &lt;My question&gt;
}

I created another one collection with any compression option using the following code.

db.createCollection( &quot;questions&quot;)

Measured the data size using following comment

db.stats(1024*1024).dataSize + db.stats(1024*1024).indexSize

But I can't able to see the compression difference between these two collections. I referred the following links to achieve my process.

https://www.mongodb.com/blog/post/new-compression-options-mongodb-30

https://scalegrid.io/blog/enabling-data-compression-in-mongodb-3-0/

答案1

得分: 6

不要使用 dataSize 进行此比较,因为它是未压缩的大小。请改用 storageSize

以 MongoDB 4.2.2 为例:

// 创建集合
> db.createCollection('snappy')
> db.createCollection('zlib', {storageEngine: {wiredTiger: {configString: 'block_compressor=zlib'}}})

// 向两个集合插入可压缩文档
> doc = {_id:0, text:<一段文本>}
> db.snappy.insert(doc)
> db.zlib.insert(doc)

// 存储大小比较
> db.snappy.stats().storageSize
20480
> db.zlib.stats().storageSize
4096

// 数据大小比较
> db.snappy.dataSize()
697
> db.zlib.dataSize()
697

因此,zlib 的存储大小比默认的 snappy 要小得多,但它们之间的数据大小相同。

注意:如果您仍在使用 MongoDB 3.0,它已经非常过时,并且自 2018 年 2 月 起不再受支持。请使用更新的版本(截止到 2020 年 1 月的最新版本为 4.2.2)。

英文:

Don't use dataSize for this comparison since it's the uncompressed size. Use storageSize instead.

Using MongoDB 4.2.2 for example:

// create collections
&gt; db.createCollection(&#39;snappy&#39;)
&gt; db.createCollection(&#39;zlib&#39;, {storageEngine: {wiredTiger: {configString: &#39;block_compressor=zlib&#39;}}})

// insert a compressible document into both collections
&gt; doc = {_id:0, text:&lt;a paragraph of text&gt;}
&gt; db.snappy.insert(doc)
&gt; db.zlib.insert(doc)

// storage size comparison
&gt; db.snappy.stats().storageSize
20480
&gt; db.zlib.stats().storageSize
4096

// data size comparison
&gt; db.snappy.dataSize()
697
&gt; db.zlib.dataSize()
697

So the storage size of zlib is much smaller than default (snappy), but data size are the same between them.

Note: If you're still using it, MongoDB 3.0 is very much outdated and not supported anymore since February 2018. Please use a more recent version (latest as of Jan 2020 is 4.2.2).

答案2

得分: 0

db.createCollection("questions", { storageEngine: { wiredTiger: { configString: "blockCompressor=zlib" } } })

错误 -- 抛出无效参数

改用

db.createCollection("email", { storageEngine: { wiredTiger: { configString: "block_compressor=zlib" } } })

英文:

db.createCollection( "questions", { storageEngine: {
wiredTiger: { configString: "blockCompressor=zlib" }}})

ERROR -- throws invalid argument

use instead

db.createCollection( "email", { storageEngine: {wiredTiger: { configString: "block_compressor=zlib" }}})

huangapple
  • 本文由 发表于 2020年1月6日 18:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定