转载和积累系列 - 微信、陌陌 架构方案分析

微信、陌陌 架构方案分析

近两年、手机应用,莫过于微信、陌陌之类最受欢迎;但实现原理,分享文章甚少。
故,提出两种方案,供分享;不对之处,敬请留言学习。

目标

解决大型应用(微信、陌陌级别)中,用户经纬度在不断更新,用户查找频繁的问题。(每分钟1000W级)

=================================================================================================
方案A

本方案前,请先阅读 http://www.wubiao.info/372

由上文,简单可得;
1、仅需每分钟将用户的经纬度,上报到数据库;
2、然后每次用户查找附近好友时,通过 LIKE ‘wm3yr3%’,即可获取

缺点:稍有一定数据量,对数据库的鸭梨可想而知

=================================================================================================
方案B

策略

假象把中国分成,若干个一平方公里的单元格,
1、用户位置的变更,理解为一个单元格移动到另外一个单元格(或者不移动)
2、用户查找附近,理解为查找,自己所在方块的的所有人

数据结构

1、用户基本信息 纬度、经度、GeoHash值(经纬度,仅用于后期距离计算)
2、单元格 集合(用户1,用户2,…)

存储工具

1、redis string(key->value) 结构,存储用户基本信息
2、redis set(集合) 结构,以GeoHash值,前6位作为key(约表示一平方千米),存储单元格的用户群

算法流程

1、更新用户信息,先删除用户原所在集合,再更新当前用户信息,最后更新当前用户所在集合
2、查找附近,直接查找,所在单元格集合所有用户ID

具体实现

  1. <?php  
  2.    
  3.  /** 
  4.   * LBS核心类 
  5.   * 
  6.   * @author name <simplephp@163.com> 
  7.   * @site http://www.wubiao.info 
  8.   */  
  9.    
  10.    
  11. include_once('geohash.class.php');  
  12.    
  13. class lbs  
  14. {  
  15.    
  16.     //索引长度 6位  
  17.     protected $index_len = 6;  
  18.    
  19.     protected $redis;  
  20.    
  21.     protected $geohash;  
  22.    
  23.     public function __construct()  
  24.     {  
  25.         //redis  
  26.         $this->redis = new Redis();  
  27.         $this->redis->pconnect('127.0.0.1','6379');  
  28.    
  29.         //geohash  
  30.         $this->geohash = new Geohash();  
  31.     }  
  32.    
  33.     /** 
  34.     * 更新用户信息 
  35.     * 
  36.     * @param mixed $latitude 纬度 
  37.     * @param mixed $longitude 经度 
  38.     */  
  39.     public function upinfo($user_id,$latitude,$longitude)  
  40.     {  
  41.    
  42.         //原数据处理  
  43.    
  44.         //获取原Geohash  
  45.         $o_hashdata = $this->redis->hGet($user_id,'geo');  
  46.    
  47.         if(!empty($o_hashdata))  
  48.         {  
  49.             //原索引  
  50.             $o_index_key = substr($o_hashdata, 0, $this->index_len);  
  51.    
  52.             //删除  
  53.             $this->redis->sRem($o_index_key,$user_id);  
  54.         }  
  55.    
  56.    
  57.         //新数据处理  
  58.    
  59.         //纬度  
  60.         $this->redis->hSet($user_id,'la',$latitude);  
  61.    
  62.         //经度  
  63.         $this->redis->hSet($user_id,'lo',$longitude);  
  64.    
  65.         //Geohash  
  66.         $hashdata = $this->geohash->encode($latitude,$longitude);  
  67.         $this->redis->hSet($user_id,'geo',$hashdata);  
  68.    
  69.         //索引  
  70.         $index_key = substr($hashdata, 0, $this->index_len);  
  71.    
  72.         //存入  
  73.         $this->redis->sAdd($index_key,$user_id);  
  74.    
  75.         return true;  
  76.     }  
  77.    
  78.     /** 
  79.     * 获取附近用户 
  80.     * 
  81.     * @param mixed $latitude 纬度 
  82.     * @param mixed $longitude 经度 
  83.     */  
  84.     public function serach($latitude,$longitude)  
  85.     {  
  86.         //Geohash  
  87.         $hashdata = $this->geohash->encode($latitude,$longitude);  
  88.    
  89.         //索引  
  90.         $index_key = substr($hashdata, 0, $this->index_len);  
  91.    
  92.         //取得  
  93.         $user_id_array = $this->redis->sMembers($index_key);  
  94.    
  95.         return $user_id_array;  
  96.     }  
  97.    
  98. }  
  99.    
  100. ?>  

