Oracle Index µÄÈý¸öÎÊÌâ
Ë÷Òý( Index )Êdz£¼ûµÄÊý¾Ý¿â¶ÔÏó£¬ËüµÄÉèÖúûµ¡¢Ê¹ÓÃÊÇ·ñµÃµ±£¬¼«´óµØÓ°ÏìÊý¾Ý¿âÓ¦ÓóÌÐòºÍDatabase µÄÐÔÄÜ¡£ËäÈ»ÓÐÐí¶à×ÊÁϽ²Ë÷ÒýµÄÓ÷¨£¬ DBA ºÍ Developer ÃÇÒ²¾³£ÓëËü´ò½»µÀ£¬µ«±ÊÕß·¢ÏÖ£¬»¹ÊÇÓв»ÉÙµÄÈ˶ÔËü´æÔÚÎó½â£¬Òò´ËÕë¶ÔʹÓÃÖеij£¼ûÎÊÌ⣬½²Èý¸öÎÊÌâ¡£´ËÎÄËùÓÐʾÀýËùÓõÄÊý¾Ý¿âÊÇ Oracle 8.1.7 OPS on HP N series ,ʾÀýÈ«²¿ÊÇÕæʵÊý¾Ý£¬¶ÁÕß²»ÐèҪעÒâ¾ßÌåµÄÊý¾Ý´óС£¬¶øӦעÒâÔÚʹÓò»Í¬µÄ·½·¨ºó£¬Êý¾ÝµÄ±È½Ï¡£±¾ÎÄËù½²»ù±¾¶¼Êdz´ÊÀĵ÷£¬µ«ÊDZÊÕßÊÔͼͨ¹ýʵ¼ÊµÄÀý×Ó£¬À´ÕæÕýÈÃÄúÃ÷°×ÊÂÇéµÄ¹Ø¼ü¡£
µÚÒ»½²¡¢Ë÷Òý²¢·Ç×ÜÊÇ×î¼ÑÑ¡Ôñ
Èç¹û·¢ÏÖOracle ÔÚÓÐË÷ÒýµÄÇé¿öÏ£¬Ã»ÓÐʹÓÃË÷Òý£¬Õâ²¢²»ÊÇOracle µÄÓÅ»¯Æ÷³ö´í¡£ÔÚÓÐЩÇé¿öÏ£¬Oracle ȷʵ»áÑ¡ÔñÈ«±íɨÃ裨Full Table Scan£©,¶ø·ÇË÷ÒýɨÃ裨Index Scan£©¡£ÕâЩÇé¿öͨ³£ÓУº
1. ±íδ×östatistics, »òÕß statistics ³Â¾É£¬µ¼Ö Oracle ÅжÏʧÎó¡£
2. ¸ù¾Ý¸Ã±íÓµÓеļǼÊýºÍÊý¾Ý¿éÊý£¬Êµ¼ÊÉÏÈ«±íɨÃèÒª±ÈË÷ÒýɨÃè¸ü¿ì¡£
¶ÔµÚ1ÖÖÇé¿ö£¬×î³£¼ûµÄÀý×Ó£¬ÊÇÒÔÏÂÕâ¾äsql Óï¾ä£º
select count(*) from mytable;
ÔÚδ×÷statistics ֮ǰ£¬ËüʹÓÃÈ«±íɨÃ裬ÐèÒª¶ÁÈ¡6000¶à¸öÊý¾Ý¿é£¨Ò»¸öÊý¾Ý¿éÊÇ8k£©, ×öÁËstatistics Ö®ºó£¬Ê¹ÓõÄÊÇ INDEX (FAST FULL SCAN) £¬Ö»ÐèÒª¶ÁÈ¡450¸öÊý¾Ý¿é¡£µ«ÊÇ£¬statistics ×öµÃ²»ºÃ£¬Ò²»áµ¼ÖÂOracle ²»Ê¹ÓÃË÷Òý¡£
µÚ2ÖÖÇé¿ö¾ÍÒª¸´Ôӵöࡣһ°ã¸ÅÄîÉ϶¼ÈÏΪË÷Òý±È±í¿ì£¬±È½ÏÄÑÒÔÀí½âʲôÇé¿öÏÂÈ«±íɨÃèÒª±ÈË÷ÒýɨÃè¿ì¡£ÎªÁ˽²Çå³þÕâ¸öÎÊÌ⣬ÕâÀïÏȽéÉÜÒ»ÏÂOracle ÔÚÆÀ¹ÀʹÓÃË÷ÒýµÄ´ú¼Û£¨cost£©Ê±Á½¸öÖØÒªµÄÊý¾Ý£ºCF(Clustering factor) ºÍ FF(Filtering factor).
CF: Ëùν CF, ͨË׵ؽ²£¬¾ÍÊÇÿ¶ÁÈëÒ»¸öË÷Òý¿é£¬Òª¶ÔÓ¦¶ÁÈë¶àÉÙ¸öÊý¾Ý¿é¡£
FF: Ëùν FF, ¾ÍÊǸÃsql Óï¾äËùÑ¡ÔñµÄ½á¹û¼¯£¬Õ¼×ܵÄÊý¾ÝÁ¿µÄ°Ù·Ö±È¡£
´óÔ¼µÄ¼ÆË㹫ʽÊÇ£ºFF * (CF + Ë÷Òý¿é¸öÊý) £¬Óɴ˹À¼Æ³ö£¬Ò»¸ö²éѯ£¬ Èç¹ûʹÓÃij¸öË÷Òý£¬»áÐèÒª¶ÁÈëµÄÊý¾Ý¿é¿éÊý¡£ÐèÒª¶ÁÈëµÄÊý¾Ý¿éÔ½¶à£¬Ôò cost Ô½´ó£¬Oracle Ò²¾ÍÔ½¿ÉÄܲ»Ñ¡ÔñʹÓà index. £¨È«±íɨÃèÐèÒª¶ÁÈëµÄÊý¾Ý¿éÊýµÈÓڸñíµÄʵ¼ÊÊý¾Ý¿éÊý£©
ÆäºËÐľÍÊÇ£¬ CF ¿ÉÄÜ»á±Èʵ¼ÊµÄÊý¾Ý¿éÊýÁ¿´ó¡£CF Êܵ½Ë÷ÒýÖÐÊý¾ÝµÄÅÅÁз½Ê½Ó°Ï죬ͨ³£ÔÚË÷Òý¸Õ½¨Á¢Ê±£¬Ë÷ÒýÖеļǼÓë±íÖеļǼÓÐÁ¼ºÃµÄ¶ÔÓ¦¹Øϵ£¬CF ¶¼ºÜС£»ÔÚ±í¾¹ý´óÁ¿µÄ²åÈë¡¢Ð޸ĺó£¬ÕâÖÖ¶ÔÓ¦¹ØϵԽÀ´Ô½ÂÒ£¬CF Ò²Ô½À´Ô½´ó¡£´ËʱÐèÒª DBA ÖØн¨Á¢»òÕß×éÖ¯¸ÃË÷Òý¡£
Èç¹ûij¸ösql Óï¾äÒÔǰһֱʹÓÃijË÷Òý£¬½Ï³¤Ê±¼äºó²»ÔÙʹÓã¬Ò»ÖÖ¿ÉÄܾÍÊÇ CF ÒѾ±äµÃÌ«´ó£¬ÐèÒªÖØÐÂÕûÀí¸ÃË÷ÒýÁË¡£
FF ÔòÊÇOracle ¸ù¾Ý statistics Ëù×öµÄ¹À¼Æ¡£±ÈÈç, mytables ±íÓÐ32ÍòÐУ¬ÆäÖ÷¼ümyidµÄ×îСֵÊÇ1£¬×î´óÖµÊÇ409654£¬¿¼ÂÇÒÔÏÂsql Óï¾ä£º
Select * from mytables where myid>=1; ºÍ
Select * from mytables where myid>=400000
ÕâÁ½¾ä¿´ËƲ¶àµÄ sql Óï¾ä£¬¶ÔOracle ¶øÑÔ£¬È´Óо޴óµÄ²î±ð¡£ÒòΪǰÕßµÄ FF ÊÇ100%£¬ ¶øºóÕßµÄ FF ¿ÉÄÜÖ»ÓÐ 1%¡£Èç¹ûËüµÄCF ´óÓÚʵ¼ÊµÄÊý¾Ý¿éÊý£¬ÔòOracle ¿ÉÄÜ»áÑ¡ÔñÍêÈ«²»Í¬µÄÓÅ»¯·½Ê½¡£¶øʵ¼ÊÉÏ£¬ÔÚÎÒÃǵÄÊý¾Ý¿âÉϵIJâÊÔÑéÖ¤ÁËÎÒÃǵÄÔ¤²â. ÒÔÏÂÊÇÔÚHP ÉÏÖ´ÐÐʱËüÃÇµÄ explain plan:
µÚÒ»¾ä£º
SQL> select * from mytables where myid>=1;
ÒÑÑ¡Ôñ325917ÐС£
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3132 Card=318474 Byt es=141402456)
1 0 TABLE ACCESS (FULL) OF 'MYTABLES' (Cost=3132 Card=318474 Byt es=141402456)
Statistics
----------------------------------------------------------
7 recursive calls
89 db block gets
41473 consistent gets
19828 physical reads
0 redo size
131489563 bytes sent via SQL*Net to client
1760245 bytes received via SQL*Net from client
21729 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
325917 rows processed
µÚ¶þ¾ä£º
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=346 Card=663 Bytes=2 94372)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'MYTABLES' (Cost=346 Card=663
Bytes=294372)
2 1 INDEX (RANGE SCAN) OF 'PK_MYTABLES' (UNIQUE) (Cost=5 Card=663)
Statistics
----------------------------------------------------------
1278 recursive calls
0 db block gets
6647 consistent gets
292 physical reads
0 redo size
3544898 bytes sent via SQL*Net to client
42640 bytes received via SQL*Net from client
524 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
7838 rows processed
ÏÔ¶øÒ×¼û£¬µÚ1¾äûÓÐʹÓÃË÷Òý£¬µÚ2¾äʹÓÃÁËÖ÷¼üË÷Òýpk_mytables. FFµÄ¾Þ´óÓ°ÏìÓɴ˿ɼûÒ»°ß¡£ÓÉ´ËÏëµ½£¬ÎÒÃÇÔÚдsql Óï¾äʱ£¬Èç¹ûÔ¤ÏȹÀ¼ÆһϠFF, Äã¾Í¼¸ºõ¿ÉÒÔÔ¤¼ûµ½ Oracle »á·ñʹÓÃË÷Òý¡£
µÚ¶þ½²¡¢Ë÷ÒýÒ²Óкûµ
Ë÷ÒýÓÐ B tree Ë÷Òý£¬ Bitmap Ë÷Òý£¬ Reverse b tree Ë÷Òý£¬ µÈ¡£×î³£ÓõÄÊÇ B tree Ë÷Òý¡£ B µÄÈ«³ÆÊÇBalanced , ÆäÒâÒåÊÇ£¬´Ó tree µÄ root µ½ÈκÎÒ»¸öleaf £¬Òª¾¹ýͬÑù¶àµÄ level. Ë÷Òý¿ÉÒÔÖ»ÓÐÒ»¸ö×ֶΣ¨Single column£©, Ò²¿ÉÒÔÓжà¸ö×ֶΣ¨Composite£©,×î¶à32¸ö×ֶΣ¬8I »¹Ö§³Ö Function-based index. Ðí¶àdeveloper ¶¼ÇãÏòÓÚʹÓõ¥ÁÐB Ê÷Ë÷Òý¡£
ËùνË÷ÒýµÄºÃ»µÊÇÖ¸£º
1£¬Ë÷Òý²»ÊÇÔ½¶àÔ½ºÃ¡£ÌرðÊÇ´óÁ¿´ÓÀ´»òÕß¼¸ºõ²»ÓõÄË÷Òý£¬¶ÔϵͳֻÓÐË𺦡£OLTPϵͳÿ±í³¬¹ý5¸öË÷Òý¼´»á½µµÍÐÔÄÜ£¬¶øÇÒÔÚÒ»¸ösql ÖУ¬ Oracle ´Ó²»ÄÜʹÓó¬¹ý 5¸öË÷Òý¡£
2£¬ºÜ¶àʱºò£¬µ¥ÁÐË÷Òý²»È縴ºÏË÷ÒýÓÐЧÂÊ¡£
3£¬ÓÃÓÚ¶à±íÁ¬½áµÄ×ֶΣ¬¼ÓÉÏË÷Òý»áºÜÓÐ×÷Óá£
ÄÇô£¬ÔÚʲôÇé¿öϵ¥ÁÐË÷Òý²»È縴ºÏË÷ÒýÓÐЧÂÊÄØ£¿ÓÐÒ»ÖÖÇé¿öÊÇÏÔ¶øÒ×¼ûµÄ£¬ÄǾÍÊÇ£¬µ±sql Óï¾äËù²éѯµÄÁУ¬È«²¿¶¼³öÏÖÔÚ¸´ºÏË÷ÒýÖÐʱ£¬´ËʱÓÉÓÚ Oracle Ö»ÐèÒª²éѯË÷Òý¿é¼´¿É»ñµÃËùÓÐÊý¾Ý£¬µ±È»±ÈʹÓöà¸öµ¥ÁÐË÷ÒýÒª¿ìµÃ¶à¡££¨´Ëʱ£¬ÕâÖÖÓÅ»¯·½Ê½±»³ÆΪ Index only access path£©
³ý´ËÖ®ÍâÄØ£¿ÎÒÃÇ»¹ÊÇÀ´¿´Ò»¸öÀý×Ó°É£º
ÔÚ HP£¨Oracle 8.1.7£© ÉÏÖ´ÐÐÒÔÏÂÓï¾ä£º
select count(1) from mytabs where coid>=130000 and issuedate >= to_date ('2001-07-20','yyyy-mm-dd')¡£
Ò»¿ªÊ¼£¬ÎÒÃÇÓÐÁ½¸öµ¥ÁÐË÷Òý£ºI_mytabs1(coid), I_mytabs2(issuedate), ÏÂÃæÊÇÖ´ÐÐÇé¿ö£º
COUNT(1)
----------
6427
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=384 Card=1 Bytes=11)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'T_MYTABS' (Cost=384 Card
=126 Bytes=1386)
3 2 INDEX (RANGE SCAN) OF 'I_MYTABS2' (NON-UNIQUE) (Cost=11
Card=126)
Statistics
----------------------------------------------------------
172 recursive calls
1 db block gets
5054 consistent gets
2206 physical reads
0 redo size
293 bytes sent via SQL*Net to client
359 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
1 rows processed
¿ÉÒÔ¿´µ½£¬Ëü¶ÁÈ¡ÁË7000¸öÊý¾Ý¿éÀ´»ñµÃËù²éѯµÄ 6000¶àÐС£
ÏÖÔÚ£¬È¥µôÕâÁ½¸öµ¥ÁÐË÷Òý£¬Ôö¼ÓÒ»¸ö¸´ºÏË÷ÒýI_mytabs_test ( coid, issuedate), ÖØÐÂÖ´ÐУ¬½á¹ûÈçÏ£º
COUNT(1)
----------
6436
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=1 Bytes=11)
1 0 SORT (AGGREGATE)
2 1 INDEX (RANGE SCAN) OF 'I_MYTABS_TEST' (NON-UNIQUE) (Cost=3 Card=126 Bytes=1386)
Statistics
----------------------------------------------------------
806 recursive calls
5 db block gets
283 consistent gets
76 physical reads
0 redo size
293 bytes sent via SQL*Net to client
359 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
1 rows processed
¿ÉÒÔ¿´µ½£¬Õâ´ÎÖ»¶ÁÈ¡ÁË300¸öÊý¾Ý¿é¡£
7000¿é¶Ô300¿é£¬Õâ¾ÍÊÇÔÚÕâ¸öÀý×ÓÖУ¬µ¥ÁÐË÷ÒýÓ븴ºÏË÷ÒýµÄ´ú¼ÛÖ®±È¡£Õâ¸öÀý×ÓÌáʾÎÒÃÇ£¬ ÔÚÐí¶àÇé¿öÏ£¬µ¥ÁÐË÷Òý²»È縴ºÏË÷ÒýÓÐЧÂÊ¡£
¿ÉÒÔ˵£¬ÔÚË÷ÒýµÄÉèÖÃÎÊÌâÉÏ£¬ÆäʵÓÐÐí¶à¹¤×÷¿ÉÒÔ×ö¡£ÕýÈ·µØÉèÖÃË÷Òý£¬ÐèÒª¶ÔÓ¦ÓýøÐÐ×ÜÌåµÄ·ÖÎö¡£ µÚÈý½²¡¢Ë÷ÒýÔٺ㬲»ÓÃÒ²ÊÇ°×´î
Å׿ªÇ°ÃæËù˵µÄ£¬¼ÙÉèÄãÉèÖÃÁËÒ»¸ö·Ç³£ºÃµÄË÷Òý£¬ÈκÎɵ¹Ï¶¼ÖªµÀÓ¦¸ÃʹÓÃËü£¬µ«ÊÇOracle È´Æ«Æ«²»Óã¬ÄÇô£¬ÐèÒª×öµÄµÚÒ»¼þÊÂÇ飬ÊÇÉóÊÓÄãµÄ sql Óï¾ä¡£
Oracle ҪʹÓÃÒ»¸öË÷Òý£¬ÓÐһЩ×î»ù±¾µÄÌõ¼þ£º ¡¡¡¡1£¬ where ×Ó¾äÖеÄÕâ¸ö×ֶΣ¬±ØÐëÊǸ´ºÏË÷ÒýµÄµÚÒ»¸ö×ֶΣ» ¡¡¡¡2£¬ where ×Ó¾äÖеÄÕâ¸ö×ֶΣ¬²»Ó¦¸Ã²ÎÓëÈκÎÐÎʽµÄ¼ÆËã
¾ßÌåÀ´½²£¬¼ÙÉèÒ»¸öË÷ÒýÊÇ°´ f1, f2, f3µÄ´ÎÐò½¨Á¢µÄ£¬ÏÖÔÚÓÐÒ»¸ö sql Óï¾ä, where ×Ó¾äÊÇ f2 = : var2, ÔòÒòΪ f2 ²»ÊÇË÷ÒýµÄµÚ1¸ö×ֶΣ¬ÎÞ·¨Ê¹ÓøÃË÷Òý¡£
µÚ2¸öÎÊÌ⣬ÔòÔÚÎÒÃÇÖ®Öзdz£ÑÏÖØ¡£ÒÔÏÂÊÇ´Ó Êµ¼ÊϵͳÉÏÃæ×¥µ½µÄ¼¸¸öÀý×Ó£º
Select jobid from mytabs where isReq='0' and to_date (updatedate) >= to_Date ( '2001-7-18', 'YYYY-MM-DD')£»
¡¡¡
ÒÔÉϵÄÀý×ÓÄܺÜÈÝÒ׵ؽøÐиĽø¡£Çë×¢ÒâÕâÑùµÄÓï¾äÿÌ춼ÔÚÎÒÃǵÄϵͳÖÐÔËÐУ¬ÏûºÄÎÒÃÇÓÐÏÞµÄcpu ºÍ ÄÚ´æ×ÊÔ´¡£
³ýÁË1£¬2ÕâÁ½¸öÎÒÃDZØÐëÀμÇÓÚÐĵÄÔÔòÍ⣬»¹Ó¦¾¡Á¿ÊìϤ¸÷ÖÖ²Ù×÷·û¶Ô Oracle ÊÇ·ñʹÓÃË÷ÒýµÄÓ°Ïì¡£ÕâÀïÎÒÖ»½²ÄÄЩ²Ù×÷»òÕß²Ù×÷·û»áÏÔʽ£¨explicitly£©µØ×èÖ¹ Oracle ʹÓÃË÷Òý¡£ÒÔÏÂÊÇһЩ»ù±¾¹æÔò£º
1£¬ Èç¹û f1 ºÍ f2 ÊÇͬһ¸ö±íµÄÁ½¸ö×ֶΣ¬Ôò f1>f2, f1>=f2, f1 ¡¡¡¡2£¬ f1 is null, f1 is not null, f1 not in, f1 !=, f1 like ¡®%pattern%¡¯; ¡¡¡¡3£¬ Not exist ¡¡¡¡4£¬ ijЩÇé¿öÏ£¬f1 in Ò²»á²»ÓÃË÷Òý
¶ÔÓÚÕâЩ²Ù×÷£¬±ðÎÞ°ì·¨£¬Ö»Óо¡Á¿±ÜÃâ¡£±ÈÈ磬Èç¹û·¢ÏÖÄãµÄ sql ÖÐµÄ in ²Ù×÷ûÓÐʹÓÃË÷Òý£¬Ò²Ðí¿ÉÒÔ½« in ²Ù×÷¸Ä³É ±È½Ï²Ù×÷ + union all¡£±ÊÕßÔÚʵ¼ùÖз¢ÏֺܶàʱºòÕâºÜÓÐЧ¡£
µ«ÊÇ£¬Oracle ÊÇ·ñÕæÕýʹÓÃË÷Òý£¬Ê¹ÓÃË÷ÒýÊÇ·ñÕæÕýÓÐЧ£¬»¹ÊDZØÐë½øÐÐʵµØµÄ²âÑé¡£ºÏÀíµÄ×ö·¨ÊÇ£¬¶ÔËùдµÄ¸´Ô sql, ÔÚ½«ËüдÈëÓ¦ÓóÌÐò֮ǰ£¬ÏÈÔÚ²úÆ·Êý¾Ý¿âÉÏ×öÒ»´Îexplain . explain »á»ñµÃOracle ¶Ô¸Ã sql µÄ½âÎö£¨plan£©,¿ÉÒÔÃ÷È·µØ¿´µ½ Oracle ÊÇÈçºÎÓÅ»¯¸Ã sql µÄ¡£
Èç¹û¾³£×ö explain, ¾Í»á·¢ÏÖ£¬Ï²°®Ð´¸´Ô sql ²¢²»ÊǸöºÃÏ°¹ß£¬ÒòΪ¹ý·Ö¸´ÔÓµÄsql Æä½âÎö¼Æ»®ÍùÍù²»¾¡ÈçÈËÒâ¡£ÊÂʵÉÏ£¬½«¸´Ô sql ²ð¿ª£¬ÓÐʱºò»á¼«´óµØÌá¸ßЧÂÊ£¬ÒòΪÄÜ»ñµÃºÜºÃµÄÓÅ»¯¡£µ±È»ÕâÒѾÊÇÌâÍâ»°ÁË¡£
ÎÄÕÂÀ´Ô´£ºÎ´Öª |