x

UPDATE (with a join) performance... can I improve this?

Greetings,

I'm trying to update the columns of one table to contain values copied from a table in a different database.

The update statement I'm using is:

    UPDATE loadbannerImpressions
       SET isp = ip.ipISP,
           region = ip.ipRegion,
           city = ip.ipCity,
           countryCode = ip.countryShort,
           country = ip.countryLong
  FROM loadbannerImpressions 
  INNER JOIN ip2location.dbo.ip2locationData ip 
        ON clientIPno BETWEEN ip.ipFrom AND ip.ipTo
     WHERE memberNo = 0

The ip2location.dbo.ip2locationData is a table the contains approx 7 million records with regional data based on IP addresses.
Sample Data:

ipFROM                 ipTO                   countrySHORT countryLONG      
---------------------- ---------------------- ------------ -----------------
0                      33554431               -            -                
33554432               33685503               UK           UNITED KINGDOM   
33685504               50331647               UK           UNITED KINGDOM   
50331648               67108863               US           UNITED STATES    
67108864               67109119               US           UNITED STATES

I've created non-clustered indexes on the ipFrom and ipTo columns as follows: create nonclustered index idx_ipFrom on ip2locationdata (ipFrom) ON FG_INDEXES create nonclustered index idx_ipTo on ip2locationdata (ipTo) ON FG_INDEXES

The loadBannerImpressions table contains approx. 500,000 rows.

The update statement tends to take a long time to execute, and I'm wondering if I'm going about this the wrong way. 500,000 rows, joining with a table of 7,000,000 rows is going to eat some CPU time, but I'm wondering if there's a better way I could be doing this?

Thanks,

Bill

more ▼

asked Feb 04, 2010 at 02:24 PM in Default

avatar image

Bill
146 6 10 12

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Have you examined the execution plans to see which of the indexes are being used? In all likelihood you will probably need to create a compound index using both the IP columns. SQL Server can do index intersection, but it's not that common for it to do so. You may also need to include the columns from the select list to avoid the key lookup. But again, without the execution plan, it's hard to know.

What's the clustered index on ip2locationData?

more ▼

answered Feb 04, 2010 at 02:28 PM

avatar image

Grant Fritchey ♦♦
137k 20 42 81

(comments are locked)
10|1200 characters needed characters left

In SQL Server 2008 and later it's best to avoid using "UPDATE with join". Use MERGE instead. MERGE is frequently more efficieant and is standard SQL whereas UPDATE...JOIN is proprietary. UPDATE...JOIN also gives unpredictable results if the join criteria is non-unique.

more ▼

answered Feb 04, 2010 at 03:46 PM

avatar image

David 1
1.8k 3 5

Good tip. This is SQL Server 2005, however. I'm assuming I should continue as I am...

Thanks.

Feb 04, 2010 at 04:04 PM Bill
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x2016
x288
x161

asked: Feb 04, 2010 at 02:24 PM

Seen: 4388 times

Last Updated: Feb 05, 2010 at 10:01 AM

Copyright 2016 Redgate Software. Privacy Policy