性能测试

  1. <?php  
  2.    
  3.  /** 
  4.   * 模拟数据上报 
  5.   * 
  6.   * @author name <simplephp@163.com> 
  7.   * @site http://www.wubiao.info 
  8.   */  
  9.    
  10. include_once('lbs.class.php');  
  11.    
  12.    
  13. $b_time = microtime(true);  
  14.    
  15. $n = 0;  
  16.    
  17. while(1)  
  18. {  
  19.     //user_id 1~1000000  
  20.     $user_id = rand(1,1000000);  
  21.    
  22.     //latitude 30.59773~30.726786  
  23.     $rand_latitude = rand(30597730,30726786);  
  24.     $latitude = $rand_latitude/1000000;  
  25.    
  26.     //longitude 103.983192 ~104.16069  
  27.     $rand_longitude = rand(103983192,104160690);  
  28.     $longitude = $rand_longitude/1000000;  
  29.    
  30.     $lbs = new lbs();  
  31.    
  32.     $lbs->upinfo($user_id,$latitude,$longitude);  
  33.    
  34.     $n++;  
  35.     mylog($n);  
  36.    
  37.     $e_time = microtime(true);  
  38.    
  39.     if(($e_time-$b_time)>=60)  
  40.     {  
  41.         exit;  
  42.     }  
  43. }  
  44.    
  45.    
  46. function mylog($content)  
  47. {  
  48.     file_put_contents('upinfo.log',$content."\r\n",FILE_APPEND);  
  49. }  
  50.    
  51. ?>  

  1. <?php  
  2.    
  3.  /** 
  4.   * 模拟查找附近 
  5.   * 
  6.   * @author name <simplephp@163.com> 
  7.   * @site http://www.wubiao.info 
  8.   */  
  9.    
  10. include_once('lbs.class.php');  
  11.    
  12. $b_time = microtime(true);  
  13.    
  14. $n = 0;  
  15.    
  16. while(1)  
  17. {  
  18.     //latitude 30.59773~30.726786  
  19.     $rand_latitude = rand(30597730,30726786);  
  20.     $latitude = $rand_latitude/1000000;  
  21.    
  22.     //longitude 103.983192 ~104.16069  
  23.     $rand_longitude = rand(103983192,104160690);  
  24.     $longitude = $rand_longitude/1000000;  
  25.    
  26.     $lbs = new lbs();  
  27.    
  28.     $re = $lbs->serach($latitude,$longitude);  
  29.    
  30.     $n++;  
  31.     mylog($n);  
  32.    
  33.     $e_time = microtime(true);  
  34.    
  35.     if(($e_time-$b_time)>=60)  
  36.     {  
  37.         exit;  
  38.     }  
  39. }  
  40.    
  41.    
  42. function mylog($content)  
  43. {  
  44.     file_put_contents('search.log',$content."\r\n",FILE_APPEND);  
  45. }  
  46.    
  47.    
  48. ?>  

测试环境

虚拟机,内存256M,主频2.93GHz

性能结果

模拟了100W活跃用户行为,不断更新,不断查找附近好友

//60 seconds insert
88544

//60 seconds search
117660

//成都 100W人,数据占用内存
11.97M

总结

从测试结果来看,完全能满足,微信、陌陌之类的性能要求;

尚可改进之处:

