I have a table that looks like this:
ID StartRange EndRange
----------------------------
1 1 3
2 4 8
3 9 12
And so on and so forth, so that there are over 5 million records. The last record looks something like this:
ID StartRange EndRange
---------------------------------
5235976 9894727374 9894727378
In other words, the StartRange and EndRange will never overlap for each record.
I need to do a query that finds the corresponding ID of a number that matches the range:
SELECT ID FROM BigTable WHERE '5000000' BETWEEN StartRange AND EndRange;
Unfortunately, this query takes several seconds to complete. I need to optimize it so that it takes the least amount of execution time. I did a little bit of research it looks like adding an index is not helpful because it would only apply if the number is exactly the StartRange or EndRange value, but not if it’s between.
Does anyone have any tips or tricks I can use to bring down the execution time? Ideally I’d want it to be under 1 second if possible.
I had a similar problem with a table of ip address ranges and the below really did the trick for me. You’ll want an index on at least StartRange.