The problem is that your lookup table has non-overlapping address fragments (ranges). However, SQL Server probably does not recognize this. So, when you have ipaddress between A and B , it should scan the entire index, starting from the beginning to A.
I do not know if there is a way to explain what the table does in such a way that the optimizer goes to the first matching record in the index. Perhaps something like this will work:
select bt.*, (select top 1 RangeEndValue from #RangeLookupTable rlt where rlt.RangeStartValue <= bt.SomeIntegerValue order by RangeStartValue desc) FROM #BaseTable bt
This can trick the optimizer into looking for only one entry in the index. The data in your example is too small to determine if this will affect performance.
Another approach is to use equi-join to stop the search. In each table, add a portion of the TypeA address (first byte). This can either be redundant if the second field has a full address, or you can put the remaining three bytes in the next field. Any ip list that spans multiple TypeA addresses should be split into separate entries.
Make this field the first column in the index with the address (or the rest of the address) as the second part of the primary key. You can use constraints to create a primary key with multiple columns.
The request will look like this:
SELECT * FROM
Then the index scan will be limited only to values ββwith the same first byte. Of course, you can also extend this to TypeAB using the first two bytes.
Gordon linoff
source share