1、Geohash,可写成PHP C扩展;或者其他Geohash实现方式
2、Redis,内存消耗较大,可考虑redis集群方案
3、本文仅查出本单元格用户,提高精度,可查出周围八个单元个,求交集
4、求出结果,如需按照由远到近排序;读出Redis经纬度,利用距离公式排序方可。(可参照上一篇文字)

附redis安装方法
=================================================================================================
//redis

  1. wget http://redis.googlecode.com/files/redis-2.4.14.tar.gz  
  2. make  
  3. make install  
  4.    
  5. //配置  
  6. cp redis.conf /etc/  
  7.    
  8. vi /etc/redis.conf  
  9. #后台  
  10. daemonize yes  
  11. #日志  
  12. logfile /dev/null  
  13. #存储  
  14. dir ./  
  15.    
  16. //小内存,内核参数  
  17. echo 1 > /proc/sys/vm/overcommit_memory  
  18.    
  19. //防火墙  
  20. vi /etc/sysconfig/iptables  
  21. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT  
  22. service iptables restart  
  23.    
  24. //启动  
  25. redis-server /etc/redis.conf  
  26.    
  27. //测试  
  28. redis-cli set foo bar  
  29. OK  
  30. redis-cli get foo  
  31. bar  

=================================================================================================
//php redis 扩展

  1. //源码  
  2.    
  3. http://pecl.php.net/package/redis  
  4.    
  5. //手册  
  6.    
  7. http://redis.readthedocs.org/en/latest/  
  8.    
  9. //安装  
  10. /opt/server/php/bin/phpize  
  11. ./configure --with-php-config=/opt/server/php/bin/php-config  
  12. make  
  13. make install  
  14.    
  15. //配置  
  16. vi php.ini  
  17. [redis]  
  18. extension = redis.so  

来源:http://www.wubiao.info/372


查找附近的xxx 球面距离以及Geohash方案探讨

随着移动终端的普及,很多应用都基于LBS功能,附近的某某(餐馆、银行、妹纸等等)。

基础数据中,一般保存了目标位置的经纬度;利用用户提供的经纬度,进行对比,从而获得是否在附近。

目标:
查找附近的XXX,由近到远返回结果,且结果中有与目标点的距离。

针对查找附近的XXX,提出两个方案,如下:

一、方案A:
=================================================================================================

抽象为球面两点距离的计算,即已知道球面上两点的经纬度;
点(纬度,经度),A($radLat1,$radLng1)、B($radLat2,$radLng2);

优点:通俗易懂,部署简单便捷

缺点:每次都会查询数据库,性能堪忧

1、推导

通过余弦定理以及弧度计算方法,最终推导出来的算式A为:

  1. $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;  

目前网上大多使用Google公开的距离计算公司,推导算式B为:

  1. $s = 2*asin(sqrt(pow(sin(($radLat1-$radLat2)/2),2)+cos($radLat1)*cos($radLat2)*pow(sin(($radLng1-$radLng2)/2),2)))*$R;  

其中 :
$radLat1、$radLng1,$radLat2,$radLng2 为弧度

$R 为地球半径

2、通过测试两种算法,结果相同且都正确,但通过PHP代码测试,两点间距离,10W次性能对比,自行推导版本计算时长算式B较优,如下:

//算式A
0.56368780136108float(431)
0.57460689544678float(431)
0.59051203727722float(431)

//算式B
0.47404885292053float(431)
0.47808718681335float(431)
0.47946381568909float(431)

3、所以采用数学方法推导出的公式:

  1. <?php  
  2.    
  3.     //根据经纬度计算距离 其中A($lat1,$lng1)、B($lat2,$lng2)  
  4.     public static function getDistance($lat1,$lng1,$lat2,$lng2)  
  5.     {    
  6.         //地球半径  
  7.         $R = 6378137;  
  8.    
  9.         //将角度转为狐度  
  10.         $radLat1 = deg2rad($lat1);  
  11.         $radLat2 = deg2rad($lat2);  
  12.         $radLng1 = deg2rad($lng1);  
  13.         $radLng2 = deg2rad($lng2);  
  14.            
  15.         //结果  
  16.         $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;  
  17.    
  18.         //精度  
  19.         $s = round($s* 10000)/10000;  
  20.    
  21.         return  round($s);  
  22.     }  
  23.    
  24. ?>  

