比如: 饿了么搜索附近的餐厅,qq,微信附件的人,后端怎么搜索出来的呢
数据库里面有1w条餐厅的坐标记录,当用户打开附件的餐厅的时候,搜集到用户的坐标,比如针对某一个店的话,两点的坐标,计算球面距离是好计算的,问题是,有1w条店,怎么找出来附件500米,1000米 ,2000米....
回复内容:
实现方案有很多:1. mysql的空间数据库:
Mysql gis 空间数据库功能详解学习;
2. solr空间索引天然支持按经纬度索引,按位置查询,按距离排序;
http://tech.meituan.com/solr-spatial-search.html
3. redis新版本也支持geohash了;
Redis GEO 特性简介 一般都采用Geohash算法,用一个字符串表示二维坐标,并且两个点越接近,这两个Geohash的字符串前缀相同的位数就越多(这句话可能不太严谨),这样查询的效率就会高很多。
Geohash 一般都是Geohash算法,不过这个算法的问题是可能很近的但是分在不同格子里就捞不到了,会选取周围8个格子来计算,另外就是这个算法不好按照范围搜,比如附近xx米。
第二种算法是附近xx米是一个圆,计算出圆的外接正方形的经纬度范围,就是当前位置为中心边长是xx*2的一个正方形的经纬度范围,然后在数据库里通过大于小于条件来捞落入正方形内的POI(店铺),然后,要严谨的话把四个角边缘的数据剔除(通过计算距离就可以,这时候数据量很少,不需要全表扫描计算,就很快了),精度要求不高的话,不需要剔除直接就能用了,这种做法只要把数据库中经度和纬度两个字段做索引,应付一般数据量足够了,做法也简单直白,题主只有1W条数据,这种方法妥妥的。
第三种就是利用数据库的空间索引来做,貌似MySQL本身就支持的,用R Tree实现的,效率更高。 1、把经纬度换成整数。
2、不算圆型算方型,避开乘方根号运算,直接 between 经度 and between 纬度。
3、确定了这个方内的人后,再进行乘方(但不开根号)排序就行。 查找 1000 米以内的人,数据库设计和查询如何实现这个功能? - 数据库设计 快使用开源工具,横横哈嘿,快打开源代码,嘿嘿哈横。 用mongodb,自带这类算法 大家的坐标保存起来,然后把你的坐标与大家的坐标求距离就行了。 geohash | rtree 方案:geohash + redis set
时间复杂度:O(1)
空间复杂度:O(n),挺省的,因为redis的key很短,参考下表:


TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
