90210 から 30 マイル以内の郵便番号を検索すると、スクリプトの実行が遅すぎます。郵便番号データベースにも重複がないにもかかわらず、一部が重複していたため、DISTINCT u.id. 必要な列だけを選択する以外に、これをスピードアップする方法についてのアイデアはありますか?
<?php
$zipcode = queryDB("SELECT * FROM zipcodes WHERE zipcode='$location' LIMIT 1", 'a');
$distance = 30;
$sql = "SELECT DISTINCT zipcode, ( 3959 * acos( cos( radians(".$zipcode['lat'].") ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(".$zipcode['lon'].") ) + sin( radians(".$zipcode['lat'].") ) * sin( radians( lat ) ) ) ) AS distance FROM zipcodes HAVING distance <= $distance ORDER BY distance";
$zipcodes = queryDB($sql, 'r');
$sql = '';
while($row = mysqli_fetch_assoc($zipcodes)){
$sql .= "u.category='$category' AND u.zipcode='".$row['zipcode']."' AND u.zipcode = l.zipcode";
if($_GET['photos'] == 'y'){ $sql .= " AND photo_1!=''"; }
$sql .= " OR ";
}
$sql = rtrim($sql, ' OR ');
$profiles = queryDB("SELECT DISTINCT u.id, u.*, l.city, l.state, l.zipcode FROM user_accounts as u, zipcodes as l WHERE $sql ORDER BY date_added DESC", 'r');
while($row = mysqli_fetch_assoc($profiles)){
//this is where i display the profile information
} ?>
更新: 最大の問題は、user_accounts 選択で結合を使用していないことでした。これが私の更新されたSQLクエリです。
$sql = '';
$zipcodes = new ZipCodesRange($location, $distance);
foreach ($zipcodes->zipCodes as $zipcode){
$sql .= "(u.category='$category' AND u.zipcode='".$zipcode['ZIPCODE']."'";
if($_GET['photos'] == 'y'){ $sql .= " AND photo_1!=''"; }
$sql .= ") OR ";
}
$sql = rtrim($sql, ') OR ');
$sql .= ")";
$profiles = queryDB("SELECT u.id, u.photo_1, u.name, u.business, u.date_added, l.city, l.state, l.zipcode FROM user_accounts as u INNER JOIN zipcodes as l ON u.zipcode = l.zipcode WHERE $sql ORDER BY date_added DESC", 'r');