I am running a search query but it is taking too long (even when I use limits). I have tried cutting down on the number of joins as well but nothing seems to help. Normally the query takes longer than 3 minutes and the page times out.
Any suggestions? I think it has something to do with the way I am joining the tables?
Any suggestions? I think it has something to do with the way I am joining the tables?
PHP Code:
$sql = "SELECT t1.id_account, t1.mns_file_no, t1.contact_person, t1.company_name1, t2.username, t2.password, ";
if ($countryon) $sql .= "t3.country_name, ";
$sql .= "t1.email, t1.date_last_changed, ";
if ($pmapactiveon) $sql .= "t4.id_pmaps, t4.period_active, t4.pmap_status, ";
$sql .= "t2.user_active FROM tb_account AS t1
LEFT JOIN tb_user AS t2 ON t1.id_account = t2.id_account ";
if ($countryon) $sql .= "LEFT JOIN tb_country AS t3 ON t1.id_country = t3.id_country ";
if ($pmapactiveon) $sql .= "LEFT JOIN tb_account_pmaps as t4
ON t1.id_account = t4.id_account ";
$sql .= "WHERE $where1 $where2 $where3 $where4 $where5 $like1 $like2 $like3
GROUP BY t1.id_account ";
$sql .= "ORDER BY $order_by $order_how $limit";
Comment