4、在实际应用中,需要从数据库中遍历取出符合条件,以及排序等操作,

将所有数据取出,然后通过PHP循环对比,筛选符合条件结果,显然性能低下;所以我们利用下Mysql存储函数来解决这个问题吧。

4.1、创建Mysql存储函数,并对经纬度字段建立索引

  1. DELIMITER $$  
  2.    
  3. CREATE DEFINER=`root`@`%` FUNCTION `GETDISTANCE`(lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2 DOUBLE) RETURNS double  
  4.    
  5. READS SQL DATA  
  6.    
  7. DETERMINISTIC  
  8.    
  9. BEGIN  
  10.    
  11. DECLARE RAD DOUBLE;  
  12.    
  13. DECLARE EARTH_RADIUS DOUBLE DEFAULT 6378137;  
  14.    
  15. DECLARE radLat1 DOUBLE;  
  16.    
  17. DECLARE radLat2 DOUBLE;  
  18.    
  19. DECLARE radLng1 DOUBLE;  
  20.    
  21. DECLARE radLng2 DOUBLE;  
  22.    
  23. DECLARE s DOUBLE;  
  24.    
  25. SET RAD = PI() / 180.0;  
  26.    
  27. SET radLat1 = lat1 * RAD;  
  28.    
  29. SET radLat2 = lat2 * RAD;  
  30.    
  31. SET radLng1 = lng1 * RAD;  
  32.    
  33. SET radLng2 = lng2 * RAD;  
  34.    
  35. SET s = ACOS(COS(radLat1)*COS(radLat2)*COS(radLng1-radLng2)+SIN(radLat1)*SIN(radLat2))*EARTH_RADIUS;  
  36.    
  37. SET s = ROUND(s * 10000) / 10000;  
  38.    
  39. RETURN s;  
  40.    
  41. END$$  
  42.    
  43. DELIMITER ;  

4.2、查询SQL

通过SQL,可设置距离以及排序;可搜索出符合条件的信息,以及有一个较好的排序

  1. SELECT *,latitude,longitude,GETDISTANCE(latitude,longitude,30.663262,104.071619) AS distance FROM  mb_shop_ext where 1 HAVING distance<1000 ORDER BY distance ASC LIMIT 0,10  

二、方案B
=================================================================================================

Geohash算法;geohash是一种地址编码,它能把二维的经纬度编码成一维的字符串。
比如,成都永丰立交的编码是wm3yr31d2524

优点:

1、利用一个字段,即可存储经纬度;搜索时,只需一条索引,效率较高
2、编码的前缀可以表示更大的区域,查找附近的,非常方便。 SQL中,LIKE ‘wm3yr3%’,即可查询附近的所有地点。
3、通过编码精度可模糊坐标、隐私保护等。

缺点: 距离和排序需二次运算(筛选结果中运行,其实挺快)

1、geohash的编码算法

成都永丰立交经纬度(30.63578,104.031601)

1.1、纬度范围(-90, 90)平分成两个区间(-90, 0)、(0, 90), 如果目标纬度位于前一个区间,则编码为0,否则编码为1。
由于30.625265属于(0, 90),所以取编码为1。
然后再将(0, 90)分成 (0, 45), (45, 90)两个区间,而39.92324位于(0, 45),所以编码为0,
然后再将(0, 45)分成 (0, 22.5), (22.5, 45)两个区间,而39.92324位于(22.5, 45),所以编码为1,
依次类推可得永丰立交纬度编码为101010111001001000100101101010。

