Permalink: 2014-02-21 11:52:05 by ning in mongo tags: all mongo

测试代码:

t1 = db.storage_size;

test = function(cnt, obj, index){
    t1.drop()
    t1.ensureIndex(index);

    docs1 = new Array();
    for (var i = 0; i < cnt; ++ i){
        docs1[i] = obj;
    }

    t1.insert(docs1);
    print('db.stats() ' + cnt + ' ' + tojson(obj));
    printjson(t1.stats());
    stats = t1.stats();
    print( 'NOTICE: ' + cnt + ' ' + tojson(obj) + ' record_size:' +  stats["avgObjSize"]  + ' index_size: ' + stats["indexSizes"]["x_1"] / 1000 + ' ' +  stats["indexSizes"]["xxxxxxxxxx_1"] / 1000 );
    tojson(t1.stats());
}

test(1, {x: 1}, {x:1});
test(1, {x: 1024*1024*1024*8}, {x:1});
test(1, {x: ''}, {x:1});
test(1, {x: 'a'}, {x:1});
test(1, {x: 'aaa'}, {x:1});
test(1, {x: '好'}, {x:1});
test(1, {x: '中国'}, {x:1});
test(1, {x: '北京市'}, {x:1});
test(1, {x: '我爱北京天安门'}, {x:1});

print( 'NOTICE: ---------------------------- ' );

test(1000, {x: 1}, {x:1});
test(1000, {x: 1024*1024*1024*8}, {x:1});
test(1000, {x: ''}, {x:1});
test(1000, {x: 'a'}, {x:1});
test(1000, {x: 'aaa'}, {x:1});
test(1000, {x: '好'}, {x:1});
test(1000, {x: '中国'}, {x:1});
test(1000, {x: '北京市'}, {x:1});
test(1000, {x: '我爱北京天安门'}, {x:1});

print( 'NOTICE: ---------------------------- ' );

test(1000, {xxxxxxxxxx: 1}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: 1024*1024*1024*8}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: ''}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: 'a'}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: 'aaa'}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: '好'}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: '中国'}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: '北京市'}, {xxxxxxxxxx:1});
test(1000, {xxxxxxxxxx: '我爱北京天安门'}, {xxxxxxxxxx:1});

...

结果:

NOTICE: 1 { "x" : 1 } record_size:36 index_size: 8.176 NaN
NOTICE: 1 { "x" : 8589934592 } record_size:36 index_size: 8.176 NaN
NOTICE: 1 { "x" : "" } record_size:32 index_size: 8.176 NaN
NOTICE: 1 { "x" : "a" } record_size:32 index_size: 8.176 NaN
NOTICE: 1 { "x" : "aaa" } record_size:36 index_size: 8.176 NaN
NOTICE: 1 { "x" : "好" } record_size:36 index_size: 8.176 NaN
NOTICE: 1 { "x" : "中国" } record_size:36 index_size: 8.176 NaN
NOTICE: 1 { "x" : "北京市" } record_size:40 index_size: 8.176 NaN
NOTICE: 1 { "x" : "我爱北京天安门" } record_size:52 index_size: 8.176 NaN
NOTICE: ----------------------------
NOTICE: 1000 { "x" : 1 } record_size:36.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : 8589934592 } record_size:36.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "" } record_size:32 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "a" } record_size:32 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "aaa" } record_size:36.008 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "好" } record_size:36.008 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "中国" } record_size:36.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "北京市" } record_size:40.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "我爱北京天安门" } record_size:52.02 index_size: 57.232 NaN
NOTICE: ----------------------------
NOTICE: 1000 { "xxxxxxxxxx" : 1 } record_size:44.012 index_size: NaN 40.88
NOTICE: 1000 { "xxxxxxxxxx" : 8589934592 } record_size:44.012 index_size: NaN 40.88
NOTICE: 1000 { "xxxxxxxxxx" : "" } record_size:40.008 index_size: NaN 32.704
NOTICE: 1000 { "xxxxxxxxxx" : "a" } record_size:40.008 index_size: NaN 32.704
NOTICE: 1000 { "xxxxxxxxxx" : "aaa" } record_size:44.012 index_size: NaN 32.704
NOTICE: 1000 { "xxxxxxxxxx" : "好" } record_size:44.012 index_size: NaN 32.704
NOTICE: 1000 { "xxxxxxxxxx" : "中国" } record_size:48.032 index_size: NaN 40.88
NOTICE: 1000 { "xxxxxxxxxx" : "北京市" } record_size:48.032 index_size: NaN 40.88
NOTICE: 1000 { "xxxxxxxxxx" : "我爱北京天安门" } record_size:60 index_size: NaN 57.232
NOTICE: ----------------------------
NOTICE: 1000 { "x" : "" } record_size:32 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "a" } record_size:32 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "aa" } record_size:32 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "aaa" } record_size:36.008 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "aaaa" } record_size:36.008 index_size: 32.704 NaN
NOTICE: 1000 { "x" : "aaaaa" } record_size:36.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "aaaaaa" } record_size:36.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "aaaaaaa" } record_size:40.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "aaaaaaaa" } record_size:40.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "aaaaaaaaa" } record_size:40.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "aaaaaaaaaa" } record_size:40.008 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "aaaaaaaaaaa" } record_size:44.012 index_size: 40.88 NaN
NOTICE: 1000 { "x" : "aaaaaaaaaaaa" } record_size:44.012 index_size: 40.88 NaN
NOTICE: ----------------------------
NOTICE: 1000 { "x" : 1 } record_size:36.008 index_size: 40.88 NaN
NOTICE: 1000 { "xx" : 1 } record_size:36.008 index_size: NaN NaN
NOTICE: 1000 { "xxx" : 1 } record_size:36.008 index_size: NaN NaN
NOTICE: 1000 { "xxxx" : 1 } record_size:36.008 index_size: NaN NaN
NOTICE: 1000 { "xxxxx" : 1 } record_size:40.008 index_size: NaN NaN
NOTICE: 1000 { "xxxxxx" : 1 } record_size:40.008 index_size: NaN NaN
NOTICE: 1000 { "xxxxxxx" : 1 } record_size:40.008 index_size: NaN NaN
NOTICE: 1000 { "xxxxxxxx" : 1 } record_size:40.008 index_size: NaN NaN
NOTICE: 1000 { "xxxxxxxxx" : 1 } record_size:44.012 index_size: NaN NaN

1   结论

  1. 整数:
    • 小整数(1) 和长整数(8589934592) 占用字节数一样
  2. 字符串:
    • 字符串按照4字节对齐: aaa, aaaa, aaaaa, aaaaaa 占用存储一样
    • key 和value 都是4字节对齐 所以这里建议key<4字节即可
  3. 整数vs字符串:
    • 短字符(0,1,2字节)串占用存储小于 整数, 3字节的字符串存储大小和整数一样, 3字节以上字符串存储大于整数.
  4. 中文:
    • 每个中文字符(utf8)占用存储3字节.
  5. key:
    • key越长, 占用存储越大, key长度不影响索引大小.

1.1   mongo中如何存long

mongo中所有的数字都是都是按照 double (8 byte) 存的,

int64 的范围中, 2^52 以下表达为double是不损失精度的, 2^52以上, 表达为double就会丢失精度, 所以如果需要存int64, 就需要显示使用NumberLong("2090845886852")

注意int64是带符号的.

Comments