博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MONGODB(二)——索引操作
阅读量:2215 次
发布时间:2019-05-07

本文共 1406 字,大约阅读时间需要 4 分钟。

一、

1、插入10w条数据
> for(var i = 0;i<100000;i++){
... var rand = parseInt(i*Math.random());
... db.person.insert({"name":"hxc"+i,"age":i})
... }
2、使用性能分析函数explain
> db.person.find({“name”:”hxc”+1000}).explain()
{
“cursor” : “BasicCursor”, //表扫描,也就是顺序查找
“isMultiKey” : false,
“n” : 1, //返回了一个文档
“nscannedObjects” : 100006, //浏览的文档数
“nscanned” : 100006,
“millis” : 58, //总共耗时58毫秒
}
3、建立索引
> db.person.ensureIndex({“name”:1})
> db.person.find({“name”:”hxc”+1000}).explain()
{
“cursor” : “BtreeCursor name_1″,采用B树结构存储索引,索引名为”name_1″
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 1,
“nscanned” : 1, //!!!!
“nscannedObjectsAllPlans” : 1,
“nscannedAllPlans” : 1,
“nChunkSkips” : 0,
“millis” : 0, //!!!!
}

二、

1、唯一索引
建立唯一索引,重复键值不能输入。唯一索引也不能再已经存在重复键值的表上创建
db.person.ensureIndex({“name”:1},{“unique”:true})。
2、组合索引
> db.person.ensureIndex({“name”:1,”birthday”:1})
> db.person.ensureIndex({“birthday”:1,”name”:1})
查看生成的索引> db.person.getIndexes()
{
“v” : 1,
“key” : {
“name” : 1,
“birthday” : 1
},
“ns” : “test.person”,
“name” : “name_1_birthday_1″
},
{
“v” : 1,
“key” : {
“birthday” : 1,
“name” : 1
},
“ns” : “test.person”,
“name” : “birthday_1_name_1″
}
3、删除索引
> db.person.dropIndexes(“name_1″)
{
“nIndexesWas” : 4,
“msg” : “non-_id indexes dropped for collection”,
“ok” : 1
}

参考

[1] 《8天学通MongoDB》 http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html

转载于:https://www.cnblogs.com/fonxian/p/5091502.html

你可能感兴趣的文章
特征工程怎么做
查看>>
机器学习算法应用中常用技巧-1
查看>>
机器学习算法应用中常用技巧-2
查看>>
通过一个kaggle实例学习解决机器学习问题
查看>>
决策树的python实现
查看>>
Sklearn 快速入门
查看>>
了解 Sklearn 的数据集
查看>>
用ARIMA模型做需求预测
查看>>
推荐系统
查看>>
TensorFlow-11-策略网络
查看>>
浅谈 GBDT
查看>>
如何选择优化器 optimizer
查看>>
一文了解强化学习
查看>>
CART 分类与回归树
查看>>
seq2seq 的 keras 实现
查看>>
seq2seq 入门
查看>>
什么是 Dropout
查看>>
用 LSTM 做时间序列预测的一个小例子
查看>>
用 LSTM 来做一个分类小问题
查看>>
详解 LSTM
查看>>