用于获取地理位置记录的MySQL查询优化

时间:2013-12-09 12:07:24

标签: php mysql performance pdo query-optimization

我有一个地方的地理位置意味着我有一个CenterLatitudeCenterLongitude以及radius。在此半径范围内,有许多区域记录具有不同类型的RegionType。我希望通过匹配RegionType中每个RegionType的记录强度来获取正确的radius记录。

我当前的逻辑就是这个,

第1步:找出最小和最大纬度和长值

    $milesToKM = 112.654;

    $minLat = $data['lat'] - ($data['rad'] / $milesToKM);

    $maxLat = $data['lat'] + ($data['rad'] / $milesToKM);

    $minLong = $data['lon'] - ($data['rad'] / $milesToKM);

    $maxLong = $data['lon'] + ($data['rad'] / $milesToKM);

第2步:选择Regiontype <{1}}

count

第3步:决定相关$query = $pdo->prepare("SELECT `RegionType`, count(`RegionType`) as count from (SELECT RegionType FROM data_api_region WHERE (CenterLatitude between $minLat and $maxLat) AND (CenterLongitude between $minLong and $maxLong) AND languageCode = '" . $data['locale'] . "') q GROUP BY `RegionType`"); $query->execute(); $regionCounts = $query->fetchAll(PDO::FETCH_ASSOC); unset($query); $finalCountArr = array(); foreach ($regionCounts as $regionCountData) { $finalCountArr[$regionCountData['RegionType']] = $regionCountData['count']; }

RegionType

第4步:获取正确的记录

switch (true) {
            case (isset($finalCountArr['Continent']) && $finalCountArr['Continent'] > 5):
                $regionTypeFinal = 'Continent';
                break;
            case (isset($finalCountArr['Country']) && $finalCountArr['Country'] > 5):
                $regionTypeFinal = 'Country';
                break;
            case (isset($finalCountArr['Multi-Region (within a country)']) && $finalCountArr['Multi-Region (within a country)'] > 5):
                $regionTypeFinal = 'Multi-Region (within a country)';
                break;
            case (isset($finalCountArr['Province (State)']) && $finalCountArr['Province (State)'] > 5):
                $regionTypeFinal = 'Province (State)';
                break;
            case (isset($finalCountArr['Multi-City (Vicinity)']) && $finalCountArr['Multi-City (Vicinity)'] > 5):
                $regionTypeFinal = 'Multi-City (Vicinity)';
                break;
            case (isset($finalCountArr['City']) && $finalCountArr['City'] > 5):
                $regionTypeFinal = 'City';
                break;
            case (isset($finalCountArr['Neighborhood']) && $finalCountArr['Neighborhood'] > 5):
                $regionTypeFinal = 'Neighborhood';
                break;
            default:
                $regionTypeFinal = 'Point of Interest';
                break;
        }

任何人都可以告诉我如何优化这个过程,因为该表包含缺少记录,所以这个过程需要时间..是否可以使用一个SQL查询?

1 个答案:

答案 0 :(得分:0)

没有测试数据或表格布局,所以我无法真正测试这个,但它可能是在一个这样的SQL中完成的: -

SELECT `id` as regionId, `RegionName` as regionName,`RegionNameLong` as regionNameLong, `RegionType` as regionType,`CenterLongitude` as centerLongitude, `CenterLatitude` as centerLatitude ,`LanguageCode` as languageCode 
FROM data_api_region 
INNER JOIN
(
    SELECT CASE
            WHEN RegionType = 'Continent' AND `count` > 5
                THEN 'Continent'
            WHEN RegionType = 'Country' AND `count`  > 5
                THEN 'Country'
            WHEN RegionType = 'Multi-Region (within a country)' AND `count`  > 5
                THEN 'Multi-Region (within a country)'
            WHEN RegionType = 'Province (State)' AND `count`  > 5
                THEN 'Province (State)'
            WHEN RegionType = 'Multi-City (Vicinity)' AND `count`  > 5
                THEN 'Multi-City (Vicinity)'
            WHEN RegionType = 'City' AND `count`  > 5
                THEN 'City'
            WHEN RegionType = 'Neighborhood' AND `count`  > 5
                THEN 'Neighborhood'
            ELSE 'Point of Interest'
        END AS regionTypeFinal
    FROM 
    (
        SELECT `RegionType`, count(`RegionType`) as `count` 
        FROM data_api_region 
        WHERE (CenterLatitude between $minLat and $maxLat) 
        AND (CenterLongitude between $minLong and $maxLong) AND languageCode = '" . $data['locale'] . "'
        GROUP BY `RegionType`
    ) q 
) Sub1
ON data_api_region.RegionType = Sub1.regionTypeFinal
WHERE (CenterLatitude between $minLat and $maxLat) 
AND (CenterLongitude between $minLong and $maxLong) 
AND languageCode = '" . $data['locale'] . "'