Does the order of conditions in a WHERE change the efficiency of the query?

4

If I have a query of the type:

SELECT a, b FROM c WHERE a = 5 and b LIKE '%foo%'

And I have another:

SELECT a, b FROM c WHERE b LIKE '%foo%' and a = 5

I have executed both and both have the same execution time, but as for the consumption of resources, is one more efficient than the other?

    
asked by Jorge Arturo Juarez 27.06.2017 в 23:24
source

2 answers

3

Not really, regardless of the order in which you write the WHERE clauses, SQL will prioritize the column by indexes in the table. I would do more stress on LIKE in any case, since in relation to WHERE is less efficient by reading the conditioned column.

In a worse scenario we would have the clauses WHERE that share a function, for example:

WHERE anio = YEAR(date);

In any case, if you are looking to optimize your clauses in the WHERE , first think about using the indexes in the table and do not use functions to compare.

    
answered by 27.06.2017 / 23:52
source
3

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

    
answered by 28.06.2017 в 00:14