【转】sql中索引不会被用到的几种情况

2017-06-20  本文已影响30人  王帅199207

转自:http://www.cnblogs.com/xixibaby/p/6409928.html

SELECT
    /*+ INDEX(I CIRCLEICONMAST_IX1)*/
    I.ICONNO,
    I.CIRCLEID,
    I.FILEPATH,
    I.REGDT,
    I.FILEPATH || '/' || I.FILENAME IMGNAME,
    I.FILEPATH || '/' || 'th_160_' || I.FILENAME SMALLIMGNAME,
    I.MEMBERID,
    I.ADMCHK STATUS,
    I.ADMCHK ORIGINALSTATUS,
    ROWNUM RN
     FROM CIRCLEICONMAST I
    WHERE I.REGDT BETWEEN TO_DATE('20120619', 'YYYYMMDD') - 10000 AND
      TO_DATE('20120621', 'YYYYMMDD')
      AND NOT EXISTS (
       SELECT C.VALIDFLG 
         FROM CIRCLEMAST C 
        WHERE C.VALIDFLG IN ('N', 'F') 
        AND I.CIRCLEID = C.CIRCLEID)      
      AND I.ADMCHK = 'N'

改成左连接:

SELECT
/*+ INDEX(I CIRCLEICONMAST_IX1)*/
I.ICONNO,
I.CIRCLEID,
I.FILEPATH,
I.REGDT,
I.FILEPATH || '/' || I.FILENAME IMGNAME,
I.FILEPATH || '/' || 'th_160_' || I.FILENAME SMALLIMGNAME,
I.MEMBERID,
I.ADMCHK STATUS,
I.ADMCHK ORIGINALSTATUS,
ROWNUM RN
  FROM CIRCLEICONMAST I, CIRCLEMAST C 
WHERE I.REGDT BETWEEN TO_DATE('20110620', 'YYYYMMDD') AND
       TO_DATE('20120621', 'YYYYMMDD') + 1
   AND C.VALIDFLG NOT IN ('N', 'F') 
   AND I.CIRCLEID = C.CIRCLEID
      
   AND I.ADMCHK = 'N'

总结:oracle中有很多情况会导致index失效,并且走全表扫描的代价是相当大的,所以在写sql的时候一定要注意这个会使索引失效的情况,养成良好的习惯。

上一篇下一篇

猜你喜欢

热点阅读