1.2、经度也用同样的算法,对(-180, 180)依次细分,(-180,0)、(0,180) 得出编码110010011111101001100000000000

1.3、合并经纬度编码,从高到低,先取一位经度,再取一位纬度;得出结果 111001001100011111101011100011000010110000010001010001000100

1.4、用0-9、b-z(去掉a, i, l, o)这32个字母进行base32编码,得到(30.63578,104.031601)的编码为wm3yr31d2524。

  1. 11100 10011 00011 11110 10111 00011 00001 01100 00010 00101 00010 00100 => wm3yr31d2524  
  2.    
  3. 十进制  0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  
  4. base32   0   1   2   3   4   5   6   7   8   9   b   c   d   e   f   g  
  5. 十进制  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  
  6. base32   h   j   k   m   n   p   q   r   s   t   u   v   w   x   y   z  

2、策略

1、在纬度和经度入库时,数据库新加一字段geohash,记录此点的geohash值

2、查找附近,利用 在SQL中 LIKE ‘wm3yr3%’;且此结果可缓存;在小区域内,不会因为改变经纬度,而重新数据库查询

3、查找出的有限结果,如需要求距离或者排序,可利用距离公式和二维数据排序;此时也是少量数据,会很快的。

3、PHP基类

geohash.class.php

  1. <?php  
  2.    
  3. /** 
  4. * Encode and decode geohashes 
  5. * 
  6. */  
  7.    
  8. class Geohash  
  9. {  
  10.     private $coding="0123456789bcdefghjkmnpqrstuvwxyz";  
  11.     private $codingMap=array();  
  12.    
  13.     public function Geohash()  
  14.     {  
  15.         for($i=0; $i<32; $i++)  
  16.         {  
  17.             $this->codingMap[substr($this->coding,$i,1)]=str_pad(decbin($i), 5, "0", STR_PAD_LEFT);  
  18.         }  
  19.    
  20.     }  
  21.    
  22.     public function decode($hash)  
  23.     {  
  24.         $binary="";  
  25.         $hl=strlen($hash);  
  26.         for($i=0; $i<$hl$i++)  
  27.         {  
  28.             $binary.=$this->codingMap[substr($hash,$i,1)];  
  29.         }  
  30.    
  31.         $bl=strlen($binary);  
  32.         $blat="";  
  33.         $blong="";  
  34.         for ($i=0; $i<$bl$i++)  
  35.         {  
  36.             if ($i%2)  
  37.                 $blat=$blat.substr($binary,$i,1);  
  38.             else  
  39.                 $blong=$blong.substr($binary,$i,1);  
  40.    
  41.         }  
  42.    
  43.         $lat=$this->binDecode($blat,-90,90);  
  44.         $long=$this->binDecode($blong,-180,180);  
  45.    
  46.         $latErr=$this->calcError(strlen($blat),-90,90);  
  47.         $longErr=$this->calcError(strlen($blong),-180,180);  
  48.    
  49.         $latPlaces=max(1, -round(log10($latErr))) - 1;  
  50.         $longPlaces=max(1, -round(log10($longErr))) - 1;  
  51.    
  52.         $lat=round($lat$latPlaces);  
  53.         $long=round($long$longPlaces);  
  54.    
  55.         return array($lat,$long);  
  56.     }  
  57.    
  58.     public function encode($lat,$long)  
  59.     {  
  60.         $plat=$this->precision($lat);  
  61.         $latbits=1;  
  62.         $err=45;  
  63.         while($err>$plat)  
  64.         {  
  65.             $latbits++;  
  66.             $err/=2;  
  67.         }  
  68.    
  69.         $plong=$this->precision($long);  
  70.         $longbits=1;  
  71.         $err=90;  
  72.         while($err>$plong)  
  73.         {  
  74.             $longbits++;  
  75.             $err/=2;  
  76.         }  
  77.    
  78.         $bits=max($latbits,$longbits);  
  79.    
  80.         $longbits=$bits;  
  81.         $latbits=$bits;  
  82.         $addlong=1;  
  83.         while (($longbits+$latbits)%5 != 0)  
  84.         {  
  85.             $longbits+=$addlong;  
  86.             $latbits+=!$addlong;  
  87.             $addlong=!$addlong;  
  88.         }  
  89.    
  90.    
  91.         $blat=$this->binEncode($lat,-90,90, $latbits);  
  92.    
  93.         $blong=$this->binEncode($long,-180,180,$longbits);  
  94.    
  95.         $binary="";  
  96.         $uselong=1;  
  97.         while (strlen($blat)+strlen($blong))  
  98.         {  
  99.             if ($uselong)  
  100.             {  
  101.                 $binary=$binary.substr($blong,0,1);  
  102.                 $blong=substr($blong,1);  
  103.             }  
  104.             else  
  105.             {  
  106.                 $binary=$binary.substr($blat,0,1);  
  107.                 $blat=substr($blat,1);  
  108.             }  
  109.             $uselong=!$uselong;  
  110.         }  
  111.    
  112.         $hash="";  
  113.         for ($i=0; $i<strlen($binary); $i+=5)  
  114.         {  
  115.             $n=bindec(substr($binary,$i,5));  
  116.             $hash=$hash.$this->coding[$n];  
  117.         }  
  118.    
  119.    
  120.         return $hash;  
  121.     }  
  122.    
  123.     private function calcError($bits,$min,$max)  
  124.     {  
  125.         $err=($max-$min)/2;  
  126.         while ($bits--)  
  127.             $err/=2;  
  128.         return $err;  
  129.     }  
  130.    
  131.     private function precision($number)  
  132.     {  
  133.         $precision=0;  
  134.         $pt=strpos($number,'.');  
  135.         if ($pt!==false)  
  136.         {  
  137.             $precision=-(strlen($number)-$pt-1);  
  138.         }  
  139.    
  140.         return pow(10,$precision)/2;  
  141.     }  
  142.    
  143.     private function binEncode($number$min$max$bitcount)  
  144.     {  
  145.         if ($bitcount==0)  
  146.             return "";  
  147.         $mid=($min+$max)/2;  
  148.         if ($number>$mid)  
  149.             return "1".$this->binEncode($number$mid$max,$bitcount-1);  
  150.         else  
  151.             return "0".$this->binEncode($number$min$mid,$bitcount-1);  
  152.     }  
  153.    
  154.     private function binDecode($binary$min$max)  
  155.     {  
  156.         $mid=($min+$max)/2;  
  157.    
  158.         if (strlen($binary)==0)  
  159.             return $mid;  
  160.    
  161.         $bit=substr($binary,0,1);  
  162.         $binary=substr($binary,1);  
  163.    
  164.         if ($bit==1)  
  165.             return $this->binDecode($binary$mid$max);  
  166.         else  
  167.             return $this->binDecode($binary$min$mid);  
  168.     }  
  169. }  
  170.    
  171. ?>  

