请如何优化下面的SQL,select速度0.1秒(数据量为300条),整个SQL执行为34.4秒。
insert into fx_accountdetails (id, checkcash, invoiceid, detailid, bookid, scale, netamount, amount, checkscale, checkdiscount, checkamount, invoicetype, modifytime) select SYS_GUID(), 'F00049085', v.invoiceid, v.id, v.bookid, v.scale, v.netamount, v.amount, v.scale - nvl(v.accountscale, 0), v.discount, (v.scale - nvl(v.accountscale, 0)) * (v.netamount / v.scale) * v.discount / 100, v.invoicetype, sysdate from d_r_detailview v where v.invoiceid in ('D00062888','D00062889','D00062890','D00062891','D00062893','D00062894','D00062895','D00062896','D00062897','D00062899') and v.scale > nvl(v.accountscale, 0) and not exists (select 1 from fx_accountdetails where checkcash = 'F00049085' and invoiceid = v.invoiceid)

解决方案 »

  1.   

    只查询的话300条记录0.1秒,insert要花34.4秒?
    全部记录有多少条?
    看看fx_accountdetails表上是否有触发器
      

  2.   

    elect速度0.1秒(数据量为3000条),fx_accountdetails(3461776条)
      

  3.   

    把not exists 改成merger试试
      

  4.   

    MERGE into fx_accountdetails A
    USING ( select SYS_GUID(), 'F00049085', v.invoiceid, v.id, v.bookid, v.scale, v.netamount, v.amount, v.scale - nvl(v.accountscale, 0), 
     v.discount, (v.scale - nvl(v.accountscale, 0)) * (v.netamount / v.scale) * v.discount / 100, v.invoicetype, sysdate 
     from d_r_detailview v
      where v.invoiceid in ('D00062888','D00062889','D00062890','D00062891','D00062893','D00062894','D00062895','D00062896','D00062897','D00062899')
       and v.scale > nvl(v.accountscale, 0) )  B
    ON ( checkcash = 'F00049085' and invoiceid = v.invoiceid )
    WHEN MATCHED THEN  INSERT(A.id, A.checkcash, A.invoiceid, A.detailid, A.bookid, A.scale, A.netamount, A.amount, A.checkscale, A.
                              A.checkdiscount, A.checkamount, A.invoicetype, A.modifytime) 
                        VALUES(SYS_GUID(), 'F00049085', B.invoiceid, B.id, B.bookid, B.scale, B.netamount, B.amount, B.scale - nvl(B.accountscale, 0), 
     B.discount, (B.scale - nvl(B.accountscale, 0)) * (B.netamount / B.scale) * B.discount / 100, B.invoicetype, sysdate );
      

  5.   

    有点诡异哦~
    fx_accountdetails 表上面触发器、索引去掉看看~