Given the following two queries, which one would be faster (in theory) if there is any difference at all.
SELECT `ViewCounterView`.`id`, `ViewCounterView`.`referer`,
(COUNT(`ViewCounterView`.`id`)) AS `ViewCounterView__referer_count`,
(LOCATE("http", `ViewCounterView`.`referer`) >= 1) AS `ViewCounterView__external`
FROM `dogmatic69`.`view_counter_views` AS `ViewCounterView`
WHERE `ViewCounterView`.`referer` IS NOT NULL
GROUP BY `ViewCounterView`.`referer`
ORDER BY (LOCATE("http", `ViewCounterView`.`referer`) >= 1) desc
LIMIT 20
or
SELECT `ViewCounterView`.`id`, `ViewCounterView`.`referer`,
(COUNT(`ViewCounterView`.`id`)) AS `ViewCounterView__referer_count`,
(LOCATE("http", `ViewCounterView`.`referer`) >= 1) AS `ViewCounterView__external`
FROM `dogmatic69`.`view_counter_views` AS `ViewCounterView`
WHERE `ViewCounterView`.`referer` IS NOT NULL
GROUP BY `ViewCounterView`.`referer`
ORDER BY ViewCounterView__referer_count desc
LIMIT 20
Difference being in the ORDER BY. In the first instance would MySQL know the fragment is being repeated and not calculate again, or in the second one does it use the alias to get the fragment and recalculate the value?
There is no difference: MySQL will calculate the value only once per row. As you do it in the field definition, it is already done.