Antoine
Antoine

Reputation: 5245

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. 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?

Upvotes: 4

Views: 4082

Answers (1)

WiredPrairie
WiredPrairie

Reputation: 59763

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.

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. 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.

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

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

Upvotes: 4

Related Questions