Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Correct index strategy for BETWEEN

$
0
0
Two tables:GeoIpLocation (Id, City, Country, Latitude, Longitude; PK: Id; 2m rows)GeoIpBlock (IpFrom, IpTo, GeoIpLocation_Id; PK: IdFrom, IdTo; 100k rows)Simple concept: get location for given IP. So:[code="sql"]SELECT TOP 1 GeoIpLocation.Country, GeoIpLocation.CityFROM GeoIPLocation INNER JOIN GeoIPBlock ON GeoIPBlock.GeoIpLocation_Id = GeoIPLocation.IdWHERE @nIpNumber BETWEEN GeoIPBlock.IpFrom AND GeoIPBlock.IpTo[/code]Result: disaster.The between operator uses the index on PK on GeoIpBlock, which results on half the table for first part of between and half the table for the second part of between. A bit better query is with FORCESCAN on GeoIPBlock, but it still runs slow, as it scans a lot of records.Question: Is there a better way to index this kind of data?

Viewing all articles
Browse latest Browse all 3145

Trending Articles