简体   繁体   English

sql半联接优化

[英]sql semi join optimize

there are two tables 有两个桌子

t1 { id, name, ...}

t2 { t1_id , date_time, parameter, value, ...}

t1 and t2 are oracle partitioned table. t1和t2是oracle分区表。 t2 is large. t2大。

i want to fetch t1 which matches a time range from t2 : 我想获取与t2的时间范围匹配的t1:

select id, name, ... from t1 partition(t1_partition_name) 
where t1.id in( select distinct t1_id from t2 partition(t2_partition_name) 
             where date_time > to_date('20120627 00','YYYYMMDD HH24') 
                   and date_time <to_date('20120627 12','YYYYMMDD HH24')
          )

the sub query would return about 10K t1_id. 子查询将返回大约1万个t1_id。 it is really slow, any suggestion? 这真的很慢,有什么建议吗?

You could try: 您可以尝试:

SELECT id, name, ... 
  FROM t1 partition(t1_partition_name) aa
 WHERE EXISTS (
     SELECT * 
       FROM t2 partition(t2_partition_name) ex
      WHERE ex.t1_id = aa.id
        AND ex.date_time > to_date('20120627 00','YYYYMMDD HH24') 
        AND ex.date_time <to_date('20120627 12','YYYYMMDD HH24' )
     )
  ;

BTW I Don't know anything about Oracle's partition sub-syntax. 顺便说一句,我对Oracle的分区子语法一无所知。 I expect that the partition(tx_partition_name) can be removed completely, since the DBMS should already know this. 我希望可以完全删除partition(tx_partition_name),因为DBMS应该已经知道这一点。 It also collides with the "PARTITION BY" phrase. 它还与“ PARTITION BY”短语冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM