【发布时间】:2020-11-21 12:22:36
【问题描述】:
下面的查询只需要很长时间,下面的谓词仅用于获取唯一记录,因此想知道是否有不同的方法来重写相同的查询,而无需多次调用下面的谓词来获取唯一 ID。
select max(c.id) from plocation c where c.ids = y.ids and c.idc = y.idc)
select max(cr.id) from plocation_log cr where cr.ids = yt.ids and cr.idc = yt.idc)
select max(pr.id) from patentpr where pr.ids = p.ids and pr.idc = p.idc)
我的完整示例查询
SELECT to_char(p.pid) AS patentid,
p.name,
x.dept,
y.location
FROM patent p
JOIN pdetails x ON p.pid = x.pid AND x.isactive = 1
JOIN plocation y
ON y.idr = p.idr
AND y.idc = p.idc
AND y.id = *(select max(c.id) from plocation c where c.ids = y.ids and c.idc = y.idc)*
AND y.idopstype in (36, 37)
JOIN plocation_log yt
ON yt.idr = y.idr
AND yt.idc= y.idc
AND yt.id = *(select max(cr.id) from plocation_log cr where cr.ids = yt.ids and cr.idc = yt.idc)*
AND yt.idopstype in (36,37)
WHERE
p.idp IN (10,20,30)
AND p.id = *(select max(pr.id) from patent pr where pr.ids = p.ids and pr.idc = p.idc)*
AND p.idopstype in (36,37)
【问题讨论】:
-
能否详细介绍一下表定义?
-
如果您使用窗口函数而不是相关子查询来改写查询,它将运行得更快。您的 Oracle 版本是否支持 windows 函数(
OVER子句)? -
Oracle 从 8i 开始支持窗口分析功能 :) 我认为没有人使用旧版本
-
向我们展示表格上的索引。
-
@TheImpaler 谢谢。我喜欢这个建议,您是否碰巧有一个带有窗口函数的示例,而不是我的实例的相关子查询,以获取唯一的 ID?
标签: sql oracle query-optimization greatest-n-per-group window-functions