gpt4 book ai didi

mysql - 为什么 mongoDB 的 Select 时间比 Fetch 时间少?

转载 作者:行者123 更新时间:2023-11-29 00:05:31 27 4
gpt4 key购买 nike

我有 10mill 行的集合,没有任何索引。在这种情况下,系统应该读取整个表吗?

当我使用 eplain 语句时,它会显示db.employees.find({hundreds2:{$lt:1}},{}).explain();“nscannedObjects”:10000000,“n”:105毫秒”:6027

它工作正常。

但我正在使用 java 来处理查询。这是代码

                  whereQuery = new BasicDBObject();
whereQuery.put("hundreds2",new BasicDBObject("$lt", rangeQuery));
timer.start();
setupMongoDBReadQuery(arrForRange[posOfArr]);
cursor = coll.find(whereQuery);
timer.stop();
this.selectTime= timer.elapsed();

timer.start();
while (cursor.hasNext())
{
numberOfRows++;
cursor.next();
}
timer.stop();
this.fetchTime= timer.elapsed();
this.totalOfSelAndFetch=this.selectTime+this.fetchTime;

但是在测试结果之后。我得到了这些信息

selTime=2 fetchTime=6350 numRows105 TotalTime6352
selTime=0 fetchTime=6290 numRows471 TotalTime6290
selTime=0 fetchTime=6365 numRows922 TotalTime6365

为什么 fetch 时间比 select 多。据我所知,while 循环只是打印数据。为什么打印要花这么多时间以及 mongoDB 如何选择 0 或 2 millSec 的行数?

我在 MySQL 中用类似的代码做了同样的实验,结果是

selTime=6302 fetchTime=1 numRows105 TotalTime6303
selTime=6318 fetchTime=1 numRows471 TotalTime6319
selTime=6387 fetchTime=2 numRows922 TotalTime6389

最佳答案

MongoDB 对游标使用惰性求值。这意味着在许多情况下,当您启动返回游标的 MongoDB 查询时,查询尚未执行。

实际选择发生在您开始从游标请求数据时。

主要原因是这允许你调用像sort(by), limit(n) or skip(n)这样的方法在选择任何数据之前,通常可以在数据库上更有效地处理游标。

所以你用“获取时间”衡量的其实也是选择的一部分。

当你想在不获取任何数据的情况下强制执行查询时,你可以在游标上调用 explain()。如果不实际执行查询,数据库就无法测量执行时间。但是,在实际使用中,我建议您不要这样做并按预期方式使用游标。

关于mysql - 为什么 mongoDB 的 Select 时间比 Fetch 时间少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27704709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com