MongoDB find() slow when querying a 64-bit integer field

时间:2016-04-15 14:45:30

标签: mongodb performance find int64

I have a Mongo collection called Elements containing ~9 million documents. Each document has the following structure:

{
  _id : "1",
  Timestamp : Numberlong(12345),
  Nationality : "ITA",
  Value: 5
}

If I run the following query:

db.Elements.find({ Nationality: 'ITA' })

the query performs fast (a few milliseconds).

If, instead, I run the following query:

db.Elements.find({ Timestamp: 12345 })

the query is slow, in the order of magnitude of tens of seconds. Obviously, if I add an index on Timestamp, the query runs much faster. Running the same query on the field Value, which is of type Int32, runs as fast as the first query.

What I am trying to understand is: why would the second query (without index) perform significantly worse than the first? Does Mongo treat Int64 values differently than other values?

1 个答案:

答案 0 :(得分:1)

事实证明我犯了一个错误。

我使用Robomongo来执行查询;默认情况下,Robomongo会对结果进行分页(默认页面大小为50项)。

由于frequency="22050" bitrate="32000" ffmpeg -loop 1 -i a.jpg -i audio.mp3 -shortest -r 1 -c:v libx264 -crf 28 \ -ar "$frequency" -c:a libfdk_aac -profile:a aac_he_v2 -b:a "$bitrate" -ac 1 result.mkv 字段包含几乎总是不同的值,因此查询必须执行几乎完全扫描才能填满并返回一页。另一方面,因为其他字段包含具有有限范围的值(Timestamp字段,尽管它是Int32,在我的应用程序中具有有限的域)我很快就得到了结果,因为我只是看着第一页。

当我在没有页面的情况下运行相同的查询时(例如,通过附加Value或获取执行计划),所有查询都没有索引,性能很差。

因此,与其他原始类型相比,Int64值似乎没有任何特殊处理。