question

Bill avatar image
Bill asked

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

sql-server-2005performanceupdate
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image
Grant Fritchey answered

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?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David 1 avatar image
David 1 answered

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.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Bill avatar image Bill commented ·
Good tip. This is SQL Server 2005, however. I'm assuming I should continue as I am... Thanks.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.