鉴于以下两个查询,如果存在任何差异,哪一个会更快(理论上)。
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
或
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
ORDER BY中存在差异。在第一个例子中MySQL会知道片段是重复的而不是再次计算,或者在第二个例子中是否使用别名来获取片段并重新计算值?
答案 0 :(得分:1)
没有区别:MySQL每行只计算一次值。正如您在字段定义中所做的那样,它已经完成。