They have the same execution time because both conditions have to be processed for each record
For example:
Since you have a=5
and b LIKE '%foo%'
with the logical expression and
you have to check both for a record to be evaluated correctly
Only when finding the first condition false does not evaluate both conditions.
If we change by a OR
yes that would change the time, in this case
SELECT a, b FROM c WHERE a=5 OR b LIKE '%foo%'
When finding the first true condition, there is no need to continue evaluating the others.
In this case, looking for a=5
is less expensive than b LIKE '%foo%'
, so in this order, the best case records with a=5
will have a shorter search time. If no record complies with the first condition, the second condition would have to be evaluated (Worse of the cases)
We can appreciate this more with more complex functions
SELECT a, b FROM c WHERE a=5 OR funcionCompleja(b) -- Mejor rendimiento
SELECT a, b FROM c WHERE funcionCompleja(b) OR a=5 -- Peor rendimiento
Let's do another exercise
I'm going to create a function that only has one sleep to simulate complexity and always comes back 1
CREATE FUNCTION 'funcionCompleja'() RETURNS int(11)
BEGIN
DO SLEEP(5);
RETURN 1;
END
I'm going to fill in my table with ten data
1 | 5 | basura
2 | 5 | basura
3 | 5 | basura
4 | 5 | basura
5 | 5 | basura
6 | 5 | basura
7 | 5 | basura
8 | 5 | basura
9 | 5 | basura
10 | 5 | basura
Note a
is always 5
If I execute
SELECT a, b FROM c WHERE a=5 OR funcionCompleja(b) = 1
It takes 0.000 sec
If I execute
SELECT a, b FROM c WHERE funcionCompleja(b) = 1 OR a=5
It takes 49,998 sec
Because first execute the funcionCompleja
If I execute
SELECT a, b FROM c WHERE funcionCompleja(b) = 1 AND a=5
It takes 50.014 sec (Although it should be considered that it is always 50 seconds)
Because both are evaluated
If I execute
SELECT a, b FROM c WHERE a=0 and funcionCompleja(b) = 1
It takes 0.000 sec
Because it does not make sense to evaluate the following condition
As a rule you could say that you should ask for the simplest (fastest) before