2015-05-01 76 views
-1
select * from non_bidders_report_view 
where applicant_category_id =1314 
and applicant_status_id not in(10,11) 
and partner_id = 4 
and applicant_status_id <> 6 
and applicant_id not in (
          Select apb.applicant_id 
          from applicant_property_bids apb 
          inner join applicants a on 
          a.applicant_id=apb.applicant_id 
     where to_date(apb.bid_Date) >= to_date('30/4/2012','dd/mm/yyyy') 
     and to_date(apb.bid_Date) <= to_date('30/4/2015','dd/mm/yyyy') 
     and a.partner_id = 4 group by apb.applicant_Id 
     union 
     select aba.applicant_Id from Archive_Bid_Applicants aba 
     inner join applicants a on a.applicant_id=aba.applicant_id 
     where to_date(aba.bid_Date) >= to_date('30/4/2012','dd/mm/yyyy') 
     and to_date(aba.bid_Date) <= to_date('30/4/2015','dd/mm/yyyy') 
     and a.partner_id = 4 group by aba.applicant_Id 
     ); 
+4

需要表模式,索引,解釋計劃等 – Mat

+0

而不是'applicant_id不在()'中,使用'not exists'結構,應該更快。或者使用'JOIN'結構,可能會更快。 – HoneyBadger

+0

@ HoneyBadger,謝謝先生,但'不存在'不起作用。 –

回答

1

你可以試試這個查詢:

select * from non_bidders_report_view nb 
    where applicant_category_id = 1314 and partner_id = 4 
    and applicant_status_id not in (6, 10, 11) 
    and not exists (
     select 1 from applicant_property_bids abp 
     join applicants a on a.applicant_id=abp.applicant_id and a.partner_id=4 
      and abp.bid_Date between date '2012-04-30' and date '2015-04-30' 
     where abp.applicant_id = nb.applicant_id) 
    and not exists (
     select 1 from archive_bid_applicants aba 
     join applicants a on a.applicant_id=aba.applicant_id and a.partner_id=4 
      and aba.bid_Date between date '2012-04-30' and date '2015-04-30' 
     where aba.applicant_id = nb.applicant_id) 

的想法是擺脫group byunion這似乎是unnecesary這裏改變not innot exists

替代解決方案:

select * from non_bidders_report_view nb 
    where applicant_category_id = 1314 and partner_id = 4 
    and applicant_status_id not in (6, 10, 11) 
    and not exists (
     select 1 from (
      select applicant_id, bid_date from applicant_property_bids 
      union all 
      select applicant_id, bid_date from archive_bid_applicants 
     ) ab 
     join applicants a on a.applicant_id=ab.applicant_id and a.partner_id=4 
      and ab.bid_Date between date '2012-04-30' and date '2015-04-30' 
     where ab.applicant_id = nb.applicant_id) 
+0

非常感謝,它的工作。 –

+0

出於好奇 - 您選擇哪種查詢,現在運行多長時間? –

+0

@ Ponder Stibbons,我選擇了最後一個,執行時只需要5秒, –

0

如果你有百萬計的數據,然後創建主鍵索引。它會增加你的表現。索引有助於加速數據檢索。在所有3個表上創建索引。

+0

感謝您的快速響應,但已在3張桌子上創建了sir索引。 –

+0

而不是通過apb.applicant_Id **使用**組。使用**選擇不同的apb.applicant_id **。 – Jigar

+0

您還可以使用Sql Developer的解釋計劃功能來優化您的查詢。 – Jigar