¡¾×ª¡¿ Ïê½éoracleµÄRBO/CBOÓÅ»¯Æ÷
Ïê½éoracleµÄRBO/CBOÓÅ»¯Æ÷
OracleµÄÓÅ»¯Æ÷ÓÐÁ½ÖÖÓÅ»¯·½Ê½,¼´»ùÓÚ¹æÔòµÄÓÅ»¯·½Ê½(Rule-Based Optimization£¬¼ò³ÆΪRBO)ºÍ»ùÓÚ´ú¼ÛµÄÓÅ»¯·½Ê½(Cost-Based Optimization£¬¼ò³ÆΪCBO)£¬ÔÚOracle8¼°ÒÔºóµÄ°æ±¾,OracleÇ¿ÁÐÍƼöÓÃCBOµÄ·½Ê½
RBO·½Ê½£ºÓÅ»¯Æ÷ÔÚ·ÖÎöSQLÓï¾äʱ,Ëù×ñѵÄÊÇOracleÄÚ²¿Ô¤¶¨µÄһЩ¹æÔò¡£±ÈÈçÎÒÃdz£¼ûµÄ£¬µ±Ò»¸öwhere×Ó¾äÖеÄÒ»ÁÐÓÐË÷Òýʱȥ×ßË÷Òý¡£
CBO·½Ê½£ºËüÊÇ¿´Óï¾äµÄ´ú¼Û(Cost),ÕâÀïµÄ´ú¼ÛÖ÷ÒªÖ¸CpuºÍÄÚ´æ¡£ÓÅ»¯Æ÷ÔÚÅжÏÊÇ·ñÓÃÕâÖÖ·½Ê½Ê±,Ö÷Òª²ÎÕÕµÄÊÇ±í¼°Ë÷ÒýµÄͳ¼ÆÐÅÏ¢¡£Í³¼ÆÐÅÏ¢¸ø³ö±íµÄ´óС¡¢ÓÐÉÙÐС¢Ã¿Ðеij¤¶ÈµÈÐÅÏ¢¡£ÕâЩͳ¼ÆÐÅÏ¢Æð³õÔÚ¿âÄÚÊÇûÓеģ¬ÊÇ×öanalyzeºó²Å³öÏֵģ¬ºÜ¶àµÄʱºî¹ýÆÚͳ¼ÆÐÅÏ¢»áÁîÓÅ»¯Æ÷×ö³öÒ»¸ö´íÎóµÄÖ´Ðмƻ®,ÒòЩӦ¼°Ê±¸üÐÂÕâЩÐÅÏ¢¡£
×¢Ò⣺×ßË÷Òý²»Ò»¶¨¾ÍÊÇÓŵģ¬±ÈÈçÒ»¸ö±íÖ»ÓÐÁ½ÐÐÊý¾Ý£¬Ò»´ÎIO¾Í¿ÉÒÔÍê³ÉÈ«±íµÄ¼ìË÷,¶ø´Ëʱ×ßË÷ÒýʱÔòÐèÒªÁ½´ÎIO,Õâʱȫ±íɨÃè(full table scan)ÊÇ×îºÃ
ÓÅ»¯Ä£Ê½°üÀ¨Rule¡¢Choose¡¢First rows¡¢All rowsËÄÖÖ·½Ê½£º
Rule£º»ùÓÚ¹æÔòµÄ·½Ê½¡£
Choolse£ºÄ¬ÈϵÄÇé¿öÏÂOracleÓõıãÊÇÕâÖÖ·½Ê½¡£Ö¸µÄÊǵ±Ò»¸ö±í»ò»òË÷ÒýÓÐͳ¼ÆÐÅÏ¢£¬Ôò×ßCBOµÄ·½Ê½£¬Èç¹û±í»òË÷Òýûͳ¼ÆÐÅÏ¢£¬±íÓÖ²»ÊÇÌرðµÄС£¬¶øÇÒÏàÓ¦µÄÁÐÓÐË÷Òýʱ£¬ÄÇô¾Í×ßË÷Òý£¬×ßRBOµÄ·½Ê½¡£
First Rows£ºËüÓëChoose·½Ê½ÊÇÀàËƵģ¬Ëù²»Í¬µÄÊǵ±Ò»¸ö±íÓÐͳ¼ÆÐÅϢʱ£¬Ëü½«ÊÇÒÔ×î¿ìµÄ·½Ê½·µ»Ø²éѯµÄ×îÏȵļ¸ÐУ¬´Ó×ÜÌåÉϼõÉÙÁËÏìӦʱ¼ä¡£
All Rows:Ò²¾ÍÊÇÎÒÃÇËù˵µÄCostµÄ·½Ê½£¬µ±Ò»¸ö±íÓÐͳ¼ÆÐÅϢʱ£¬Ëü½«ÒÔ×î¿ìµÄ·½Ê½·µ»Ø±íµÄËùÓеÄÐУ¬´Ó×ÜÌåÉÏÌá¸ß²éѯµÄÍÌÍÂÁ¿¡£Ã»ÓÐͳ¼ÆÐÅÏ¢Ôò×ßRBOµÄ·½Ê½¡£
É趨ѡÓÃÄÄÖÖÓÅ»¯Ä£Ê½£º
A¡¢Instance¼¶±ðÎÒÃÇ¿ÉÒÔͨ¹ýÔÚinitSID.oraÎļþÖÐÉ趨OPTIMIZER_MODE=RULE/CHOOSE/FIRST_ROWS/ALL_ROWSÈç¹ûûÉ趨OPTIMIZER_MODE²ÎÊýÔòĬÈÏÓõÄÊÇChoose·½Ê½¡£
B¡¢Sessions¼¶±ðͨ¹ýALTER SESSION SET OPTIMIZER_MODE=RULE/CHOOSE/FIRST_ROWS/ALL_ROWSÀ´É趨¡£
C¡¢Óï¾ä¼¶±ðÓÃHint£¨/*+ ... */£©À´É趨
Ϊʲô±íµÄij¸ö×Ö¶ÎÃ÷Ã÷ÓÐË÷Òý£¬µ«Ö´Ðмƻ®È´²»×ßË÷Òý£¿
1¡¢ÓÅ»¯Ä£Ê½ÊÇall_rowsµÄ·½Ê½
&
Ïà¹ØÎĵµ£º
ÔÚSQLÓï¾äÓÅ»¯¹ý³ÌÖУ¬¾³£»áÓõ½hint,
ÒÔÏÂÊÇÔÚSQLÓÅ»¯¹ý³ÌÖг£¼ûOracleÖÐ"HINT"µÄ30¸öÓ÷¨1. /*+ALL_ROWS*/
±íÃ÷¶ÔÓï¾ä¿éÑ¡Ôñ»ùÓÚ¿ªÏúµÄÓÅ»¯·½·¨,²¢»ñµÃ×î¼ÑÍÌÍÂÁ¿,ʹ×ÊÔ´ÏûºÄ×îС»¯.
ÀýÈç:
SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_IN from BSEMPMS WHERE EMP_NO='SCOTT';
2. /*+FIRST_ROWS*/
±íÃ÷¶ÔÓï¾ä¿éÑ¡Ôñ ......
ÎÒÃÇÒª×öµ½²»µ«»áдSQL,»¹Òª×öµ½Ð´³öÐÔÄÜÓÅÁ¼µÄSQL,ÒÔÏÂΪ±ÊÕßѧϰ¡¢ÕªÂ¼¡¢²¢»ã×ܲ¿·Ö×ÊÁÏÓë´ó¼Ò·ÖÏí£¡
£¨1£© Ñ¡Ôñ×îÓÐЧÂʵıíÃû˳Ðò(Ö»ÔÚ»ùÓÚ¹æÔòµÄÓÅ»¯Æ÷ÖÐÓÐЧ)£º
ORACLEµÄ½âÎöÆ÷°´ÕÕ´ÓÓÒµ½×óµÄ˳Ðò´¦Àífrom×Ó¾äÖеıíÃû£¬from×Ó¾äÖÐдÔÚ×îºóµÄ±í(»ù´¡±í driving table)½«±»×îÏÈ´¦Àí£ ......
½ñÌìÔÚ¿ª·¢µÄʱºòÐèÒª²éѯÊý¾Ý¿âÖÐÄÄЩѧÉúÓжà¸öѧºÅ£¬Ð´µÄsqlÈçÏ£º
select * from(
select count(t1.xh) as xhs,t2.uuid,t3.xm as xsxm,t3.sfzjh, t1.xjzt as xjzt from hust_xs_jb t1
INNER JOIN hust_xx_identity t2 on t1.sfid ......
oracleȨÏÞÌåϵÖÐÓиödefault role£¬±È½ÏÄÑÒÔÀí½â¡£ÏÂÃæÓÃʵÀý˵Ã÷Ò»ÏÂ×÷Óá£
ÎÒÃÇ¿ÉÒÔ¸øij¸öÓû§·ÖÅäһЩ½ÇÉ«£¬±ÈÈçrole r1,r2,r3,r4£¬¶øÆäÖпÉÒÔ½«Ä³Ð©½ÇÉ«±ÈÈçr1ÉèÖÃΪdefault role£¬ÆäËûµÄ²»ÉèÖóÉdefault role£¬ÕâÑù£¬µ±¸ÃÓû§µÇ¼ʱ£¬×Ô¶¯¾ßÓÐdefault roleÖÐËù°üº¬µÄȨÏÞ£¬ÆäËûµÄ½ÇÉ«Ëù¾ßÓеÄȨÏÞҪͨ¹ýset role ½ÇÉ ......
Ò»¡¢²é¿´Êý¾Ý¿âÔËÐÐģʽ
¡¡¡¡¿ÉÒÔÓ󬼶Óû§£¨INTERNAL£©ÔÚSQLPLUSÖÐʹÓÃÃüÁîARCHIVE LOG LIST²é¿´
¡¡¡¡SQL> archive log list
¡¡¡¡Database log mode No Archive Mode
¡¡¡¡Automatic archivalDisabled
¡¡¡ ......