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.
}
?>
LVL 1
websethAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
websethAuthor Commented:
perfect
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.