请帮助您解决此问题我有一个数据库,其中有150,000条业务记录,每个业务记录都有自己的业务类别(例如:酒吧,酒馆,餐厅)。
我正在使用此SQl来根据访问者的位置获取类别列表。
SELECT
ROUND(6371*acos(cos(radians('52.28231599999999'))*cos(radians(bizprof.vLatitude))*cos(radians(bizprof.vLongitude)-radians('-1.584927'))+sin(radians('52.28231599999999'))*sin(radians(bizprof.vLatitude))),2) AS distance,
`bizcat`.`vCategoryName`,
`bizcat`.`iCategoryId` FROM `business_profile` `bizprof`
LEFT JOIN `users` `u` ON u.iUserId = bizprof.iUserId
AND u.tiIsProfileSet = 1
AND u.tiIsActive = 1
AND u.tiIsDeleted = 0
LEFT JOIN `business_categories` `bizcat` ON bizcat.iCategoryId = bizprof.iCategoryId
GROUP BY `bizcat`.`iCategoryId`
HAVING distance >= 0 AND distance <= 10
此查询花费太多时间来呈现记录中的数据。
对此有任何想法吗?
只是一些建议。确保在
上具有正确的互补索引表business_profile列(iUserId,iCategoryId)
表用户列(iUserId,tiIsProfileSet,tiIsActive,tiIsDeleted)
表business_categories列(iCategoryId)
那么您不应该使用没有聚合功能的分组方式(如果需要不同的结果,请在select中添加DISTINCT子句)
您还可以使用where(对距离重复di代码)子句,而不必对结果进行过滤
SELECT
ROUND(6371*acos(cos(radians('52.28231599999999'))*cos(radians(bizprof.vLatitude))*cos(radians(bizprof.vLongitude)-radians('-1.584927'))+sin(radians('52.28231599999999'))*sin(radians(bizprof.vLatitude))),2) AS distance,
`bizcat`.`vCategoryName`,
`bizcat`.`iCategoryId`
FROM `business_profile` `bizprof`
LEFT JOIN `users` `u` ON u.iUserId = bizprof.iUserId
AND u.tiIsProfileSet = 1
AND u.tiIsActive = 1
AND u.tiIsDeleted = 0
LEFT JOIN `business_categories` `bizcat` ON bizcat.iCategoryId = bizprof.iCategoryId
WHERE ROUND(6371*acos(cos(radians('52.28231599999999'))*cos(radians(bizprof.vLatitude))*cos(radians(bizprof.vLongitude)-radians('-1.584927'))+sin(radians('52.28231599999999'))*sin(radians(bizprof.vLatitude))),2) >= 0
AND ROUND(6371*acos(cos(radians('52.28231599999999'))*cos(radians(bizprof.vLatitude))*cos(radians(bizprof.vLongitude)-radians('-1.584927'))+sin(radians('52.28231599999999'))*sin(radians(bizprof.vLatitude))),2) <= 10
AND u.tiIsProfileSet = 1 AND u.tiIsActive = 1 AND u.tiIsDeleted = 0
上移动到where
条件。business_categories
中获取数据尝试执行此查询
SELECT
ST_Distance_Sphere(Point('52.28231599999999','-1.584927'), Point(`bizprof`.`vLatitude`,`bizprof`.`vLongitude`), 6370986 ) AS `distance`,
`bizprof`.`iCategoryId`
FROM `business_profile` `bizprof`
LEFT JOIN `users` `u` ON `u`.`iUserId` = `bizprof`.`iUserId`
WHERE 1=1
AND `u`.`tiIsProfileSet` = 1
AND `u`.`tiIsActive` = 1
AND `u`.`tiIsDeleted` = 0
HAVING distance >= 0 AND distance <= 10*1000