Sql 如何优化此查询,执行时需要40分钟?

Sql 如何优化此查询,执行时需要40分钟?,sql,oracle,Sql,Oracle,您可以尝试以下查询: 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.applic

您可以尝试以下查询:

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 
        );
我们的想法是要摆脱群体和联盟,这在这里似乎是不必要的,并改变为不存在

替代解决方案:

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 )

如果您有数百万个数据,则在主键上创建索引。这会提高你的表现。索引有助于加快数据检索。在所有3个表上创建索引。

需要表架构、索引、解释计划等。与申请人id不在中相比,使用不存在的结构应该更快。或者使用连接构造,可能会更快。@HoneyBadger,谢谢您,先生,但“不存在”不起作用。任何在没有看到跟踪文件或索引信息的情况下向您提供任何信息的人都只是在进行最佳实践。跟踪查询并查看挂起的位置。@mmmmm我大体上同意。但在这个特殊的查询中,我注意到有两个分区分组,然后这些分区合并。所有这些步骤都不是必需的。根据OP的话,在明显的更正后,查询在5秒内完成。感谢您的快速响应,但sir索引已经在3个表上创建。而不是使用**group by apb.申请人\ U Id**。使用**select distinct apb.applicator\u id**。您也可以使用Sql Developer的解释计划功能来优化您的查询。你好,Anand,有没有成功?您是如何实现的,现在花了多少时间?出于好奇-您选择了哪个查询,现在运行了多长时间?@Pounder Stibons,我选择的最后一个查询,执行只需5秒,
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 )