select isnull(count(1), 0) from tblbizimportinvoice a , tblCRMCustomer b where a.Consignee = b.Id 
                            AND exists(select 1 from tblBizImportDeclarationInvoice b, tblBizImportDeclaration d                         where b.DeclarationNO = d.DeclarationNO and d.Ispay >= 0 and b.InvoiceNO = a.InvoiceNO) 
                            and not exists (select 1 from tblBizImportVATInvoiceDetail c where c.importinvoiceno = a.invoiceno)
                            and b.IsNeedVATInvoice = 1
                            and ((b.IsFreeNeedVATInvoice = 1 AND a.IsFree = 1) OR (a.IsFree <> 1))
这条语句运行要16秒,有什么好的想法替代这条语句呢.

解决方案 »

  1.   

    select 1 
              from tblBizImportDeclarationInvoice b join tblBizImportDeclaration d on b.DeclarationNO = d.DeclarationNO  where d.Ispay >= 0 and ) 写的很模糊,这个b.InvoiceNO = a.InvoiceNO  中 b.InvoiceNO是?换成join连接看看
      

  2.   


     and not exists (select 1 from tblBizImportVATInvoiceDetail c where c.importinvoiceno = a.invoiceno)
    主要是这个select有4W多行的数据量.导致匹配变慢了..
    跟同学研究了好久还是找不到解决方案.求指导.
      

  3.   

    在表 tblBizImportVATInvoiceDetail  建一个关联字段的索引试试。