等待查询完成(永远不会完成)

Waiting for the query to complete (never completes)

当我 运行 这个查询停留在 'Waiting for the query to complete'

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 中推导出来。我想这可能需要一些时间,但问题是查询永远不会完成。

知道为什么吗?

谢谢!

问题大概出在这里:

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 子句是语法错误。