mongodb 优化

固定集合     如果空间用完 新的值将取代集合中旧的值

创建一个固定集合

db.createCollection('c2',{capped:true,size:1000000,max:5});  大小1M   存5个json数据 
{ "ok" : 1 }
> db.c2.stats();
{
        "ns" : "test.c2",
        "count" : 0,
        "size" : 0,
        "storageSize" : 1000192,
        "numExtents" : 1,
        "nindexes" : 0,
        "lastExtentSize" : 1000192,
        "paddingFactor" : 1,
        "flags" : 0,
        "totalIndexSize" : 0,
        "indexSizes" : {

        },
        "capped" : 1,   //固定集合
        "max" : 5,     // 只能存5个
        "ok" : 1
}

把普通集合  转换成固定集合
db.runCommand({converTocapped:'c1',size:100000,size:5});   

 > show tables;
c1
c2
system.indexes
> db.c2.find();
> db.c2.insert({'name':'user1'});
> db.c2.insert({'name':'user2'});
> db.c2.insert({'name':'user3'});
> db.c2.insert({'name':'user4'});
> db.c2.insert({'name':'user5'});
> db.c2.find();
{ "_id" : ObjectId("55c21166d9c7fa428406b564"), "name" : "user1" }
{ "_id" : ObjectId("55c2116cd9c7fa428406b565"), "name" : "user2" }
{ "_id" : ObjectId("55c2116fd9c7fa428406b566"), "name" : "user3" }
{ "_id" : ObjectId("55c21175d9c7fa428406b567"), "name" : "user4" }
{ "_id" : ObjectId("55c21179d9c7fa428406b568"), "name" : "user5" }
> db.c2.insert({'name':'user6'});
> db.c2.find();                  
{ "_id" : ObjectId("55c2116cd9c7fa428406b565"), "name" : "user2" }
{ "_id" : ObjectId("55c2116fd9c7fa428406b566"), "name" : "user3" }
{ "_id" : ObjectId("55c21175d9c7fa428406b567"), "name" : "user4" }
{ "_id" : ObjectId("55c21179d9c7fa428406b568"), "name" : "user5" }
{ "_id" : ObjectId("55c211a8d9c7fa428406b569"), "name" : "user6" }

只能存5条数据   再多插入  新的数据会取代旧的数据 


//删除一个集合 数据库
> show tables;
c1
c2
system.indexes
> db.c1.drop();
true
> show tables;
c2
system.indexes
> db;               
test
> show dbs;  
admin   (empty)
local   (empty)
test    0.0625GB
> db.dropDatabase(); 
{ "dropped" : "test", "ok" : 1 }
> show dbs;
admin   (empty)
local   (empty)



查看索引信息
db.c1.getIndexKeys();
[ { "_id" : 1 } ]
> db.c1.getIndexes(); 详细查看
[
        {
                "name" : "_id_",
                "ns" : "test.c1",
                "key" : {
                        "_id" : 1
                },
                "v" : 0
        }
]

> db.c1.find({'name':'user5'}).explain();     发现是全表扫描
{
        "cursor" : "BasicCursor",
        "nscanned" : 10,
        "nscannedObjects" : 10,
        "n" : 1,
        "millis" : 0,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : false,
        "indexOnly" : false,
        "indexBounds" : {

        }
}


建立普通索引
> db.c1.ensureIndex({name:1});
> db.c1.getIndexKeys();
[ { "_id" : 1 }, { "name" : 1 } ]   1为升序   -1降序 
当数据量比较大时可以 加上{'background':true}在后台进行

唯一索引  {unique:true}
> db.c1.getIndexKeys();
[ { "_id" : 1 }, { "name" : 1 } ]
> db.c1.ensureIndex({age:1},{unique:true})
> db.c1.getIndexKeys();
[ { "_id" : 1 }, { "name" : 1 }, { "age" : 1 } ]

删除指定索引
> db.c1.dropIndex({age:1});
{ "nIndexesWas" : 3, "ok" : 1 }

删除所有索引
> db.c1.dropIndexes();
{
        "nIndexesWas" : 2,
        "msg" : "non-_id indexes dropped for collection",
        "ok" : 1
}
> db.c1.getIndexKeys();
[ { "_id" : 1 } ]


慢查询日志
0 - 不开启
1 - 记录慢查询命令  (默认大于100ms)
2 - 记录所有命令 

> db.getProfilingLevel();
0
> db.setProfilingLevel(1,1000);   设置1s 就记录慢查询
{ "was" : 0, "slowms" : 100, "ok" : 1 }
// 发现毫秒数没有改过来  可以通过启动的时候更改    mongod -h    下有  --profile arg    0=off 1=slow, 2=all     --slowms arg (=100)
> db.getProfilingLevel();      
1
> 

mongostat  监控





mongodb  备份  
<pre name="code" class="plain">// -d 数据库  -o  备份目录   如果不指定备份目录则在当前目录下创建dump的文件夹     -c  可指定到处具体哪个集合

[root@localhost bin]# ./mongodump -d test -o /tmp/
connected to: 127.0.0.1
DATABASE: test   to     /tmp/test
        test.c1 to /tmp/test/c1.bson
                 11 objects
        test.system.indexes to /tmp/test/system.indexes.bson
                 5 objects
        test.fs.files to /tmp/test/fs.files.bson
                 0 objects
        test.fs.chunks to /tmp/test/fs.chunks.bson
                 0 objects
        test.system.profile to /tmp/test/system.profile.bson
                 0 objects

还原
[root@localhost mongodb]# ./bin/mongorestore -d test /tmp/test/
connected to: 127.0.0.1
Fri Aug  7 01:36:17 /tmp/test/fs.files.bson
Fri Aug  7 01:36:17      going into namespace [test.fs.files]
Fri Aug  7 01:36:17 file /tmp/test/fs.files.bson empty, skipping
Fri Aug  7 01:36:17 /tmp/test/fs.chunks.bson
Fri Aug  7 01:36:17      going into namespace [test.fs.chunks]
Fri Aug  7 01:36:17 file /tmp/test/fs.chunks.bson empty, skipping
Fri Aug  7 01:36:17 /tmp/test/system.profile.bson
Fri Aug  7 01:36:17      skipping
Fri Aug  7 01:36:17 /tmp/test/c1.bson
Fri Aug  7 01:36:17      going into namespace [test.c1]
Fri Aug  7 01:36:17      11 objects found
Fri Aug  7 01:36:17 /tmp/test/system.indexes.bson
Fri Aug  7 01:36:17      going into namespace [test.system.indexes]
Fri Aug  7 01:36:17 { name: "_id_", ns: "test.c1", key: { _id: 1 }, v: 0 }
Fri Aug  7 01:36:18 { name: "_id_", ns: "test.fs.files", key: { _id: 1 }, v: 0 }
Fri Aug  7 01:36:18 { ns: "test.fs.files", key: { filename: 1 }, name: "filename_1", v: 0 }
Fri Aug  7 01:36:18 { name: "_id_", ns: "test.fs.chunks", key: { _id: 1 }, v: 0 }
Fri Aug  7 01:36:18 { ns: "test.fs.chunks", key: { files_id: 1, n: 1 }, name: "files_id_1_n_1", v: 0 }
Fri Aug  7 01:36:18      5 objects found

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值