mgo查询对于大型数据集返回”EOF”。

huangapple go评论67阅读模式
英文:

mgo query returns "EOF" for large datasets

问题

我想执行一个查询,从我的MongoDB服务器返回一些数据,但是当数据量变大时,c.Find().All()查询会返回一个"EOF"错误。

基本上我有:

activeData := []DataEntry{}
activeDataQuery := bson.M{"data.active": true}
err := sigdb.Find(activeDataQuery).All(&activeData)

这在一个包含大约50,000个项目的小测试中运行良好,但是当我尝试使用包含超过一百万个项目的完整数据集时,它返回"EOF",尽管有数据可以查询。

这可能是什么原因?我在运行Ubuntu 14.04的笔记本电脑上同时运行Go程序和MongoDB服务器,使用的是Go 1.3。

编辑:在进一步尝试后,我还收到了相同查询的"write tcp 127.0.0.1:27017: broken pipe"错误。

英文:

I want to perform a query that returns some data from my MongoDB server, but when the amount of data becomes big I get an "EOF" error from the c.Find().All() query.

Basically I have:

activeData := []DataEntry{}
activeDataQuery := bson.M{"data.active": true}
err := sigdb.Find(activeDataQuery).All(&activeData)

Which works fine for a small test with about 50,000 items, but when I try my full dataset, which is more than one million items, it returns "EOF", eventhough there is data there to be queried.

What could be causing this? I am running both the Go program nad the MongoDB server on my laptop running Ubuntu 14.04 using Go 1.3.

Edit: Upon further trials, I am also getting: "write tcp 127.0.0.1:27017: broken pipe" from the same query.

答案1

得分: 6

All 方法会将所有匹配的数据加载到内存中,这是处理大型数据集的非常糟糕的方式。幸运的是,在方法完成之前,你可能会遇到超时错误,而在最糟糕的情况下,机器会因为内存不足而崩溃。

对于任何非平凡的数据集,应该使用普通的迭代方式,使用 IterNext 方法。

英文:

The All method will load all matching data into memory, which is a very bad way to handle large data sets. With luck you'll get such timeouts before the method is finished, and in the worst case the machine will crash out of memory.

On any sort of non-trivial data set, use normal iteration instead, with the Iter and Next methods.

huangapple
  • 本文由 发表于 2015年3月6日 02:12:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/28884793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定