我遇到如下性能问题:
Table : { _id, procedure, latitude, longitude}
我想写一个api检索差距最大5Km:
Api: SearchProductByLocalAroundHere(double latitude, double longitude, int lmax)
我创建了一个函数来计算距离mongdb:
CalculateDistance
function (latitude0, longitude0, latitude1, longitude1, lmax) {
var pi = 3.1415926535897931; var circumference = 40000.0; var distance = 0.0; var latitude0Rad = latitude0 * pi / 180.0; var longitude0Rad = longitude0 * pi / 180.0; var latititude1Rad = latitude1 * pi / 180.0; var longitude1Rad = longitude1 * pi / 180.0; var logitudeDiff = Math.abs(longitude0Rad - longitude1Rad); if (logitudeDiff > pi) logitudeDiff = 2.0 * pi - logitudeDiff; var angleCalculation = Math.acos( Math.sin(latititude1Rad) * Math.sin(latitude0Rad) + Math.cos(latititude1Rad) * Math.cos(latitude0Rad) * Math.cos(logitudeDiff)); distance = circumference * angleCalculation / (2.0 * pi); return distance <=lmax; }
我用它(C#)
var query = Query.EQ("$where", string.Format("CalculateDistance(this.latitude,this.longitude,{0},{1},{2})", latitude, longitude, lmax));
var result = _collection.Find(query).SetLimit(2000).OrderByDescending(x => x.id).ToList();
我在数据库锁定数量时遇到问题&gt; 200000。
期待您的帮助和指导。
感谢!