oracle语句优化30例子

SQLÓï¾äÓÅ»¯·½·¨30Àý

ÔÚSQLÓï¾äÓÅ»¯¹ý³ÌÖУ­ÎÒÃǾ�³£»áÓõ½hint,ÏÖ×ܽáÒ»ÏÂÔÚSQLÓÅ»¯¹ý³ÌÖг£¼ûOracle HINTµÄÓ÷¨£º

1. /*+ALL_ROWS*/ 
±íÃ÷��Óï¾ä¿éÑ¡Ôñ»ùÓÚ¿ªÏúµÄÓÅ»¯·½·¨,²¢»ñµÃ×î¼ÑÍÌÍÂÁ¿,ʹ×ÊÔ´ÏûºÄ×îС»¯. 
ÀýÈç: 
SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT'; 
2. /*+FIRST_ROWS*/ 
±íÃ÷��Óï¾ä¿éÑ¡Ôñ»ùÓÚ¿ªÏúµÄÓÅ»¯·½·¨,²¢»ñµÃ×î¼ÑÏìӦʱ¼ä,ʹ×ÊÔ´ÏûºÄ×îС»¯. 
ÀýÈç: 
SELECT /*+FIRST_ROWS*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT'; 

3. /*+CHOOSE*/ 
±íÃ÷Èç¹ûÊý¾Ý×ÖµäÖÐÓзÃÎʱíµÄͳ¼ÆÐÅÏ¢,½«»ùÓÚ¿ªÏúµÄÓÅ»¯·½·¨,²¢»ñµÃ×î¼ÑµÄÍÌÍÂÁ¿; 
±íÃ÷Èç¹ûÊý¾Ý×ÖµäÖÐûÓзÃÎʱíµÄͳ¼ÆÐÅÏ¢,½«»ùÓÚ¹æÔò¿ªÏúµÄÓÅ»¯·½·¨; 
ÀýÈç: 
SELECT /*+CHOOSE*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT'; 

4. /*+RULE*/ 
±íÃ÷��Óï¾ä¿éÑ¡Ôñ»ùÓÚ¹æÔòµÄÓÅ»¯·½·¨. 
ÀýÈç: 
SELECT /*+ RULE */ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT'; 

5. /*+FULL(TABLE)*/ 
±íÃ÷��±íÑ¡ÔñÈ«¾ÖɨÃèµÄ·½·¨. 
ÀýÈç: 
SELECT /*+FULL(A)*/ EMP_NO,EMP_NAM FROM BSEMPMS A WHERE EMP_NO='SCOTT'; 

6. /*+ROWID(TABLE)*/ 
ÌáʾÃ÷È·±íÃ÷��Ö¸��±í¸ù¾ÝROWID½øÐзÃÎÊ. 
ÀýÈç: 
SELECT /*+ROWID(BSEMPMS)*/ * FROM BSEMPMS WHERE ROWID>='AAAAAAAAAAAAAA' 
AND EMP_NO='SCOTT'; 

7. /*+CLUSTER(TABLE)*/ 
ÌáʾÃ÷È·±íÃ÷��Ö¸��±íÑ¡Ôñ´ØɨÃèµÄ·ÃÎÊ·½·¨,ËüÖ»��´Ø��ÏóÓÐЧ. 
ÀýÈç: 
SELECT /*+CLUSTER */ BSEMPMS.EMP_NO,DPT_NO FROM BSEMPMS,BSDPTMS 
WHERE DPT_NO='TEC304' AND BSEMPMS.DPT_NO=BSDPTMS.DPT_NO; 

8. /*+INDEX(TABLE INDEX_NAME)*/ 
±íÃ÷��±íÑ¡ÔñË÷ÒýµÄɨÃè·½·¨. 
ÀýÈç: 
SELECT /*+INDEX(BSEMPMS SEX_INDEX) USE SEX_INDEX BECAUSE THERE ARE FEWMALE BSEMPMS */ FROM BSEMPMS WHERE SEX='M'; 

9. /*+INDEX_ASC(TABLE INDEX_NAME)*/ 
±íÃ÷��±íÑ¡ÔñË÷ÒýÉýÐòµÄɨÃè·½·¨. 
ÀýÈç: 
SELECT /*+INDEX_ASC(BSEMPMS PK_BSEMPMS) */ FROM BSEMPMS WHERE DPT_NO='SCOTT'; 

10. /*+INDEX_COMBINE*/ 
Ϊָ��±íÑ¡Ôñλͼ·ÃÎÊ·¾�,Èç¹ûINDEX_COMBINEÖÐûÓÐÌṩ×÷Ϊ²ÎÊýµÄË÷Òý,½«Ñ¡Ôñ³öλͼË÷ÒýµÄ²¼��×éºÏ·½Ê½. 
ÀýÈç: 
SELECT /*+INDEX_COMBINE(BSEMPMS SAL_BMI HIREDATE_BMI)*/ * FROM BSEMPMS 
WHERE SAL<5000000 AND HIREDATE<SYSDATE; 

11. /*+INDEX_JOIN(TABLE INDEX_NAME)*/ 
ÌáʾÃ÷È·ÃüÁîÓÅ»¯Æ÷ʹÓÃË÷Òý×÷Ϊ·ÃÎÊ·¾�. 
ÀýÈç: 
SELECT /*+INDEX_JOIN(BSEMPMS SAL_HMI HIREDATE_BMI)*/ SAL,HIREDATE 
FROM BSEMPMS WHERE SAL<60000; 

12. /*+INDEX_DESC(TABLE INDEX_NAME)*/ 
±íÃ÷��±íÑ¡ÔñË÷Òý½µÐòµÄɨÃè·½·¨. 
ÀýÈç: 
SELECT /*+INDEX_DESC(BSEMPMS PK_BSEMPMS) */ FROM BSEMPMS WHERE DPT_NO='SCOTT'; 

13. /*+INDEX_FFS(TABLE INDEX_NAME)*/ 
��Ö¸��µÄ±íÖ´ÐпìËÙÈ«Ë÷ÒýɨÃè,��²»ÊÇÈ«±íɨÃèµÄ°ì·¨. 
ÀýÈç: 
SELECT /*+INDEX_FFS(BSEMPMS IN_EMPNAM)*/ * FROM BSEMPMS WHERE DPT_NO='TEC305'; 

14. /*+ADD_EQUAL TABLE INDEX_NAM1,INDEX_NAM2,...*/ 
ÌáʾÃ÷È·½øÐÐÖ´Ðй滮µÄÑ¡Ôñ,½«¼¸¸öµ¥ÁÐË÷ÒýµÄɨÃèºÏÆðÀ´. 
ÀýÈç: 
SELECT /*+INDEX_FFS(BSEMPMS IN_DPTNO,IN_EMPNO,IN_SEX)*/ * FROM BSEMPMS WHERE EMP_NO='SCOTT' AND DPT_NO='TDC306'; 

15. /*+USE_CONCAT*/ 
��²éѯÖеÄWHEREºóÃæµÄORÌõ¼þ½øÐÐת»»ÎªUNION ALLµÄ×éºÏ²éѯ. 
ÀýÈç: 
SELECT /*+USE_CONCAT*/ * FROM BSEMPMS WHERE DPT_NO='TDC506' AND SEX='M'; 

16. /*+NO_EXPAND*/ 
��ÓÚWHEREºóÃæµÄOR »òÕßIN-LISTµÄ²éѯÓï¾ä,NO_EXPAND½«×èÖ¹Æä»ùÓÚÓÅ»¯Æ÷��Æä½øÐÐÀ©Õ¹. 
ÀýÈç: 
SELECT /*+NO_EXPAND*/ * FROM BSEMPMS WHERE DPT_NO='TDC506' AND SEX='M'; 

17. /*+NOWRITE*/ 
½ûÖ¹��²éѯ¿éµÄ²éѯÖØд²Ù×÷. 

18. /*+REWRITE*/ 
¿ÉÒÔ½«ÊÓͼ×÷Ϊ²ÎÊý. 

19. /*+MERGE(TABLE)*/ 
Äܹ»��ÊÓͼµÄ¸÷¸ö²éѯ½øÐÐÏàÓ¦µÄºÏ²¢. 
ÀýÈç: 
SELECT /*+MERGE(V) */ A.EMP_NO,A.EMP_NAM,B.DPT_NO FROM BSEMPMS A (SELET DPT_NO 
,AVG(SAL) AS AVG_SAL FROM BSEMPMS B GROUP BY DPT_NO) V WHERE A.DPT_NO=V.DPT_NO 
AND A.SAL>V.AVG_SAL; 

20. /*+NO_MERGE(TABLE)*/ 
��ÓÚÓпɺϲ¢µÄÊÓͼ²»Ôٺϲ¢. 
ÀýÈç: 
SELECT /*+NO_MERGE(V) */ A.EMP_NO,A.EMP_NAM,B.DPT_NO FROM BSEMPMS A (SELECT DPT_NO,AVG(SAL) AS AVG_SAL FROM BSEMPMS B GROUP BY DPT_NO) V WHERE A.DPT_NO=V.DPT_NO AND A.SAL>V.AVG_SAL; 

