Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String vs. Number Comparison Efficiency in MongoDB

Suppose that I querying by an arbitrary field category that will be four characters long with all numbers.

So, it could be '1234', '2341', '9999' etc.

Now, if I am certain that this numerical representation of this string will be greater than or equal to 1000 and less than or equal to 9999, does making this field a number instead of a string make the query more efficient? I believe that number comparisons are more efficient than string comparisons, but I am not sure that it will have any discernible effect on the query performance. I believe that making this field a string would be better if query performance is equal because the field categories number is just an identifier and therefore a string is more descriptive of what is going on, but better query performance is more important.

Therefore, my question is if I query on this arbitrary category field will it be more efficient to query by string or by number or that it doesn't matter?

like image 368
Anthony Dito Avatar asked Mar 09 '16 22:03

Anthony Dito


People also ask

Are string comparisons slow?

Comparing two strings is very slow and expensive. Most algorithms require iterating through entire string and matching each character.

What method can we use to maximize performance and prevent MongoDB from returning more results than required for processing?

You must apply limit() to the cursor before retrieving any documents from the database. Use limit() to maximize performance and prevent MongoDB from returning more results than required for processing.

What is the improvement in performance in MongoDB?

Other ways to improve MongoDB performance after identifying your major query patterns include: Storing the results of frequent sub-queries on documents to reduce read load. Making sure that you have indices on any fields you regularly query against. Looking at your logs to identify slow queries, then check your indices.


1 Answers

According to this and that index is represented as BSON document in binary form.

Efficiency can be gained by making it small (index just what is needed) rather than playing with data types.

like image 83
profesor79 Avatar answered Sep 23 '22 17:09

profesor79