MongoDB中索引数据类型的性能影响? [英] Performance impact of index datatype in MongoDB?

查看:410
本文介绍了MongoDB中索引数据类型的性能影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个新的Mongo集合,它将数据与IP地址相关联,该地址是集合键。我想知道使用IP地址的十进制表示法是否有任何性能优势(例如 3299551096 作为整数)而不是点分表示法(例如 198.252.206.16作为字符串)。

I need a new Mongo collection that associates data with an IP address, the address being the collection key. I'm wondering if there's any performance advantage using the decimal notation of the IP adress (e.g. 3299551096 as an integer) instead of the dotted notation (e.g. "198.252.206.16" as a string).

我没有找到任何证据支持或反对,也没有在整数和字符串索引之间进行任何性能比较。是否有理由更喜欢一个?

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.

我不希望两者之间产生令人信服的性能差异。如果您计划存储IPV6地址,问题将是BSON( http://bsonspec.org/#/规范)没有用于存储16字节数字的简单数据类型,因此它不一定非常适合存储为数字。

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.

最后,我可能只是使用字符串,如果你想避免从存储转换到屏幕,或者如果你想让查询更自然地为我们大多数人写:):

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"})

如果使用字符串,我还建议你考虑存储为固定格式字符串,如 192.168.001.001 如果你想进行更复杂的搜索,例如范围搜索。由于以一致的固定格式存储的字符串将自然排序,因此您可以以比其他方式更多的方式使用它。如果范围不重要,则无需以这种方式存储。

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. 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" } })

这将找到(包括) 192.168.0.0 192.168.0.255 。

理想情况下,你将在该领域有一个索引:

Ideally, you'll have an index on the field either way:

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

这篇关于MongoDB中索引数据类型的性能影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