postgresql 标识冗余排序依据

6jjcrrmo  于 2023-03-17  发布在  PostgreSQL
关注(0)|答案(1)|浏览(99)

在我下面的代码中,我已经注解掉了我认为是多余的排序子句的行。我已经检查了带注解和不带注解的结果(行值),结果是相同的。我只是想知道是否存在这样的情况:我在第二个和第四个CTE中注解掉的两个order by不是真的多余。

speed_dataset as (
select uc_id, imei, points_geom, time_created, st_distance((points_geom::geography),lag(points_geom::geography) over (partition by imei order by time_created))
 / nullif(( EXTRACT(EPOCH FROM time_created) - EXTRACT(EPOCH FROM LAG(time_created) OVER(PARTITION BY imei ORDER BY time_created)))::FLOAT8,0)  as speed
from orig_dataset 
order by imei,time_created 
)
,

subset_speed as (

select uc_id, ROW_NUMBER() OVER (ORDER BY (time_created)) AS row_id, speed, imei,points_geom ,time_created
from speed_dataset sd 
where speed < 0.1 or speed between 0.75 and 2 
--order by time_created 
)
,

leading_speeds as (
select *,lead (speed) over (partition by imei order by time_created) as lead_speed from subset_speed 
)

,

subset_cr as (
select * from leading_speeds 
where 
(
(speed < 0.1 and lead_speed between 0.75 and 2)
or 
(speed between 0.75 and 2 and lead_speed < 0.1)
)
--order by imei,time_created 
)


,

clustering as(
SELECT uc_id,row_id,imei, speed, points_geom ,time_created, ST_ClusterDBSCAN(st_transform(points_geom,24313),eps := 150, minPoints := 3) 
  OVER(ORDER BY row_id) AS cluster_id FROM subset_cr 
)
8hhllhi2

8hhllhi21#

你的直觉是对的,作为一个规则,你应该永远不要在CTE或者视图定义中有ORDER BY,除非你把它和DISTINCT ON (..)或者LIMIT/FETCH FIRST ... ROWS ONLY一起使用。

相关问题