设为首页 收藏本站
查看: 1011|回复: 0

[经验分享] mongoDB各种查询方式执行效率测试

[复制链接]

尚未签到

发表于 2015-7-8 00:23:58 | 显示全部楼层 |阅读模式
  最近正在使用mongoDB,使用官方的C# Drivers,对于不同的方式执行的效率(时间)不一样的。
  下面我们开始进入主题 :
  启动本地mongoDB数据库



> mongod -dbpath data
  
  现在数据库有一个User表,大约有100万行数据。



> db.myuser.count()
> 1000005
  
  其中,age字段已设置为索引



> db.entities.ensureIndex({"age":1})
  
  
  我们一起来统计某些用户的最大年龄,符合条件的有21845条记录



> db.entities.find({"age":{$lt:12}})
> 21845
  
  
  以下使用c# drivers的方式:
  第一种方式:



TestMethod(testCount, () =>
{
var querty = Query.LT(c => c.Age, 12);
var collection1 = database.GetCollection("entities");
collection1.Find(querty).Max(c => c.Age);
});
  语句监控:



{ "op" : "query", "ns" : "test.entities", "query" : { "age" : { "$lt" : 12 } }, "cursorid" : 9328463997967889, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 102, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : 321, "w" : 0 }, "timeAcquiringMicros" : { "r" : 5, "w" : 2 } }, "nreturned" : 101, "responseLength" : 7595, "millis" : 0, "ts" : { "$date" : 1370402901213 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : { "age" : { "$lt" : 12 } }, "cursorid" : 9328463997967889, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 34, "lockStats" : { "timeLockedMicros" : { "r" : 61549, "w" : 0 }, "timeAcquiringMicros" : { "r" : 57279, "w" : 2 } }, "nreturned" : 21744, "responseLength" : 1630820, "millis" : 59, "ts" : { "$date" : 1370402901309 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
  执行结果:test total:10 max:2251.1288ms min:1061.0607ms avg:1791.50248ms
  
  第二种方式 :



TestMethod(testCount, () =>
{
var querty = Query.LT(c => c.Age, 12);
var collection2 = database.GetCollection("entities");
(from a in collection2.Find(querty) select a).Max(c => c.Age);
});
  语句监控:



{ "op" : "query", "ns" : "test.entities", "query" : { "age" : { "$lt" : 12 } }, "cursorid" : 10027822751969126, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 102, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : 316, "w" : 0 }, "timeAcquiringMicros" : { "r" : 3, "w" : 2 } }, "nreturned" : 101, "responseLength" : 7595, "millis" : 0, "ts" : { "$date" : 1370403065009 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : { "age" : { "$lt" : 12 } }, "cursorid" : 10027822751969126, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 153, "lockStats" : { "timeLockedMicros" : { "r" : 81511, "w" : 0 }, "timeAcquiringMicros" : { "r" : 166793, "w" : 2 } }, "nreturned" : 21744, "responseLength" : 1630820, "millis" : 167, "ts" : { "$date" : 1370403065220 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
  执行结果:test total:10 max:2318.1326ms min:1224.07ms avg:1843.20543ms
  
  第三种方式:



            TestMethod(testCount, () =>
{
var querty = Query.LT(c => c.Age, 12);
var collection2 = database.GetCollection("entities");
(from a in collection2.Find(querty).ToList() select a).Max(c => c.Age);
});
  语句监控:



{ "op" : "query", "ns" : "test.entities", "query" : { "age" : { "$lt" : 12 } }, "cursorid" : 10180173527525485, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 102, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : 338, "w" : 0 }, "timeAcquiringMicros" : { "r" : 5, "w" : 3 } }, "nreturned" : 101, "responseLength" : 7595, "millis" : 0, "ts" : { "$date" : 1370403100720 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : { "age" : { "$lt" : 12 } }, "cursorid" : 10180173527525485, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 71, "lockStats" : { "timeLockedMicros" : { "r" : 71335, "w" : 0 }, "timeAcquiringMicros" : { "r" : 89772, "w" : 3 } }, "nreturned" : 21744, "responseLength" : 1630820, "millis" : 93, "ts" : { "$date" : 1370403100862 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
  执行结果:test total:10 max:2349.1344ms min:1079.0618ms avg:1824.10434ms
  
  第四种方式:



            TestMethod(testCount, () =>
{
var collection3 = database.GetCollection("entities");
(from a in collection3.FindAll() where a.Age < 12 select a).Max(c => c.Age);
});
  语句监控:



{ "op" : "query", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 102, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : 101, "w" : 0 }, "timeAcquiringMicros" : { "r" : 4, "w" : 2 } }, "nreturned" : 101, "responseLength" : 7595, "millis" : 0, "ts" : { "$date" : 1370403130218 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 208, "lockStats" : { "timeLockedMicros" : { "r" : 36522, "w" : 0 }, "timeAcquiringMicros" : { "r" : 224943, "w" : 2 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 225, "ts" : { "$date" : 1370403130487 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 354, "lockStats" : { "timeLockedMicros" : { "r" : 69849, "w" : 0 }, "timeAcquiringMicros" : { "r" : 372646, "w" : 6 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 373, "ts" : { "$date" : 1370403131520 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 263, "lockStats" : { "timeLockedMicros" : { "r" : 57171, "w" : 0 }, "timeAcquiringMicros" : { "r" : 275690, "w" : 8 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 287, "ts" : { "$date" : 1370403132514 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 118, "lockStats" : { "timeLockedMicros" : { "r" : 42943, "w" : 0 }, "timeAcquiringMicros" : { "r" : 126115, "w" : 8 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 142, "ts" : { "$date" : 1370403133385 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 118, "lockStats" : { "timeLockedMicros" : { "r" : 54060, "w" : 0 }, "timeAcquiringMicros" : { "r" : 139954, "w" : 4 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 140, "ts" : { "$date" : 1370403134246 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 182, "lockStats" : { "timeLockedMicros" : { "r" : 49463, "w" : 0 }, "timeAcquiringMicros" : { "r" : 192366, "w" : 8 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 205, "ts" : { "$date" : 1370403135151 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 233, "lockStats" : { "timeLockedMicros" : { "r" : 47115, "w" : 0 }, "timeAcquiringMicros" : { "r" : 241311, "w" : 6 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 253, "ts" : { "$date" : 1370403136126 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 374, "lockStats" : { "timeLockedMicros" : { "r" : 63443, "w" : 0 }, "timeAcquiringMicros" : { "r" : 390961, "w" : 5 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 391, "ts" : { "$date" : 1370403137212 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 220, "lockStats" : { "timeLockedMicros" : { "r" : 49987, "w" : 0 }, "timeAcquiringMicros" : { "r" : 226603, "w" : 7 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 246, "ts" : { "$date" : 1370403138151 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 174, "lockStats" : { "timeLockedMicros" : { "r" : 51115, "w" : 0 }, "timeAcquiringMicros" : { "r" : 189192, "w" : 7 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 195, "ts" : { "$date" : 1370403139076 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : 12058, "w" : 0 }, "timeAcquiringMicros" : { "r" : 4, "w" : 5 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 12, "ts" : { "$date" : 1370403139769 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 58, "lockStats" : { "timeLockedMicros" : { "r" : 19060, "w" : 0 }, "timeAcquiringMicros" : { "r" : 59983, "w" : 5 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 69, "ts" : { "$date" : 1370403140528 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 80, "lockStats" : { "timeLockedMicros" : { "r" : 47191, "w" : 0 }, "timeAcquiringMicros" : { "r" : 99542, "w" : 4 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 99, "ts" : { "$date" : 1370403141285 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 220, "lockStats" : { "timeLockedMicros" : { "r" : 48911, "w" : 0 }, "timeAcquiringMicros" : { "r" : 228575, "w" : 6 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 245, "ts" : { "$date" : 1370403142248 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 233, "lockStats" : { "timeLockedMicros" : { "r" : 58459, "w" : 0 }, "timeAcquiringMicros" : { "r" : 251144, "w" : 4 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 254, "ts" : { "$date" : 1370403143181 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 182, "lockStats" : { "timeLockedMicros" : { "r" : 54177, "w" : 0 }, "timeAcquiringMicros" : { "r" : 200802, "w" : 4 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 203, "ts" : { "$date" : 1370403144116 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 115, "lockStats" : { "timeLockedMicros" : { "r" : 36174, "w" : 0 }, "timeAcquiringMicros" : { "r" : 121411, "w" : 8 } }, "nreturned" : 55924, "responseLength" : 4194320, "millis" : 136, "ts" : { "$date" : 1370403144950 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
{ "op" : "getmore", "ns" : "test.entities", "query" : {}, "cursorid" : 10306109926130251, "ntoreturn" : 0, "keyUpdates" : 0, "numYield" : 306, "lockStats" : { "timeLockedMicros" : { "r" : 38883, "w" : 0 }, "timeAcquiringMicros" : { "r" : 315199, "w" : 6 } }, "nreturned" : 49191, "responseLength" : 3689345, "millis" : 321, "ts" : { "$date" : 1370403145984 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
  执行结果:test total:10 max:87233.9895ms min:70624.0395ms avg:81305.95043ms
  
  第五种方式:



            TestMethod(testCount, () =>
{
var collection3 = database.GetCollection("entities");
(from a in collection3.AsQueryable() where a.Age < 12 select a).Max(c => c.Age);
});
  语句监控:



{ "op" : "query", "ns" : "test.entities", "query" : { "$query" : { "age" : { "$lt" : 12 } }, "$orderby" : { "age" : -1 } }, "ntoreturn" : 1, "ntoskip" : 0, "nscanned" : 1, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : 216, "w" : 0 }, "timeAcquiringMicros" : { "r" : 4, "w" : 3 } }, "nreturned" : 1, "responseLength" : 95, "millis" : 0, "ts" : { "$date" : 1370403374870 }, "client" : "127.0.0.1", "allUsers" : [], "user" : "" }
  执行结果:test total:10 max:449.0256ms min:327.0187ms avg:407.5233ms
  
  测试完毕,我们一起讨论一下结果:



第一种方式:test total:10 max:2251.1288ms min:1061.0607ms avg:1791.50248ms
第二种方式:test total:10 max:2318.1326ms min:1224.07ms avg:1843.20543ms
第三种方式:test total:10 max:2349.1344ms min:1079.0618ms avg:1824.10434ms
第四种方式:test total:10 max:87233.9895ms min:70624.0395ms avg:81305.95043ms
第五种方式:test total:10 max:449.0256ms min:327.0187ms avg:407.5233ms
  第一种,第二种和第三种是差不多的。
  第四种是最慢的,把全部的查找出来当然是最慢的。
  第五种是最快的,我反复测试了很多次,真的有点不敢相信。
  结论:建议尽量使用第五种方式;不建议使用第四种方式。
  
  以上是从使用的角度来测试。如有不同观点,支持回复指点,谢谢。
  
  以上测试的源码:


DSC0000.gif DSC0001.gif


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDBTest
{
class Program
{
static void Main(string[] args)
{
TestMongoDB();
Console.ReadKey();
}
///
/// 测试数据库
///
public static void TestMongoDB()
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test");
var testCount = 10;
TestMethod(testCount, () =>
{
var querty = Query.LT(c => c.Age, 12);
var collection1 = database.GetCollection("entities");
collection1.Find(querty).Max(c => c.Age);
});
//TestMethod(testCount, () =>
//{
//    var querty = Query.LT(c => c.Age, 12);
//    var collection2 = database.GetCollection("entities");
//    (from a in collection2.Find(querty) select a).Max(c => c.Age);
//});
//TestMethod(testCount, () =>
//{
//    var querty = Query.LT(c => c.Age, 12);
//    var collection2 = database.GetCollection("entities");
//    (from a in collection2.Find(querty).ToList() select a).Max(c => c.Age);
//});
//TestMethod(testCount, () =>
//{
//    var collection3 = database.GetCollection("entities");
//    (from a in collection3.FindAll() where a.Age < 12 select a).Max(c => c.Age);
//});
//TestMethod(testCount, () =>
//{
//    var collection3 = database.GetCollection("entities");
//    (from a in collection3.AsQueryable() where a.Age < 12 select a).Max(c => c.Age);
//});

}
///
/// 测试方法
///
///
///
private static void TestMethod(int testCount, Action fun)
{
var list = new List();
for (int i = 0; i < testCount; i++)
{
new System.Threading.Thread(new System.Threading.ThreadStart(() =>
{
var sd = DateTime.Now;
fun.Invoke();
list.Add(DateTime.Now.Subtract(sd).TotalMilliseconds);
if (list.Count() >= testCount)
{
var max = (from a in list select a).Max();
var min = (from a in list select a).Min();
var avg = (from a in list select a).Average();
var result = string.Format("test total:{0} max:{1}ms min:{2}ms avg:{3}ms", testCount, max, min, avg);
WriteText(result);
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
})).Start();
}
}
///
/// 写文件文本
///
///
private static void WriteText(string result)
{
var f = new System.IO.StreamWriter("result.txt", false);
f.WriteLine(result);
f.Close();
}
}
public class User
{
public ObjectId Id { get; set; }
[BsonElement("name")]
public string Name { get; set; }
[BsonElement("age")]
public int Age { get; set; }
[BsonElement("pwd")]
public string Pwd { get; set; }
[BsonIgnoreIfNull]
[BsonElement("addr")]
public string Address { get; set; }
[BsonIgnoreIfDefault]
[BsonElement("wei")]
public int Weight { get; set; }
}
}
View Code   
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-84145-1-1.html 上篇帖子: Windows 无法启动MongoDB服务 错误1067:进程意外终止 下篇帖子: MongoDB实战(MongoDB开发者现身说法)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表