错误:尚不支持这种类型的相关子查询模式

问题描述 投票:0回答:1

我有一个在 MySQL 上运行没有问题的子查询,我想在 AWS-Redshift 上使用相同的查询,但我收到此错误:

[0A000] ERROR: This type of correlated subquery pattern is not supported yet
,这是查询:

SELECT 
    COALESCE(r.id, r2.id) AS region_id,
FROM 
    search s
LEFT JOIN 
    region r ON s.region_id = r.id
LEFT JOIN 
    region r2 ON r2.id = (SELECT id
                          FROM region AS r
                          WHERE (acos(sin(radians(s.latitude))
                                      * sin(radians(r.latitude))
                                 + cos(radians(s.latitude))
                                    * cos(radians(r.latitude))
                                    * cos(radians(r.longitude) - radians(s.longitude))
                                ) * 3959 < :dis
                            )
                            AND type IN (1)
                            ORDER BY
                            (
                                acos
                                (
                                    sin(radians(s.latitude))
                                    * sin(radians(r.latitude))
                                    + cos(radians(s.latitude))
                                    * cos(radians(r.latitude))
                                    * cos(radians(r.longitude) - radians(s.longitude))
                                )
                                * 3959
                            ) ASC LIMIT 1)
WHERE 
    s.user IS NOT NULL
ORDER BY
    s.date_created DESC; 

到目前为止,我发现这部分代码就是问题所在:

                            ( acos
                                (
                                    sin(radians(s.latitude))
                                    * sin(radians(r.latitude))
                                    + cos(radians(s.latitude))
                                    * cos(radians(r.latitude))
                                    * cos(radians(r.longitude) - radians(s.longitude))
                                ) * 3959 < :dis
                            )
                            AND type IN (1)
                            ORDER BY
                            (
                                acos
                                (
                                    sin(radians(s.latitude))
                                    * sin(radians(r.latitude))
                                    + cos(radians(s.latitude))
                                    * cos(radians(r.latitude))
                                    * cos(radians(r.longitude) - radians(s.longitude))
                                )
                                * 3959
                            )

但我不知道如何在没有子查询的情况下做到这一点。

sql subquery amazon-redshift correlated-subquery
1个回答
0
投票

问题在于 ON 子句中的子查询需要针对每种连接可能性重新评估。在集群数据库上,这是非常昂贵的。因此,您需要将其展平为一组附加的 JOIN 信息,其中包含连接 r 和 r2 所需的所有信息。

我可以尝试一下,但我不知道你的数据,我只是猜测查询中重要的内容。

SELECT 
    COALESCE(r.id, c.id2) AS region_id,
FROM 
    search s
LEFT JOIN 
    region r ON s.region_id = r.id
LEFT JOIN ( SELECT id1, id2
            FROM ( SELECT s.id as id1, x.id as id2, 
                     acos(sin(radians(t.latitude))
                     * sin(radians(x.latitude))
                     + cos(radians(t.latitude))
                     * cos(radians(x.latitude))
                     * cos(radians(x.longitude) - radians(t.longitude))
                     ) * 3959 as calc,
                     ROW_NUMBER() OVER (partition by id1 order by calc asc) as rn
                   FROM region AS x
                   CROSS JOIN search t 
                   WHERE calc < :dis
                     AND type IN (1)
                     AND t.user IS NOT NULL)
            WHERE rn = 1) c 
ON c.id = s.id
WHERE 
    s.user IS NOT NULL
ORDER BY
    s.date_created DESC; 

我无法对此进行测试,因此希望此更改能为您提供一个起点。

注意交叉连接。这是相关子查询的稍微便宜的替代方案。无论哪种方式,您都会计算区域和搜索之间的每个行组合的距离(?),然后找到最小值。

© www.soinside.com 2019 - 2024. All rights reserved.