三、测试

  1. <?php  
  2.    
  3. require_once('Mysql.class.php');  
  4. require_once('geohash.class.php');  
  5.    
  6. //mysql  
  7. $conf = array(  
  8.    
  9.     'host' => '127.0.0.1',  
  10.     'port' => 3306,  
  11.     'user' => 'root',  
  12.     'password' => '123456',  
  13.     'database' => 'mocube',  
  14.     'charset' => 'utf8',  
  15.     'persistent' => false  
  16. );  
  17.    
  18. $mysql = new Db_Mysql($conf);  
  19. $geohash=new Geohash;  
  20.    
  21.    
  22. //经纬度转换成Geohash  
  23. /* 
  24.   
  25. $sql = 'select shop_id,latitude,longitude from mb_shop_ext'; 
  26.   
  27.   
  28. $data = $mysql->queryAll($sql); 
  29.   
  30.   
  31. foreach($data as $val) 
  32. { 
  33.   
  34.   $geohash_val = $geohash->encode($val['latitude'],$val['longitude']); 
  35.   
  36.   $sql = 'update mb_shop_ext set geohash= "'.$geohash_val.'" where shop_id = '.$val['shop_id']; 
  37.   
  38.   echo $sql; 
  39.   
  40.   $re = $mysql->query($sql); 
  41.   
  42.   var_dump($re); 
  43.   
  44. } 
  45. */  
  46.    
  47.    
  48. //获取附近的信息  
  49. $n_latitude = $_GET['la'];  
  50. $n_longitude = $_GET['lo'];  
  51.    
  52. //开始  
  53. $b_time = microtime(true);  
  54.    
  55.    
  56. //方案A,直接利用数据库存储函数,遍历排序  
  57. /* 
  58. $sql = 'SELECT *,latitude,longitude,GETDISTANCE(latitude,longitude,'.$n_latitude.','.$n_longitude.') AS distance FROM  mb_shop_ext where 1 HAVING distance<1000 ORDER BY distance ASC'; 
  59.   
  60. $data = $mysql->queryAll($sql); 
  61.   
  62. //结束 
  63. $e_time = microtime(true); 
  64.   
  65. echo $e_time - $b_time; 
  66.   
  67. var_dump($data); 
  68. exit; 
  69. */  
  70.    
  71. //方案B geohash求出附近,然后排序  
  72.    
  73. //当前 geohash值  
  74. $n_geohash = $geohash->encode($n_latitude,$n_longitude);  
  75.    
  76. //附近  
  77. $n = $_GET['n'];  
  78. $like_geohash = substr($n_geohash, 0, $n);  
  79.    
  80. $sql = 'select * from mb_shop_ext where geohash like "'.$like_geohash.'%"';  
  81.    
  82. echo $sql;  
  83.    
  84. $data = $mysql->queryAll($sql);  
  85.    
  86. //算出实际距离  
  87. foreach($data as $key=>$val)  
  88. {  
  89.     $distance = getDistance($n_latitude,$n_longitude,$val['latitude'],$val['longitude']);  
  90.    
  91.     $data[$key]['distance'] = $distance;  
  92.    
  93.     //排序列  
  94.     $sortdistance[$key] = $distance;  
  95. }  
  96.    
  97. //距离排序  
  98. array_multisort($sortdistance,SORT_ASC,$data);  
  99.    
  100. //结束  
  101. $e_time = microtime(true);  
  102.    
  103. echo $e_time - $b_time;  
  104.    
  105. var_dump($data);  
  106.    
  107.    
  108.    
  109. //根据经纬度计算距离 其中A($lat1,$lng1)、B($lat2,$lng2)  
  110. function getDistance($lat1,$lng1,$lat2,$lng2)  
  111. {  
  112.     //地球半径  
  113.     $R = 6378137;  
  114.    
  115.     //将角度转为狐度  
  116.     $radLat1 = deg2rad($lat1);  
  117.     $radLat2 = deg2rad($lat2);  
  118.     $radLng1 = deg2rad($lng1);  
  119.     $radLng2 = deg2rad($lng2);  
  120.    
  121.     //结果  
  122.     $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;  
  123.    
  124.     //精度  
  125.     $s = round($s* 10000)/10000;  
  126.    
  127.     return  round($s);  
  128. }  
  129.    

四、总结

方案B的亮点在于:
1、搜索结果可缓存,重复使用,不会因为用户有小范围的移动,直接穿透数据库查询。
2、先缩小结果范围,再运算、排序,可提升性能。

254条记录,性能对比,

在实际应用场景中,方案B数据库搜索可内存缓存;且如数据量更大,方案B结果会更优。

方案A:
0.016560077667236
0.032402992248535
0.040318012237549

方案B
0.0079810619354248
0.0079669952392578
0.0064868927001953

五、其他

两种方案,根据应用场景以及负载情况合理选择,当然推荐方案B;
不管哪种方案,都记得,给列加上索引,利于数据库检索。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值