SQL 查询性能问题子查询和缺少括号

SQL Query Performance Issue sub-query and missing parenthesis

我在下面的查询中有两个问题

  1. 当我尝试 运行ning 查询时,我在 oracle

  2. 中收到以下错误
  3. 性能问题,即当我 运行 没有 IN 子句的查询时,即删除 {select * from cand_profile where postal_code in }在下面的查询中,它需要 15 秒。如何微调此 sql 查询?

查询

select *
from cand_profile 
where postal_code in (
    SELECT ZIP
    FROM (
        SELECT
            dest.ID,
            dest.postal_code AS ZIP,
            ACOS(SIN(RADIANS(src.latitude))*SIN(RADIANS(dest.latitude))+COS(RADIANS(src.latitude))*COS(RADIANS(dest.latitude))*COS(RADIANS(src.longitude)-RADIANS(dest.longitude)))* 3959 AS DISTANCE
        FROM post_codes dest
            CROSS JOIN
        post_codes src
        WHERE src.ID = (
            SELECT ID
            FROM post_codes
            WHERE postal_code = '60195'
            GROUP BY ID
        ) 
        AND ( dest.ID <> src.ID OR  dest.ID = src.ID )
    )
    GROUP BY ID,ZIP,DISTANCE
    HAVING DISTANCE <= 5
    ORDER BY DISTANCE
))

ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis" *Cause:
*Action:

'in' 子句往往效率很低。我会尝试使用连接:

select c.*
from cand_profile c
join (
    SELECT ZIP
    FROM (
        SELECT dest.ID,dest.postal_code AS ZIP, ACOS(SIN ( RADIANS( src.latitude) ) * SIN ( RADIANS ( dest.latitude )) 
+ COS ( RADIANS ( src.latitude)) * COS ( RADIANS ( dest.latitude )) * COS ( RADIANS( src.longitude ) - RADIANS ( dest.longitude ))) * 3959
AS DISTANCE
        FROM post_codes dest
            CROSS JOIN
        post_codes src
        WHERE src.ID = (
            SELECT ID
            FROM post_codes
            WHERE postal_code = '60195'
            GROUP BY ID
        ) 
        AND ( dest.ID <> src.ID OR  dest.ID = src.ID )
    )
    GROUP BY ID,ZIP,DISTANCE
    HAVING DISTANCE <= 5
    ORDER BY DISTANCE
) a
on postal_code = a.ZIP