简体   繁体   English

MongoDB中索引数据类型的性能影响?

[英]Performance impact of index datatype in MongoDB?

I need a new Mongo collection that associates data with an IP address, the address being the collection key. 我需要一个新的Mongo集合,它将数据与IP地址相关联,该地址是集合密钥。 I'm wondering if there's any performance advantage using the decimal notation of the IP adress (eg 3299551096 as an integer) instead of the dotted notation (eg "198.252.206.16" as a string). 我想知道使用IP地址的十进制表示法(例如3299551096作为整数)而不是点分表示法(例如"198.252.206.16"作为字符串)是否有任何性能优势。

I haven't found any evidence for or against, nor any performance comparison between integer and string indexes. 我没有找到任何证据支持或反对,也没有在整数和字符串索引之间进行任何性能比较。 Is there any reason to prefer one over the other? 有什么理由比较喜欢一个吗?

An integer value storage requirement is smaller, but of course, not very significant. 整数值存储要求较小,但当然不是很重要。 The sorting/indexing algorithm for a number would be slightly faster than a string normally, but the difference would be extremely small as the string is also very short. 数字的排序/索引算法通常会比字符串快,但由于字符串也很短,因此差异非常小。

I wouldn't expect a compelling performance difference between the two. 我不希望两者之间产生令人信服的性能差异。 If you're planning on storing IPV6 addresses, the issue will be that BSON ( http://bsonspec.org/#/specification ) doesn't have a simple data type for storing a 16-byte number, so it's not necessarily a natural fit to store as a number only. 如果您计划存储IPV6地址,问题将是BSON( http://bsonspec.org/#/specification )没有用于存储16字节数字的简单数据类型,因此它不一定是自然适合仅作为数字存储。

In the end, I'd likely just use strings if you want to avoid doing translation from storage to screen, or if you want to make queries more natural to write for most of us :) : 最后,如果你想避免从存储转换到屏幕,或者你想让查询更自然地为我们大多数人写:)我可能只是使用字符串:):

db.ips.find({addr: "192.168.1.1"})

If using strings, I'd also suggest you consider storing as a fixed format string such as 192.168.001.001 if you want to do more complex searches, such as a range search. 如果使用字符串,我还建议您考虑存储为固定格式字符串,如192.168.001.001如果您想进行更复杂的搜索,例如范围搜索。 Since a string stored with a consistent fixed format will sort naturally, you can use it in more ways than you'd otherwise be able to. 由于以一致的固定格式存储的字符串会自然排序,因此您可以以比其他方式更多的方式使用它。 If ranges aren't important, it's not necessary to store this way. 如果范围不重要,则无需以此方式存储。

With a fixed format, you could do a query like: 使用固定格式,您可以执行以下查询:

db.ips.find({ addr: {
                 $gte: "192.168.000.000",
                 $lte: "192.168.000.255" } })

That would find all IP addresses between (inclusive) 192.168.0.0 and 192.168.0.255 . 这将找到(包括) 192.168.0.0192.168.0.255之间的所有IP地址。

Ideally, you'll have an index on the field either way: 理想情况下,您将在该字段上有一个索引:

db.ips.ensureIndex({ addr: 1 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM