我在以下查询中有两个问题
当我尝试运行查询时,我在oracle中收到以下错误
性能问题,即当我运行没有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:缺少右括号 00907. 00000 - “缺少右括号” *原因:
*操作:
答案 0 :(得分:1)
'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