$networks is the string with multiple values seperated with Commas. Example :
$networks = "Programming,Movies,Hollywood". (There can be around 150 values or more.)
Now the Mysql query goes here :
$query = "SELECT * FROM table
WHERE ( isActive = 1 AND
isDeleted = 0 AND
onid = '0' AND
question = 0 AND
( ".$networkqur." )
)
ORDER BY id DESC
LIMIT 0,100;
Here $networkqur is :
$network = explode(',',$networks);
$networkqur = '';
for( $i = 0; $i < count($network); $i++ ){
$networkqur .= 'networks LIKE "%'.$network[$i].'%"';
if( $i != count($network) - 1 ){
$networkqur .= ' OR ';
}
}
So effectively $networkqur becomes :
$networkqur = "networks LIKE "%Programming%" OR
networks LIKE "%Movies%" OR
networks LIKE "%Hollywood%";
And the Effective Query Becomes :
$query = "SELECT * FROM posts
WHERE ( isActive = 1 AND
isDeleted = 0 AND
onid = '0' AND
question = 0 AND
( networks LIKE "%Programming%" OR
networks LIKE "%Movies%" OR
networks LIKE "%Hollywood% )
)
ORDER BY id DESC
LIMIT 0,100;
Now as we can see there can be 150 OR Conditionals in the query and multiple AND conditionals with Sorting at the end.
Can there be any replacement of this particular query or any other faster way to execute it?