简体   繁体   English

纬度/经纬度搜索的性能

[英]Performance of lat/long proximity search

I have a page where I show businesses that are within a certain number of miles of a user. 我有一个页面,其中显示距用户一定距离内的业务。 I store the user and business's latitude and longitude in the database. 我将用户和企业的纬度和经度存储在数据库中。 Currently, I'm using NHibernate to return all businesses in the database, then checking each business to see if it's lat/long is within X miles of the user, then creating a new list of only the businesses within that radius and returning those. 当前,我正在使用NHibernate返回数据库中的所有企业,然后检查每个企业的经纬度是否在用户X英里以内,然后仅创建该半径范围内的企业的新列表并返回。

public static List<Business> FindNearbyBusinesses(Coordinates coordinates, int radiusInMiles)
        {//TODO: figure out better way to do this performance-wise!
            //get all businesses.
            var criteria = DetachedCriteria.For<Core.Models.Business>();
            var businesses = FindAll(criteria);
            //find nearby businesses.
            var nearbyBusinesses = new List<Business>();
            foreach (var business in businesses)
            {
                business.MilesToLocation = GeoHelper.GetDistanceBetweenCoordinates(coordinates, business.Coordinates);
                if (business.MilesToLocation <= radiusInMiles)
                {
                    nearbyBusinesses.Add(business);
                }
            }
            //sort.
            nearbyBusinesses.Sort(delegate(Business b1, Business b2)
            {
                return b1.MilesToLocation.CompareTo(b2.MilesToLocation);
            });
            return nearbyBusinesses;
        }

This of course is terribly inefficient and won't scale well once there are tens or hundreds of thousands of businesses in the database. 当然,这是非常低效的,并且一旦数据库中有成千上万的企业,就无法很好地扩展。 Is there a way I can do this better so that I don't have to return every single business in the database first and run this distance calculation against every single one? 有没有一种方法可以使我做得更好,这样我就不必先返回数据库中的每个业务,而不必对每个业务运行此距离计算? One thought I had was maybe there's a way to say, if the radius is a certain amount of miles, then the lat/long should be within this range, then run the calc on only those. 我曾经想过,也许有一种说法,如果半径是一定的英里数,则纬度/经度应在此范围内,然后仅对那些范围进行计算。

Thanks for any suggestions, Justin 谢谢你的建议,贾斯汀

Some googling brought me back to this site: MySQL Great Circle Distance (Haversine formula) 一些谷歌搜索使我回到了该站点: MySQL大圆距离(Haversine公式)

You should use the sql statement there in a stored procedure. 您应该在存储过程中使用sql语句。 I'm using it myself for a store locator. 我自己将其用于商店定位器。

My MSSQL/C# experience is limited, but could you not write a stored procedure to do this? 我的MSSQL / C#经验有限,但是您不能编写存储过程来做到这一点吗?

Apparently you can use C# to build an SP . 显然,您可以使用C#来构建SP Saves you doing it all on the server rather than the DB. 无需您在服务器而不是数据库上做所有事情。 I could be wrong though ;) 我可能是错的;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM