OracleÔÚÖ´ÐÐÒ»¸öSQL֮ǰ,Ê×ÏÈÒª·ÖÎöÒ»ÏÂÓï¾äµÄÖ´Ðмƻ®,È»ºóÔÙ°´Ö´Ðмƻ®È¥Ö´ÐС£·ÖÎöÓï¾äµÄÖ´Ðмƻ®µÄ¹¤×÷ÊÇÓÉÓÅ»¯Æ÷(Optimizer)À´Íê³ÉµÄ¡£²»Í¬µÄÇé¿ö,Ò»ÌõSQL¿ÉÄÜÓжàÖÖÖ´Ðмƻ®,µ«ÔÚijһʱµã,Ò»¶¨Ö»ÓÐÒ»ÖÖÖ´Ðмƻ®ÊÇ×îÓŵÄ,»¨·Ñʱ¼äÊÇ×îÉٵġ£ÏàÐÅÄãÒ»¶¨»áÓÃPl/sql Developer¡¢ToadµÈ¹¤¾ßÈ¥¿´Ò»¸öÓï¾äµÄÖ´Ðмƻ®,²»¹ýÄã¿ÉÄܶÔRule¡¢Choose¡¢First rows¡¢All rowsÕ⼸ÏîÓÐÒÉÎÊ,ÒòΪÎÒµ±³õÒ²ÊÇÕâÑùµÄ,ÄÇʱÎÒÒ²ÒÉ»óΪʲôѡÁËÒÔÉϵIJ»Í¬µÄÏî,Ö´Ðмƻ®¾Í±äÁË?
1¡¢ÓÅ»¯Æ÷µÄÓÅ»¯·½Ê½
OracleµÄÓÅ»¯Æ÷¹²ÓÐÁ½ÖÖµÄÓÅ»¯·½Ê½,¼´»ùÓÚ¹æÔòµÄÓÅ»¯·½Ê½(Rule-Based Optimization,¼ò³ÆΪRBO)ºÍ»ùÓÚ´ú¼ÛµÄÓÅ»¯·½Ê½(Cost-Based Optimization,¼ò³ÆΪCBO)¡£
A¡¢RBO·½Ê½£ºÓÅ»¯Æ÷ÔÚ·ÖÎöSQLÓï¾äʱ,Ëù×ñѵÄÊÇOracleÄÚ²¿Ô¤¶¨µÄһЩ¹æÔò¡£±ÈÈçÎÒÃdz£¼ûµÄ,µ±Ò»¸öwhere×Ó¾äÖеÄÒ»ÁÐÓÐË÷Òýʱȥ×ßË÷Òý¡£
B¡¢CBO·½Ê½£ºÒÀ´ÊÒå¿ÉÖª,ËüÊÇ¿´Óï¾äµÄ´ú¼Û(Cost)ÁË,ÕâÀïµÄ´ú¼ÛÖ÷ÒªÖ¸CpuºÍÄÚ´æ¡£ÓÅ»¯Æ÷ÔÚÅжÏÊÇ·ñÓÃÕâÖÖ·½Ê½Ê±,Ö÷Òª²ÎÕÕµÄÊÇ±í¼°Ë÷ÒýµÄͳ¼ÆÐÅÏ¢¡£Í³¼ÆÐÅÏ¢¸ø³ö±íµÄ´óС ¡¢ÓÐÉÙÐС¢Ã¿Ðеij¤¶ÈµÈÐÅÏ¢¡£ÕâЩͳ¼ÆÐÅÏ¢Æð³õÔÚ¿âÄÚÊÇûÓеÄ,ÊÇÄãÔÚ×öanalyzeºó²Å³öÏÖµÄ,ºÜ¶àµÄʱºî¹ýÆÚͳ¼ÆÐÅÏ¢»áÁîÓÅ»¯Æ÷×ö³öÒ»¸ö´íÎóµÄÖ´Ðмƻ®,ÒòЩÎÒÃÇÓ¦¼°Ê±¸üÐÂÕâЩÐÅÏ¢¡£ÔÚOracle8¼°ÒÔºóµÄ°æ±¾,OracleÁÐÍƼöÓÃCBOµÄ·½Ê½¡£
ÎÒÃÇÒªÃ÷ÁË,²»Ò»¶¨×ßË÷Òý¾ÍÊÇÓÅµÄ ,±ÈÈçÒ»¸ö±íÖ»ÓÐÁ½ÐÐÊý¾Ý,Ò»´ÎIO¾Í¿ÉÒÔÍê³ÉÈ«±íµÄ¼ìË÷,¶ø´Ëʱ×ßË÷ÒýʱÔòÐèÒªÁ½´ÎIO,Õâʱ¶ÔÕâ¸ö±í×öÈ«±íɨÃè(full table scan)ÊÇ×îºÃµÄ¡£
2¡¢ÓÅ»¯Æ÷µÄÓÅ»¯Ä£Ê½(Optermizer Mode)
ÓÅ»¯Ä£Ê½°üÀ¨Rule,Choose,First rows,All rowsÕâËÄÖÖ·½Ê½,Ò²¾ÍÊÇÎÒÃÇÒÔÉÏËùÌá¼°µÄ¡£ÈçÏÂÎÒ½âÊÍһϣº
Rule:²»Óöà˵,¼´×ß»ùÓÚ¹æÔòµÄ·½Ê½¡£
Choolse:ÕâÊÇÎÒÃÇÓ¦¹Û×¢µÄ,ĬÈϵÄÇé¿öÏÂOracleÓõıãÊÇÕâÖÖ·½Ê½¡£Ö¸µÄÊǵ±Ò»¸ö±í»ò»òË÷ÒýÓÐͳ¼ÆÐÅÏ¢,Ôò×ßCBOµÄ·½Ê½,Èç¹û±í»òË÷Òýûͳ¼ÆÐÅÏ¢,±íÓÖ²»ÊÇÌرðµÄС,¶øÇÒÏàÓ¦µÄÁÐÓÐË÷Òýʱ,ÄÇô¾Í×ßË÷Òý,×ßRBOµÄ·½Ê½¡£
First Rows:ËüÓëChoose·½Ê½ÊÇÀàËƵÄ,Ëù²»Í¬µÄÊǵ±Ò»¸ö±íÓÐͳ¼ÆÐÅϢʱ,Ëü½«ÊÇÒÔ×î¿ìµÄ·½Ê½·µ»Ø²éѯµÄ×îÏȵļ¸ÐÐ,´Ó×ÜÌåÉϼõÉÙÁËÏìӦʱ¼ä¡£
All Rows:Ò²¾ÍÊÇÎÒÃÇËù˵µÄCostµÄ·½Ê½,µ±Ò»¸ö±íÓÐͳ¼ÆÐÅϢʱ,Ëü½«ÒÔ×î¿ìµÄ·½Ê½·µ»Ø±íµÄËùÓеÄÐÐ,´Ó×ÜÌåÉÏÌá¸ß²éѯµÄÍÌÍÂÁ¿¡£Ã»ÓÐͳ¼ÆÐÅÏ¢Ôò×ß»ùÓÚ¹æÔòµÄ·½Ê½
ÕâÀïÊ×ÏÈ˵ÏÂoracle·ÖÒ³²»Ïñmysql·ÖÒ³ÄÇô¼òµ¥Ö±½Ólimit ¾Í¿ÉÒÔÁË.
mysql£ºsql ="select * from talbe limit 0,10";
µÚÒ»¸öÊý±íʾϱ꣬¾ÍÊÇ´ÓµÚ¼¸ÌõÊý¾Ý¿ªÊ¼£¬
µÚ¶þ¸öÊý±íʾÿҳÏÔʾ¶àÉÙÌõÊý¾Ý£¬¿ÉÒÔ˵Êǵ±ÄãÈ·¶¨Ã¿Ò³ÏÔʾ¶àÉÙÐÐʱºò£¬Ëü¾ÍÊÇÒ»¸ö¹Ì¶¨µÄÊý¡£
ÕâÀïÐèҪעÒâµÄÊÇ£¬mysqlµÄ·ÃÎÊϱêÊÇ´Ó0¿ªÊ¼µÄ¡£Õ ......