Advertisement

12.19.2007 at 09:21AM PST, ID: 23033966
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.4

Query optimizer wrecks my Query - Can I hide UDF internals from query optimizer?

Asked by JohnBPrice in MS SQL Server

Tags: , ,

I have the need to find companies within a certain range of a zip code.  Technically this is easy as I have the Lat/Lng for every US and Canadian zip code, but the MS SQL Query optimizer messes me up.  First I had a simple function that calculates the distance between 2 zip codes, e.g.

Select * from Company where ZipToZipDistance('48111',Company.Zip) < 50

However that is really slow since it calculates the distance for every record.  I pre-assign lat/lng to every Company, so this type of query is really fast:

Select * from Company where Company.Longitude between -83.683 AND -83.292

So I wrote a couple simple functions to give me the lower and upper longitude for a target zip code and range,  thinking that it would be calculated once and used on an index for longitude, basically it is

CREATE FUNCTION dbo.UpperLng (@Zip varchar(50), @Range int) RETURNS float AS  BEGIN
        blah, blah, blah to clean up zip codes
      select @Lng = avg(Lng), @Lat = avg(Lat) from zip where zip like @TempZip
        set @Lng = @Lng + (@Range / (69.1 * Cos(@Lat / 57.3)))
      return @Lng

And the function by itself works just fine.  However, if I run a query like this:

Select * from Company where Company.Longitude between LowerLng('48111',10) AND UpperLng('48111',10)

The query optimizer goes nuts, figure out that I am doing a "zip like @TempZip" inside my functions, and gives me a hideous plan including things like
WHERE Company.Zip Like @TempZip for the upper function, another WHERE Company.zip Like @TempZip for the lower function, a gazillion Compute Scaler to calculate the upper/lower longitude for EVERY STINKING RECORD, and THEN does the compare to see if it is in range.

Is there any way I can force SQL into using the fixed result of my function instead of trying to calculate it for every record.  I know I could put the whole thing into a stored procedure, but that would be inconvenient for a particular web app I am creating.  Is there is a simple way to tell SQL "Calculate this first because it is a constant"

Start Free Trial
[+][-]12.19.2007 at 10:34AM PST, ID: 20501688

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.19.2007 at 10:45AM PST, ID: 20501759

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.19.2007 at 11:26AM PST, ID: 20502088

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: hide, optimizer, query
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 2
Solution Grade: A
 
 
[+][-]12.29.2007 at 07:13AM PST, ID: 20547936

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.29.2007 at 07:22AM PST, ID: 20547966

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.29.2007 at 12:42PM PST, ID: 20548973

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628