Answer the question
In order to leave comments, you need to log in
A few questions about the conditions?
Greetings, comrades! Do not take it for stupidity, but I have formed a couple of trivial questions to which I can not find a clear answer. Hope you can help me.
1. ON vs WHERE
Question: What works better and faster, multiple relationships in JOIN or conditions in WHERE?
Example:
-- Вариант первый
SELECT
`table_one`.`field_id`,
`table_two`.`field_data`
FROM
`table_one` INNER JOIN `table_two`
ON
`table_one`.`field_one` = `table_two`.`field_one` AND
`table_one`.`field_two` BETWEEN 23 AND 32 AND
`table_two`.`field_two` LIKE '%foobar%'
-- Вариант второй
SELECT
`table_one`.`field_id`,
`table_two`.`field_data`
FROM
`table_one` INNER JOIN `table_two`
ON
`table_one`.`field_one` = `table_two`.`field_one`
WHERE
`table_one`.`field_two` BETWEEN 23 AND 32 AND
`table_two`.`field_two` LIKE '%foobar%'
-- Вариант первый
SELECT *
FROM `table`
WHERE
`field_string` LIKE '%foobar%' AND
`field_integer` BETWEEN 23 AND 32
-- Вариант второй
SELECT *
FROM `table`
WHERE
`field_integer` BETWEEN 23 AND 32 AND
`field_string` LIKE '%foobar%'
-- Вариант первый
SELECT *
FROM `table`
WHERE
condition1 AND
condition2 AND
...
conditionN
-- Вариант второй
SELECT *
FROM `table`
WHERE 1
AND condition1
AND condition2
...
AND conditionN
Answer the question
In order to leave comments, you need to log in
1. ON vs WHERE
Didn't notice the difference (but maybe it exists from version to version). The optimizer freely moves the order of joins, conditions.
2. Order The
optimizer will rewrite anyway, taking into account indexes, joins, having and something else.
3. WHERE 1
has no effect.
You can see how the optimizer rewrote the query by running:
explain extended запрос;
show warnings;
1 Place auxiliary conditions in where, this increases performance
2 The order is not fundamental, and if it affects performance, then it is not at the level to worry about because of it
3 Again, the influence is not great, especially when comparing constants, but if the condition is redundant , it is better to exclude
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question