I have 4 tables. Users, Articles, Langs, LangList
Articles table: …, uid (Users.id), lang (Langs.id),…
LangList table: id, uid (Users.id), langid (Langs.id)
I want to select all those articles for a specified user (ex. Users.id = 66), where are in his selected languages list.
If i make this:
function FindActiveLangs($lang){
$sql ="SELECT langid FROM LangList WHERE `uid`= '66' AND `active`='1';";
$rsd = mysql_query($sql);
$lang_string = " ( ".$lang."lang = '0' ";
while($row = mysql_fetch_array($rsd)){
$lang_string .= " OR ".$lang."lang = '".$row['langid']."' ";
}
$lang_string .= " ) ";
return $lang_string;
} //end of FindActiveLangs
$tables = " Articles ";
$lang_string = FindActiveLangs("Articles.");
$where1 = "WHERE ".$lang_string." ";
$sql = "select distinct Articles.id, title, descr, thumb, relDate,
Articles.alias, Articles.lang, author, source
from ".$tables.$where1." and Users";
I took the results in 0.01-0.03 secs
In the second way, that I think is better for clean code:
$sql = "select distinct Articles.id, title, descr, thumb, relDate,
Articles.alias, Articles.lang, author, source
from Articles
where lang IN
(SELECT langid FROM LangList WHERE uid= '66' AND active='1')";
I took the results in 1.1-1.3 secs
Is there any way to execute this query, with second way with better performance? the “IN” kills the speed
Thanks in advance…
look at EXISTS instead of IN. The performance is much better.
EDIT: second attempt (check if the performance is any better):