减少从MYSQL DB中获取数据的时间(表150,000中的记录)

问题描述 投票:0回答:2

请帮助您解决此问题我有一个数据库,其中有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

此查询花费太多时间来呈现记录中的数据。

对此有任何想法吗?

mysql yii2
2个回答
0
投票

只是一些建议。确保在

上具有正确的互补索引

表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

0
投票
  1. 使用ST_Distance_Sphere(g1, g2 [, radius])spatial indexes
  2. AND u.tiIsProfileSet = 1 AND u.tiIsActive = 1 AND u.tiIsDeleted = 0上移动到where条件。
  3. 避免第三次联接,通过另一个查询(例如,通过关系)从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
© www.soinside.com 2019 - 2024. All rights reserved.