【问题标题】:Waiting for the query to complete (never completes)等待查询完成(从不完成)
【发布时间】:2021-03-23 07:26:20
【问题描述】:

当我运行此查询时,卡在“等待查询完成”

with
    cte1 as (
        select
            id as c,
            ST_SetSRID(ST_MakePoint(lo, la), 4326) as u_t1
        from t1
        ),
    cte2 as (
        select
            t2.a1 as c,
            st_union(t2.way) as c1
            from t2
            group by c
        ),
    cte3 as(
            select sum (t3.a2) as sum1              
                from cte1, cte2, t3
                where st_intersects(cte1.u_t1, cte2.c1)
                group by cte2.c1
        )
 select 
    cte3.sum1
    cte2.c  
    from cte3,  cte2

查询的建议是SUM 一个带有ST_INTERSECTS 限制的INT 列。 st_intersect 的属性在 ct1 和 ct2 中推导出来。我想这可能需要一些时间,但问题是查询永远不会完成。

知道为什么吗?

谢谢!

【问题讨论】:

  • 不使用 ANSI 连接语法,您正在创建潜在的巨大笛卡尔积。请在这里花几分钟:w3schools.com/sql
  • 感谢您的回复。我正在自学,虽然有时我需要帮助,但感谢您提供的链接。

标签: postgresql postgis common-table-expression spatial


【解决方案1】:

问题大概出在这里:

cte3 as(
        select sum (t3.a2) as sum1              
            from cte1, cte2, t3
            where st_intersects(cte1.u_t1, cte2.c1)
            group by cte2.c1
    )

您连接三个表,但cte1cte2 之间只有一个连接条件。因此,您将在结果集中(笛卡尔积)中获得来自该连接的行与来自t3 的行的所有可能组合。这可能很大并且是您的问题的原因。

为了避免这个问题,养成使用标准连接语法的习惯:

FROM cte1
   JOIN cte2 ON st_intersects(cte1.u_t1, cte2.c1)
   JOIN t3 ON ...

那么你不能忘记一个连接条件,因为没有ON子句是语法错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 2022-01-21
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2018-11-22
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多