sql – 如何重用SELECT,WHERE和ORDER BY子句的结果?

以下查询返回我们附近的场地(lat:62.0,lon:25.0),其中我们按照距离排列的半径:
SELECT *,earth_distance(ll_to_earth(62.0,25.0),ll_to_earth(lat,lon)) AS distance 
FROM venues 
WHERE earth_distance(ll_to_earth(62.0,lon)) <= radius 
ORDER BY earth_distance(ll_to_earth(62.0,lon))

是否可以(并且建议)重复使用earth_distance(ll_to_earth(62.0,lon))的结果,而不是单独为SELECT,WHERE和ORDER BY子句计算它?

解决方法

在GROUP BY和ORDER BY子句中,您可以引用列别名(输出列)或甚至SELECT列表项的序号.我引用 the manual on ORDER BY

Each expression can be the name or ordinal number of an output column
(SELECT list item)
,or it can be an arbitrary expression formed from
input-column values.

大胆强调我的.

但是在WHERE和HAVING子句中,您只能引用基表(输入列)中的列,因此您必须拼出函数调用.

SELECT *,lon)) AS dist
FROM   venues 
WHERE  earth_distance(ll_to_earth(62.0,lon)) <= radius 
ORDER  BY distance;

如果您想知道将计算打包到CTE或子查询中是否更快,只需使用EXPLAIN ANALYZE进行测试即可. (我对此表示怀疑.)

SELECT *
FROM  (
   SELECT *,lon)) AS dist
   FROM   venues
   ) x
WHERE  distance <= radius 
ORDER  BY distance;

@Mike commented一样,通过声明函数STABLE(或IMMUTABLE),您可以通知查询计划程序,对于单个语句中的相同调用,函数调用的结果可以重复使用多次.我引用the manual here

A STABLE function cannot modify the database and is guaranteed to
return the same results given the same arguments for all rows within a
single statement. This category allows the optimizer to optimize
multiple calls of the function to a single call
.

大胆强调我的.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...