扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
*****************未加索引******************************************
在弋阳等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都做网站 网站设计制作按需求定制开发,公司网站建设,企业网站建设,品牌网站制作,成都营销网站建设,外贸网站制作,弋阳网站建设费用合理。> var startTime = new Date();
> db.temMinlog.find({"id":0}).count();
1215
> (new Date().getTime()-startTime.getTime())/1000
194.811s
******************加索引*****************************************
> db.temMinlog.ensureIndex({"_id":1,"id":1});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
******************加索引后查询*****************************************
> var startTime = new Date();
> db.temMinlog.find({"id":0}).count();
1215
> (new Date().getTime()-startTime.getTime())/1000
227.247s
时间范围检索
db.temMinlog.find({"_id":{$gte:1498703926535,$lte:1498704749897},"id":0}).explain("executionStats");
结果"executionStats" : {
"executionSuccess" : true,
"nReturned" : 52,
"executionTimeMillis" : 9506,
"totalKeysExamined" : 518000,
"totalDocsExamined" : 52,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 52,
"executionTimeMillisEstimate" : 2175,
"works" : 518001,
"advanced" : 52,
"needTime" : 517947,
"needYield" : 0,
"saveState" : 8151,
"restoreState" : 8151,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 52,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 52,
"executionTimeMillisEstimate" : 2145,
模糊查询
db.temMinlog.find({"_id":{$gte:1498703926535,$lte:1498704749897},"id":/0/}).explain("executionStats");
结果 "executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 9893,
"totalKeysExamined" : 518001,
"totalDocsExamined" : 1,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 1,
"executionTimeMillisEstimate" : 2237,
"works" : 518002,
"advanced" : 1,
"needTime" : 517999,
"needYield" : 0,
"saveState" : 8151,
"restoreState" : 8151,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"filter" : {
"id" : {
"$regex" : "0"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 2185,
模糊count()
> var startTime = new Date();
> db.temMinlog.find({"_id":{$gte:1498703926535,$lte:1498704749897},"id":{$in:[1,2,3]}}).count();
156
> (new Date().getTime()-startTime.getTime())/1000
11.232s
*************************group by 统计*************************************
db.temMinlog.aggregate([{$group:{_id:"$id",num_tutorial:{$sum:1}}}]);
db.temMinlog.aggregate([{$group:{_id:"$id",max_v0:{$max:"$v0"},min_v1:{$min:"$v1"},count:{$sum:1}}}],{ allowDiskUse: true });
db.temMinlog.aggregate([{$group:{_id:"$id",max_v0:{$max:"$v0"},min_v1:{$min:"$v1"},avg_v2:{$avg:"$v2"},count:{$sum:1}}}],{ allowDiskUse: true });
db.temMinlog.aggregate([{$match:{_id:{$gte:1498703926535,$lte:1498704749897}}},{$group:{_id:"$id",max_v0:{$max:"$v0"},min_v1:{$min:"$v1"},avg_v2:{$avg:"$v2"},sum_v2:{$sum:"$v2"},count:{$sum:1}}}],{ allowDiskUse: true });
结果:12.194s
db.temMinlog.aggregate([{$match:{_id:{$gte:1498703926535,$lte:1498704749897}}},{$limit:5},{$group:{_id:"$id",max_v0:{$max:"$v0"},min_v1:{$min:"$v1"},avg_v2:{$avg:"$v2"},sum_v2:{$sum:"$v2"},count:{$sum:1}}},{$sort:{_id:-1}}],{ allowDiskUse: true });
$match:匹配条件,可选
$limit:结果条数,可选
$group:聚合规则
$sort:对查询结果排序,-1表示降序
db.temMinlog.aggregate([{$match:{_id:{$gte:1498703926535,$lte:1498704749897}}},{$group:{_id:"$id",max_v0:{$max:"$v0"},min_v1:{$min:"$v1"},avg_v2:{$avg:"$v2"},sum_v2:{$sum:"$v2"},count:{$sum:1}}},{$limit:5},{$sort:{_id:-1}}]);
结果:21.048s
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流