Hi all,
I've been struggling with this one for two days straight now and the pile of hair on my desk keeps getting bigger and bigger...
The script task is to display a number of records from the db. Through a form the user can apply some filters which the script uses to dynamicly construct a SQL-query.
The problem is that the query below takes forever to process and my script keeps timing out. Also Taskmanager tells me mysqld-nt.exe consumes 99% of my cpu so that can't be right, can it?
A friend of mine told me the query might multiply the 3 tables, so that would explain the time it's taking. However, i can't think of a better way to pull this off.
Below is the part of the script with the 'dynamic construct' and query. If you need to see more, please let me know.
<?php
// construct condition
$cnr = 0;
if($vpid) {
$condition .= "WHERE e.pid = '$vpid'";
$cnr++;}
if($g) {
if($cnr > 0) {
$condition .= " AND ";
} else {
$condition .= "WHERE ";
}
$condition .= "g.geslacht = '$g'";
$cnr++;}
if($l) {
if($cnr > 0) {
$condition .= " AND ";
} else {
$condition .= "WHERE ";
}
$condition .= "g.leeftijd = '$l'";
$cnr++;
}
$filter = $condition."AND e.cid = g.id AND e.pid = p.id";
// construct ORDER clause
if($order == "p") {
$orderby = "ORDER BY p.naam ASC";
} elseif($order == "e") {
$orderby = "ORDER BY g.email ASC";
} elseif($order == "l") {
$orderby = "ORDER BY g.leeftijd ASC";
} elseif($order == "g") {
$orderby = "ORDER BY g.geslacht ASC";
} elseif($order == "c") {
$orderby = "ORDER BY e.v1 ASC";
} elseif($order == "w") {
$orderby = "ORDER BY e.v2 ASC";
} elseif($order == "b") {
$orderby = "ORDER BY e.v3 ASC";
}
$query = "
SELECT DISTINCT p.naam AS productnaam, g.email, g.geslacht, g.leeftijd, e.id, e.v1, e.v2, e.v3, e.v4
FROM $tb[enqu] e, $tb[gebr] g, $tb[prod] p
INNER JOIN $tb[gebr] ON (e.cid = g.id)
INNER JOIN $tb[prod] ON (e.pid = p.id)
".$condition."
".$orderby."
" or die (mysql_error());
?>
I must be doing something wrong, but what is it?! Any help would be greatly appreciated!
Thanks in advance!