Skip to content

Instantly share code, notes, and snippets.

@ronbeltran
Last active March 2, 2020 09:57
Show Gist options
  • Save ronbeltran/7701123 to your computer and use it in GitHub Desktop.
Save ronbeltran/7701123 to your computer and use it in GitHub Desktop.
Need to optimize
$CS2 = mysql_query("SELECT VID, AN FROM Cl WHERE VID = '$VID' ");
while( $row = mysql_fetch_array($CS2) ) {
$AN1 = $row['AN'];
$VID = $row['VID'];
$BA = 0;
$PA = 0;
$TB = 0;
$Ba = 0;
$PHBSelect2 = mysql_query("SELECT AN, BD, BC, SUM(Amt)
FROM BHis
WHERE AN = '$AN1'
AND BD <= '$LockDate'
AND BC = 'P'
GROUP BY AN");
while($row = mysql_fetch_array($PHBSelect2)) {
$PA = $row['SUM(Amt)'];
}
$BHBSelect2 = mysql_query("SELECT AN, BD, BC, SUM(Amt)
FROM BHis
WHERE AN = '$AN1'
AND BD <= '$LockDate'
AND BC = 'B'
GROUP BY AN");
while($row = mysql_fetch_array($BHBSelect2)) {
$BA = $row['SUM(Amt)'];
}
}
@ronbeltran
Copy link
Author

Looking at the two queries above $PHBSelect2 and $BHBSelect2 is almost the same so no need to have two separate query. Integrate the two query into one and do something with the result. It will reduce the time to fetch results from the DB especially if you have millions of records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment