Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Between efficiency?

Author  Topic 

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2010-10-25 : 09:05:16
I have a table which contains two columns representing ranges of IP addresses. For example, let's say the table is


ipFrom ipTo location
------ ---- ---------
100 105 London
106 110 Brighton


I have been using BETWEEN to locate IP addresses, but I feel this is inefficient. Would it be better to have a single ipaddress column and search on that?

If so, how can I create a new table from the one above to give a new table like the one below please?

ipAddress Location
--------- --------
100 London
101 London
102 London
103 London
104 London
105 London
106 Brighton
107 Brighton
108 Brighton
109 Brighton
110 Brighton

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-10-25 : 09:51:08
Try this -

DECLARE @Start AS INT, @End AS INT, @Location AS VARCHAR(100)
DECLARE @TempTable AS TABLE ( Location varchar(100) )
DECLARE @ResultTable AS TABLE (ip INT, Location VARCHAR(100))

INSERT INTO @TempTable
SELECT Distinct Location FROM ipaddress
WHILE ( SELECT COUNT(*) FROM @Temptable ) > 0
BEGIN
SELECT TOP 1 @Location = Location FROM @TempTable
SELECT @Start = ipFrom, @End = ipto FROM ipAddress WHERE Location = @Location
WHILE ( @Start < = @End )
BEGIN
INSERT INTO @ResultTable
SELECT @Start, @Location
SELECT @Start = @Start + 1
END
DELETE TOP (1) FROM @TempTable
END
--INSERT INTO <your new table name>
SELECT * FROM @ResultTable ORDER BY Location DESC


Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-10-25 : 10:44:44
if you have indexes on ipFrom and ipTo you should be fine using >=@fromip <=@toip. what datatypes are ipFrom ipTo? what are you trying to accomplish?

If you don't have the passion to help people, you have no passion
Go to Top of Page

OldMySQLUser
Constraint Violating Yak Guru

301 Posts

Posted - 2010-10-25 : 11:01:26
quote:
Originally posted by yosiasz

if you have indexes on ipFrom and ipTo you should be fine using >=@fromip <=@toip. what datatypes are ipFrom ipTo? what are you trying to accomplish?

If you don't have the passion to help people, you have no passion



Hi yosiasz,

The columns are both int type. The database is for IP geolocation lookup. Currently a custom (php) script imports 10K+ records and seeks to identify the physical location of the ip address input.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-10-25 : 12:38:53
there is nothing wrong with using BETWEEN as long as you have index on those two fields. is there a scenario where ip ranges could change in future for a certain geolocation? are you with MI6?

If you don't have the passion to help people, you have no passion
Go to Top of Page
   

- Advertisement -