C
C
Cyapa2013-07-19 12:33:01
MySQL
Cyapa, 2013-07-19 12:33:01

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%'

My guesses: Having raped the brain of the google engine a little , I came across the reasoning that the links in the ON section are used in the process of forming the dataset, and the conditions from WHERE filter the already formed dataset. Does that mean there is no difference? Or have I misjudged the complexity? In addition, they say that the optimizer sometimes moves the conditions between sections at its discretion.
2. Order
Question: Does the order of condition concatenation affect performance?
Example:
-- Вариант первый
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%'

My guess: LIKE conditions are quite expensive. In any case, it's more expensive (more expensive?) than comparing numbers. Therefore, it would be logical to assume that it should be done after fields that do not fit the condition with numbers are excluded from the dataset. Is it so? And if so, does the optimizer do the job of ordering the conditions?
3. WHERE 1
Question: Does an empty WHERE 1 or WHERE 1=1 condition affect performance?
Example:
-- Вариант первый
SELECT *
FROM `table`
WHERE
  condition1 AND
  condition2 AND
  ...
  conditionN

-- Вариант второй
SELECT *
FROM `table`
WHERE 1
  AND condition1
  AND condition2
  ...
  AND conditionN

My guesses: It is logical that since this is a condition, then it will work the same as the rest: apply to each field from the dataset. If so, then it turns out that on large data sets we will have a huge number of empty checks? Or, how does it work differently? Or maybe the optimizer will simply remove this condition?
I am especially obsessed with formatting, you can say I have a fad on this, therefore, it is important for me to format the code as pseudo - aesthetically as possible. I recently discovered this "trick" for myself - to start each condition with AND / OR. I think it comes out pretty nice. But if it is to the detriment of performance, then I will have to abandon it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2013-07-19
@melkij

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;

Also, you might be wondering what the optimizer does with where: dev.mysql.com/doc/refman/5.1/en/where-optimizations.html

Z
zednight, 2013-07-19
@zednight

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 question

Ask a Question

731 491 924 answers to any question