mongodb 优化
来源:互联网 发布:在淘宝买金坷垃 编辑:程序博客网 时间:2024/06/13 04:13
固定集合 如果空间用完 新的值将取代集合中旧的值
创建一个固定集合
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;c1c2system.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;c1c2system.indexes> db.c1.drop();true> show tables;c2system.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
- MongoDB 优化
- mongodb优化
- mongodb 优化
- mongodb优化
- MongoDB优化
- mongodb优化
- mongodb 性能优化
- mongodb组合索引优化
- mongodb性能优化
- mongodb分页优化
- mongodb查询分页优化
- MongoDB ObjectId的优化
- Mongodb的MapReduce优化
- mongodb的优化
- mongodb 性能优化深入浅出
- java MongoDB分页优化
- MongoDB 优化器profile
- mongodb性能优化
- Strings 在 Swift 2中字符串设计的背后思想
- select 标签的使用
- 雄迈实习日志
- 姓名的“夫妻相”
- Python之正则表达式
- mongodb 优化
- Android Studio You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
- android动画
- 机器学习中的范数规则化之(一)L0、L1与L2范数
- iOS开发 - WaterflowLayout 瀑布流布局
- Ubuntu下Android真机调试
- ClearCase NAS设备上动态view迁移注册到另外一台主机上的流程(UNIX系统)
- 六西格玛游戏
- 多校5-MZL's Border