帮助, 我只想从我的数据库中知道count(*), 我有多对多关系表 有100.000 - 150.000记录 所以我使用INNER JOIN,它看起来像是为我工作..
但....
我有一个非常慢这样的查询,它的工作时间约为2-5秒
SELECT COUNT(*) FROM monitoring_pasien
INNER JOIN master_desa ON master_desa.DESA_ID = monitoring_pasien.DESA_ID
INNER JOIN monitoring_loket ON monitoring_pasien.PASIEN_ID = monitoring_loket.PASIEN_ID AND monitoring_pasien.PKM_ID = monitoring_loket.PKM_ID
INNER JOIN monitoring_pelayanan ON monitoring_pelayanan.LOKET_ID = monitoring_loket.LOKET_ID AND monitoring_pelayanan.PKM_ID = monitoring_loket.PKM_ID
INNER JOIN monitoring_diagnosa ON monitoring_diagnosa.PELAYANAN_ID = monitoring_pelayanan.PELAYANAN_ID AND monitoring_diagnosa.PKM_ID = monitoring_pelayanan.PKM_ID
INNER JOIN master_penyakit ON master_penyakit.PENYAKIT_ID = monitoring_diagnosa.PENYAKIT_ID
WHERE monitoring_pasien.DESA_ID < 19 AND master_desa.DESA_ID <> 0 AND master_penyakit.SURVEYLANS = '1';
然后...... 当我在这个查询中使用它时,它会在大约100秒内完成:(
$query = "SELECT * FROM master_desa WHERE DESA_ID < 19 AND DESA_ID <> 0 ORDER BY DESA asc";
$result = mysql_query($query) or die('Error');
while($data = mysql_fetch_array($result))
{
$DESA = $data["DESA"];
$DESA_ID = $data["DESA_ID"];
$queryPasien = "SELECT COUNT(*) AS jumPasien FROM monitoring_pasien
INNER JOIN master_desa ON master_desa.DESA_ID = monitoring_pasien.DESA_ID
INNER JOIN monitoring_loket ON monitoring_pasien.PASIEN_ID = monitoring_loket.PASIEN_ID AND monitoring_pasien.PKM_ID = monitoring_loket.PKM_ID
INNER JOIN monitoring_pelayanan ON monitoring_pelayanan.LOKET_ID = monitoring_loket.LOKET_ID AND monitoring_pelayanan.PKM_ID = monitoring_loket.PKM_ID
INNER JOIN monitoring_diagnosa ON monitoring_diagnosa.PELAYANAN_ID = monitoring_pelayanan.PELAYANAN_ID AND monitoring_diagnosa.PKM_ID = monitoring_pelayanan.PKM_ID
INNER JOIN master_penyakit ON master_penyakit.PENYAKIT_ID = monitoring_diagnosa.PENYAKIT_ID
WHERE master_desa.DESA='$DESA' AND master_penyakit.PENYAKIT_ID = '$penyakit' AND TGL LIKE '$pilihan' ";
$resultPasien = mysql_query($queryPasien) or die('Error');
while($dataPasien = mysql_fetch_array($resultPasien))
{
$jumPasien = $dataPasien['jumPasien'];
if ($jumData == 0) { $persen = "0%"; }
else {
$bagi = $jumPasien/$jumData;
$persen = round((float)$bagi * 100 ) . '%';
}
echo "<tr>
<td>$DESA</td>
<td width=\"40\"> : $persen</td>
<td width=\"70\">";
$loop = floor($persen/2);
$gambar = "images/bar.png";
for($j=0;$j<=$loop;$j++)
{
echo "<img src=\"$gambar\" alt=\"=\" />";
}
echo "</td></tr>";
}
if ($persen >= 0 AND $persen <= 25) { $color = "green"; }
else if ($persen > 25 AND $persen <= 50) { $color = "blue"; }
else if ($persen > 50 AND $persen <= 75) { $color = "yellow"; }
else if ($persen > 75 AND $persen <= 100) { $color = "red"; }
if ($DESA == 'GEDONGAN') { $gedongan_jml=" $jumPasien Penderita [ $persen ]"; $gedongan_color=$color;}
else if ($DESA == 'BALONGSARI') { $balongsari_jml=" $jumPasien Penderita [ $persen ]"; $balongsari_color=$color;}
else if ($DESA == 'BLOOTO') { $blooto_jml=" $jumPasien Penderita [ $persen ]"; $blooto_color=$color;}
}
我的代码有什么问题?
感谢您的帮助
答案 0 :(得分:0)
如果您没有理由INNER JOIN
使用LEFT JOIN
:
SELECT COUNT(*) FROM monitoring_pasien
LEFT JOIN master_desa ON master_desa.DESA_ID = monitoring_pasien.DESA_ID
LEFT JOIN monitoring_loket ON monitoring_pasien.PASIEN_ID = monitoring_loket.PASIEN_ID AND monitoring_pasien.PKM_ID = monitoring_loket.PKM_ID
LEFT JOIN monitoring_pelayanan ON monitoring_pelayanan.LOKET_ID = monitoring_loket.LOKET_ID AND monitoring_pelayanan.PKM_ID = monitoring_loket.PKM_ID
LEFT JOIN monitoring_diagnosa ON monitoring_diagnosa.PELAYANAN_ID = monitoring_pelayanan.PELAYANAN_ID AND monitoring_diagnosa.PKM_ID = monitoring_pelayanan.PKM_ID
LEFT JOIN master_penyakit ON master_penyakit.PENYAKIT_ID = monitoring_diagnosa.PENYAKIT_ID
WHERE monitoring_pasien.DESA_ID < 19 AND master_desa.DESA_ID <> 0 AND master_penyakit.SURVEYLANS = '1';