Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 90609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:54:00+00:00 2026-05-10T22:54:00+00:00

I have a table where I’m storing Lat/Long coordinates, and I want to make

  • 0

I have a table where I’m storing Lat/Long coordinates, and I want to make a query where I want to get all the records that are within a distance of a certain point.

This table has about 10 million records, and there’s an index over the Lat/Long fields

This does not need to be precise. Among other things, I’m considering that 1 degree Long == 1 degree Lat, which I know is not true, but the ellipse I’m getting is good enough for this purpose.

For my examples below, let’s say the point in question is [40, 140], and my radius, in degrees, is 2 degrees.

I’ve tried this 2 ways:


1) I created a UDF to calculate the Square of the Distance between 2 points, and I’m running that UDF in a query.

SELECT Lat, Long FROM Table    WHERE (Lat BETWEEN 38 AND 42)      AND (Long BETWEEN 138 AND 142)     AND dbo.SquareDistance(Lat, Long, 40, 140) < 4 

I’m filtering by a square first, to speed up the query and let SQL use the index, and then refining that to match only the records that fall within the circle with my UDF.


2) Run the query to get the square (same as before, but without the last line), feed ALL those records to my ASP.Net code, and calculate the circle in the ASP.Net side (same idea, calculate the square of the distance to save the Sqrt call, and compare to the square of my radius).


To my suprise, calculating the circle in the .Net side is about 10 times faster than using the UDF, which leads me to believe that I’m doing something horribly wrong with that UDF…

This is the code I’m using:

CREATE FUNCTION [dbo].[SquareDistance]  (@Lat1 float, @Long1 float, @Lat2 float, @Long2 float) RETURNS float AS BEGIN     -- Declare the return variable here     DECLARE @Result float     DECLARE @LatDiff float, @LongDiff float      SELECT @LatDiff = @Lat1 - @Lat2     SELECT @LongDiff = @Long1 - @Long2      SELECT @Result = (@LatDiff * @LatDiff) + (@LongDiff * @LongDiff)      -- Return the result of the function     RETURN @Result  END 

Am I missing something here?
Shouldn’t using a UDF within SQL Server be much faster than feeding about 25% more records than necessary to .Net, with the overhead of the DataReader, the communication between processes and whatnot?

Is there something I’m doing horribly wrong in that UDF that makes it run slow?
Is there any way to improve it?

Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:54:01+00:00Added an answer on May 10, 2026 at 10:54 pm

    You can improve the performance of this UDF by NOT declaring variables and doing your calculations more in-line. This will likely improve performance a little but (but probably not much).

    CREATE FUNCTION [dbo].[SquareDistance]  (@Lat1 float, @Long1 float, @Lat2 float, @Long2 float) RETURNS float AS BEGIN     Return ( SELECT ((@Lat1 - @Lat2) * (@Lat1 - @Lat2)) + ((@Long1 - @Long2) * (@Long1 - @Long2))) END 

    Even better would be to remove the function and put the calculations in the original query.

    SELECT Lat, Long FROM Table    WHERE (Lat BETWEEN 38 AND 42)      AND (Long BETWEEN 138 AND 142)     AND ((Lat - 40) * (Lat - 40)) + ((Long - 140) * (Long - 140))  < 4 

    There is a little bit of overhead with calling a user defined function. By removing the function, you are likely to gain a little in performance.

    Also, I encourage you to check your execution plan just to make sure you are getting index seeks like you expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In general, you can't do this. UTF-8 is capable of… May 11, 2026 at 1:57 pm
  • added an answer It supports the ability to create a tag file on… May 11, 2026 at 1:57 pm
  • added an answer In Java 1.6 you can use NavigableSet. May 11, 2026 at 1:57 pm

Related Questions

I have a table where I'm storing Lat/Long coordinates, and I want to make
I have a table where one column has duplicate records but other columns are
I have a table where one of the left column shrinks when I set
I have a situation where I build a view with a table and another
If I have a table where the cells in a column should not have
I have a table where I'm inserting, for instance, images and the names of
I want to list the recent activities of a user on my site without
The problem itself is simple, but I can't figure out a solution that does
I have a table called OffDays, where weekends and holiday dates are kept. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.