[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Way to make this mySQL query run faster?

Here's what I'm wanting to do. Run the following query, get the info, display that price range of homes that fit that school, and the number of homes. All of this info can be obtained from the same table.

Can this be rewritten to be much faster? There are quite a few subdivisions.

<?
$sql = "SELECT distinct ELEM_SCHOOL FROM DailyData_CATisRES WHERE $area ORDER BY ELEM_SCHOOL ASC";
$type = "ELEM_SCHOOL";

                  $result = @mysql_query($sql);
                while($row = @mysql_fetch_array($result)) {
                  $SCHOOL = $row["".$type.""];
                      
                        ////////////SELECT ROWS CONTAINING SUBDIVISION/////////////
                        $sqlsub = "SELECT * FROM DailyData_CATisRES WHERE $type = '$SCHOOL' ORDER BY CURRENT_PRICE DESC LIMIT 1";
                        
                            /////////////COUNT NUMBER OF HOMES AVAILABLE IN SUBDIVISION/////////////
                              $sqlsub_count = "SELECT * FROM DailyData_CATisRES WHERE $type = '$SCHOOL'";
                              $count_sub = @mysql_query($sqlsub_count);
                              $numberofsub = @mysql_num_rows($count_sub);

                        /////////////////HIGHEST PRICE & INFO/////////////////////////
                        $resultsub = @mysql_query($sqlsub);
                      while($row = @mysql_fetch_array($resultsub)) {
                       $MAX_PRICE = $row['CURRENT_PRICE'];
                        $BEDROOMS = $row['BEDROOMS'];
                        $BATHS = $row['BATHS'];
                        $SUBDIVISION = $row['SUBDIVISION'];
                        $MEDIA_REMARKS = $row['MEDIA_REMARKS'];
                        $ZIP = $row['ZIP'];
                        $CITY = $row['CITY'];
                        $UDF_PHOTO0 = $row['UDF_PHOTO0'];      
                        $images = $UDF_PHOTO0;
                      $arr_images = explode(",",$images);
                        $URLIT = urlencode($SCHOOL);
                        
                        }
                        /////////////SELECT LOWEST PRICE///////////////
                        $sqlsub2 = "SELECT * FROM DailyData_CATisRES WHERE $type = 'SCHOOL' ORDER BY CURRENT_PRICE ASC LIMIT 1";
                        $resultsub2 = @mysql_query($sqlsub2);
                      while($row = @mysql_fetch_array($resultsub2)) {
                       $MIN_PRICE = $row['CURRENT_PRICE'];
                        }
                  
      include('inc_school_resultlayout.php'); ///////The table displaying the info.
}
?>
0
webseth
Asked:
webseth
  • 2
  • 2
1 Solution
 
k_dietzCommented:
Instead of running the COUNT NUMBER OF HOMES IN SUBDIVISION for every result returned of the previous query, instead load the results into an array:

$sqlsub_count = "SELECT * FROM DailyData_CATisRES";
                              $count_sub = @mysql_query($sqlsub_count);
                              $numberofsub = @mysql_num_rows($count_sub);
 while($row = @mysql_fetch_array($sqlsub_count)) {
       $subDivHomes[$type] = $numberofsub;
}

Then call the $subDivHomes array for the appropriate school value during your subroutine.

You could probably do this for the SELECT LOWEST PRICE query as well.

This would knock off 2 of the queries for every row of your returned results, increasing your script speed dramatically.

If my description didn't quite make sense I can try to clarify more if you need it.
0
 
websethAuthor Commented:
Yes, please help me out a little more. I'm still working on understanding a lot of this stuff!!!!
0
 
k_dietzCommented:
Okay, I'll give you an example using a made up query and you should be able to translate into your own queries (I don't want to assume something is one thing on your SQL query when its another and confuse you).

/* You do this once, rather than a query for every time the previous query runs.  It saves time because you're doing it once for every school named "High School" rather than doing it every time "High School" gets retrieved by the other query */

$sql = "SELECT COUNT (id_field), school_type FROM subdivisions GROUP BY school_type";
$r = @mysql_query($sql);
while ($row = @mysql_fetch_array($sql)) {
     $school_type = $row['school_type'];
     $id_field = $row['id_field'];
     // alternatively you could use extract($row); instead of these two lines above

     $subDivHomes[school_type] = $id_field;  // Save Number of Homes For Each Type

} // end while

// NOW, in your main query:

$sql = "SELECT distinct ELEM_SCHOOL FROM DailyData_CATisRES WHERE $area ORDER BY ELEM_SCHOOL ASC";
$type = "ELEM_SCHOOL";
   $result = @mysql_query($sql);
   while($row = @mysql_fetch_array($result)) {
        $SCHOOL = $row["".$type.""];

        // THIS NEXT LINE REPLACES THE ENTIRE COUNT NUMBER OF HOMES QUERY
        $numberofsub = $subDivHomes[$SCHOOL];
     
         // .... the rest of your data handling

    } // end while


Does this help?
0
 
websethAuthor Commented:
perfect
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now