相关疑难解决方法(0)

需要SQL优化(可能是DISTINCT ON的原因?)

相关的前一个问题:
在按值(而不是列)分组后,从组中选择一个随机条目?

我当前的查询如下所示:

WITH
  points AS (
    SELECT unnest(array_of_points) AS p
  ),

 gtps AS (
   SELECT DISTINCT ON(points.p)
     points.p, m.groundtruth
   FROM measurement m, points
   WHERE st_distance(m.groundtruth, points.p) < distance
   ORDER BY points.p, RANDOM()
 )

SELECT DISTINCT ON(gtps.p, gtps.groundtruth, m.anchor_id)
  m.id, m.anchor_id, gtps.groundtruth, gtps.p
FROM measurement m, gtps
ORDER BY gtps.p, gtps.groundtruth, m.anchor_id, RANDOM()
Run Code Online (Sandbox Code Playgroud)

语义:

  1. 有两个输入值:

    • 第4行:点数组 array_of_points
    • 第12行:双精度数: distance
  2. 第一段(第1-6行):

    • 从points数组创建一个表,用于...
  3. 第二段(第8-14行):

    • 对于points表格内的每个点:从表格中获取一个距离< 的随机(!)groundtruthmeasurementdistance
    • 将这些元组保存在gtps表中
  4. 第三段(第16-19行):

    • 对于表中的每个groundtruthgtps:获取所有anchor_id …

sql postgresql postgis query-optimization aggregate-functions

5
推荐指数
1
解决办法
335
查看次数