Solved

Help with Inner Join Replace

Posted on 2010-11-16
2
287 Views
Last Modified: 2012-05-10
This query takes 2 seconds to run:

update WideCityList
set Item=1
from widecitylist
inner join (SELECT  Distinct zips.CityName, zips.ProvinceAbbr
  FROM dbo.postalcodes AS [zips]
 INNER JOIN dbo.CalculateBoundary(49.136510, - 122.831860, 45.000000, 'kilometers') AS bounds ON 1 = 1
  WHERE [zips].[Latitude] BETWEEN [bounds].[South] AND [bounds].[North]
   AND [zips].[Longitude] BETWEEN [bounds].[West] AND [bounds].[East]
   AND [zips].[Latitude] <> 0
   AND [zips].[Longitude] <> 0
   AND   (dbo.CalculateDistance(49.136510, - 122.831860, zips.Latitude, zips.Longitude, 'kilometers') <= 21.000000)  
   AND [zips].[CityType] = 'D') c on
c.cityname=widecitylist.citt  and c.provinceabbr = widecitylist.state


and all i do is change the last line to:
 
c.cityname=Replace (widecitylist.city,'-',' ')  and c.provinceabbr = widecitylist.state
       
And now it takes over 20 seconds, which is unacceptable.

What is happening is that the query pulls all fields from dbo.zips in a radius of 21 kilometers of Surrey BC, which is 32,000 (canada has hundreds of thousands of zipcodes, sometimes 10,000+ in a single city).  I then select only the distinct city, which is 24.

From those 24 results, I simply want to update WideCityList item = 1, but I needed the replace function because all cities in WideCityList have a "-" instead of a space for cities with two words.  I dont know why it takes so horribly long to run the query simply by changing the last line...I'm assuming that somewhere in there its doing an inner join using REPLACE on all 32000 results before DISTINCT is processed?  I only want to perform the inner join with Replace against the 24 results of the inside select statement.

hoping this makes sense!   Any ideas?
0
Comment
Question by:arthurh88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34151619
Try this form:

update WideCityList
set Item=1
from (SELECT  Distinct zips.CityName, zips.ProvinceAbbr
  FROM dbo.postalcodes AS [zips]
 INNER JOIN dbo.CalculateBoundary(49.136510, - 122.831860, 45.000000, 'kilometers') AS bounds ON 1 = 1
  WHERE [zips].[Latitude] BETWEEN [bounds].[South] AND [bounds].[North]
   AND [zips].[Longitude] BETWEEN [bounds].[West] AND [bounds].[East]
   AND [zips].[Latitude] <> 0
   AND [zips].[Longitude] <> 0
   AND   (dbo.CalculateDistance(49.136510, - 122.831860, zips.Latitude, zips.Longitude, 'kilometers') <= 21.000000)  
   AND [zips].[CityType] = 'D') c
inner join widecitylist on
c.cityname=Replace(widecitylist.city,'-',' ')  and c.provinceabbr = widecitylist.state
option(force order)

If there are only 24 matches, it should be reasonably quick.
If that doesn't work, reverse the condition

inner join widecitylist on
Replace(c.cityname,' ','-')=widecitylist.city  and c.provinceabbr = widecitylist.state

The reversal changes 24 rows, but uses that to do an index lookup in the widecitylist table (assuming one can be used)
0
 

Author Comment

by:arthurh88
ID: 34151631
"inner join widecitylist on
Replace(c.cityname,' ','-')=widecitylist.city  and c.provinceabbr = widecitylist.state"

that worked!!  (the reversal)  thanks so much.  
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question