簡體   English   中英

MySQL查詢優化,用於獲取地理位置記錄

[英]MySQL Query optimisation for fetching geolocation records

我有一個地方的地理位置,意味着我有一個CenterLatitudeCenterLongitude以及一個radius 在此半徑范圍內,有許多具有不同RegionType類型的區域記錄。 我想通過匹配該radius內每個RegionType的記錄強度來獲取正確的RegionType記錄。

我目前的邏輯是這樣,

步驟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:選擇帶有count Regiontype

$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'];
}

步驟3:確定相關的RegionType

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;
        }

步驟4:提取正確的記錄

$query = $pdo->prepare("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 WHERE (CenterLatitude between $minLat and $maxLat) AND (CenterLongitude between $minLong and $maxLong) AND languageCode = '" . $data['locale'] . "' AND RegionType = '$regionTypeFinal'");
        $query->execute();
        $finalResultRegions = $query->fetchAll(PDO::FETCH_ASSOC);

誰能告訴我如何優化此過程,因為表中缺少記錄,因此該過程很耗時。用一個SQL查詢可以嗎?

沒有測試數據或表布局,因此我無法真正進行測試,但是可以在單條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'] . "' 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM