Is there a difference in these 2 queries (optimization wise) ?
select * from users;
and
select * from users where first_name like '%%' and last_name like '%%'
I am building the query in PHP dynamically using the parameters passed. So, for example..
$first_name_str = "";
if($firstname)
{
$first_name_str = "first_name = '%".$firstname."%' and";
}
$last_name_str = "";
if($lastname)
{
$last_name_str = "last_name = '%".$lastname."%' and";
}
$query =
"select
*
from
users
where
".$first_name_str."
".$last_name_str."
1=1";
The reason i am asking this is because i read that mysql uses only one index while doing a select. So, if i have individual indexes on firstname and lastname, only one will be used. In the case that i have the query as :
select * from users where first_name like '%%' and last_name like '%%'
by default, i can add a concatenated index on both first_name and last_name and searching will be much faster ?
Like '%' is same as Like '%%' or Like '%%%' or LIKE '%%%%'.
To check this yourself just run explain on the query. Se some example queries I ran on my table.
2 points of @Iain are right way to go about performance. But try to locate most of the performance problems in staging using load testing.