21. /*+ORDERED*/ 
¸ù¾Ý±í³öÏÖÔÚFROMÖеÄ˳Ðò,ORDEREDʹORACLEÒÀ´Ë˳Ðò��ÆäÁ­½Ó. 
ÀýÈç: 
SELECT /*+ORDERED*/ A.COL1,B.COL2,C.COL3 FROM TABLE1 A,TABLE2 B,TABLE3 C WHERE A.COL1=B.COL1 AND B.COL1=C.COL1; 

22. /*+USE_NL(TABLE)*/ 
½«Ö¸��±íÓëÇ�Ì×µÄÁ­½ÓµÄÐÐÔ´½øÐÐÁ­½Ó,²¢°ÑÖ¸��±í×÷ΪÄÚ²¿±í. 
ÀýÈç: 
SELECT /*+ORDERED USE_NL(BSEMPMS)*/ BSDPTMS.DPT_NO,BSEMPMS.EMP_NO,BSEMPMS.EMP_NAM FROM BSEMPMS,BSDPTMS WHERE BSEMPMS.DPT_NO=BSDPTMS.DPT_NO; 

23. /*+USE_MERGE(TABLE)*/ 
½«Ö¸��µÄ±íÓëÆäËûÐÐԴͨ¹ýºÏ²¢ÅÅÐòÁ­½Ó·½Ê½Á­½ÓÆðÀ´. 
ÀýÈç: 
SELECT /*+USE_MERGE(BSEMPMS,BSDPTMS)*/ * FROM BSEMPMS,BSDPTMS WHERE BSEMPMS.DPT_NO=BSDPTMS.DPT_NO; 

24. /*+USE_HASH(TABLE)*/ 
½«Ö¸��µÄ±íÓëÆäËûÐÐԴͨ¹ý¹þÏ£Á­½Ó·½Ê½Á­½ÓÆðÀ´. 
ÀýÈç: 
SELECT /*+USE_HASH(BSEMPMS,BSDPTMS)*/ * FROM BSEMPMS,BSDPTMS WHERE BSEMPMS.DPT_NO=BSDPTMS.DPT_NO; 

25. /*+DRIVING_SITE(TABLE)*/ 
Ç¿ÖÆÓëORACLEËùÑ¡ÔñµÄλÖò»Í­µÄ±í½øÐвéѯִÐÐ. 
ÀýÈç: 
SELECT /*+DRIVING_SITE(DEPT)*/ * FROM BSEMPMS,DEPT@BSDPTMS WHERE BSEMPMS.DPT_NO=DEPT.DPT_NO; 

26. /*+LEADING(TABLE)*/ 
½«Ö¸��µÄ±í×÷ΪÁ­½Ó´ÎÐòÖеÄÊ×±í. 

27. /*+CACHE(TABLE)*/ 
µ±½øÐÐÈ«±íɨÃèʱ,CACHEÌáʾÄܹ»½«±íµÄ¼ìË÷¿é·ÅÖÃÔÚ»º³åÇø»º´æÖÐ×î½ü×îÉÙÁбíLRUµÄ×î½üʹÓÃ�� 
ÀýÈç: 
SELECT /*+FULL(BSEMPMS) CAHE(BSEMPMS) */ EMP_NAM FROM BSEMPMS; 

28. /*+NOCACHE(TABLE)*/ 
µ±½øÐÐÈ«±íɨÃèʱ,CACHEÌáʾÄܹ»½«±íµÄ¼ìË÷¿é·ÅÖÃÔÚ»º³åÇø»º´æÖÐ×î½ü×îÉÙÁбíLRUµÄ×î½üʹÓÃ�� 
ÀýÈç: 
SELECT /*+FULL(BSEMPMS) NOCAHE(BSEMPMS) */ EMP_NAM FROM BSEMPMS; 

29. /*+APPEND*/ 
Ö±½Ó²åÈëµ½±íµÄ×îºó,¿ÉÒÔÌá¸ßËÙ��. 
insert /*+append*/ into test1 select * from test4 ; 

30. /*+NOAPPEND*/ 
ͨ¹ýÔÚ²åÈëÓï¾äÉú´æÆÚÄÚÍ£Ö¹²¢ÐÐģʽÀ´Æô��³£¹æ²åÈë. 
insert /*+noappend*/ into test1 select * from test4 ; 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值