查询连接的分区表
Query joined partitioned tables
我有 2 个分区的 tables 。我想加入他们,这意味着我想要特定日期的 table 'A' 的所有记录,如果有任何匹配的记录,则 table 'B' 中的数据(也在具体日期)。 table 都是分区的,所以我在 where 子句中使用分区列(时间),但是在这个查询中,当 table 'B' 中没有任何匹配的记录时,条件 'B.time' 变为 null,table 'A' 的记录不会出现在结果中。
如果我不使用 table 'B' 的时间条件,它的执行计划会变成 partition range all 并且性能不佳。
我如何编辑查询?
select * from A left outer join B SSRESP
on A.ID = B.A_ID where
A.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and A.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time >=
to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time <=
to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
有2种可能。
1) 将条件移动到on子句:
select *
from A
left outer join B
on A.ID = B.A_ID
and B.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
where A.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and A.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
2) 过滤左连接前的table:
select *
from A
left outer join
(SELECT * FROM B
WHERE B.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')) AS B
on A.ID = B.A_ID
where A.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and A.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
我有 2 个分区的 tables 。我想加入他们,这意味着我想要特定日期的 table 'A' 的所有记录,如果有任何匹配的记录,则 table 'B' 中的数据(也在具体日期)。 table 都是分区的,所以我在 where 子句中使用分区列(时间),但是在这个查询中,当 table 'B' 中没有任何匹配的记录时,条件 'B.time' 变为 null,table 'A' 的记录不会出现在结果中。 如果我不使用 table 'B' 的时间条件,它的执行计划会变成 partition range all 并且性能不佳。 我如何编辑查询?
select * from A left outer join B SSRESP
on A.ID = B.A_ID where
A.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and A.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time >=
to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time <=
to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
有2种可能。
1) 将条件移动到on子句:
select *
from A
left outer join B
on A.ID = B.A_ID
and B.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
where A.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and A.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
2) 过滤左连接前的table:
select *
from A
left outer join
(SELECT * FROM B
WHERE B.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and B.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')) AS B
on A.ID = B.A_ID
where A.time >= to_date('1396/09/26 00:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')
and A.time <= to_date('1396/10/27 11:00:00',
'yyyy/mm/dd hh24:mi:ss',
'NLS_CALENDAR=persian')