【发布时间】:2018-09-04 17:50:58
【问题描述】:
我有父表,可以说 P,带有 Id、Name 和 Type。
P(
id (pk),
name,
type (type in (1..10) )
)
我有一个引用 P 的子表,假设该表称为 B:
B (
id,
date,
other columns,
parent_id
)
现在问题来了:表P 没有那么多记录,但表B 确实很大。
如果是Type = 3,则B 中的日期对于父记录将始终相同。
所以我想从P 和type = 3 中获取所有记录,并从表B 中获取相应的日期。考虑到 B 很大,最好的方法是什么?
我想写一些类似的东西
Select b.parent_id, p.name, max(b.date)
from B b
join P p on p.id = b.parent_id
group by b.parent_id, p.name
where p.type = 3
此处max(b.date) 或min(b.date) 无关紧要,因为日期相同。
但是表B 很大。
仅从P 中选择并加入B 不是更好吗,例如“具有最小 id 的子行”,或者基本上是任何子行?
Select p.id, p.name, b.date
from P p
join B b on (p.id = b.parent_id and "take only first matching row")
where p.type = 3
【问题讨论】:
-
在 Oracle 12c 中使用
FETCH NEXT 1 ROWS ONLY从 B 中仅获取 1 行。 -
非常感谢,我们有 Oracle 12。你能写出这个 Join 的样子吗?或者可能是整个查询?
-
B 有多“巨大”? B 上的索引是什么?
-
B 有大约 1 亿行。 P 有 ~ 6,000 行。
标签: sql oracle query-optimization