最近正在使用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<User>.LT(c => c.Age,
12
);
var
collection1 = database.GetCollection<User>(
"
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<User>.LT(c => c.Age,
12
);
var
collection2 = database.GetCollection<User>(
"
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<User>.LT(c => c.Age,
12
);
var
collection2 = database.GetCollection<User>(
"
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<User>(
"
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<User>(
"
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
第一種,第二種和第三種是差不多的。
第四種是最慢的,把全部的查找出來當然是最慢的。
第五種是最快的,我反復測試了很多次,真的有點不敢相信。
結論:建議盡量使用第五種方式;不建議使用第四種方式。
?
以上是從使用的角度來測試。如有不同觀點,支持回復指點,謝謝。
?
以上測試的源碼:
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();
}
///
<summary>
///
測試數據庫
///
</summary>
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<User>.LT(c => c.Age,
12
);
var
collection1 = database.GetCollection<User>(
"
entities
"
);
collection1.Find(querty).Max(c
=>
c.Age);
});
//
TestMethod(testCount, () =>
//
{
//
var querty = Query<User>.LT(c => c.Age, 12);
//
var collection2 = database.GetCollection<User>("entities");
//
(from a in collection2.Find(querty) select a).Max(c => c.Age);
//
});
//
TestMethod(testCount, () =>
//
{
//
var querty = Query<User>.LT(c => c.Age, 12);
//
var collection2 = database.GetCollection<User>("entities");
//
(from a in collection2.Find(querty).ToList() select a).Max(c => c.Age);
//
});
//
TestMethod(testCount, () =>
//
{
//
var collection3 = database.GetCollection<User>("entities");
//
(from a in collection3.FindAll() where a.Age < 12 select a).Max(c => c.Age);
//
});
//
TestMethod(testCount, () =>
//
{
//
var collection3 = database.GetCollection<User>("entities");
//
(from a in collection3.AsQueryable() where a.Age < 12 select a).Max(c => c.Age);
//
});
}
///
<summary>
///
測試方法
///
</summary>
///
<param name="testCount"></param>
///
<param name="fun"></param>
private
static
void
TestMethod(
int
testCount, Action fun)
{
var
list =
new
List<
double
>
();
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();
}
}
///
<summary>
///
寫文件文本
///
</summary>
///
<param name="result"></param>
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
; }
}
}
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

