Oracle9i ʵÌ廯Óͼ
  Oracle °×ƤÊé
2001 Äê           5 ÔÂ
Oracle9i ʵÌ廯Óͼ




ִиŠ                                                                                             ...
Oracle9i ʵÌ廯Óͼ




                                                                                                   ...
µ±ÏòÊý¾Ý²Ö¿âÕ¶ËÓû§ÎÆðûǣʹÐñ¬ó·È¼
                                             »áش𣺿ìËÙ׼ȷµÄÐÅÏ¢¡«ÊÇÕâÒ²¸øý¾ÝÖÉèÆß³öÁ...
ÓÃÚʵÌ廯ͼģ½ÀàÐòÉèƲ´ÏÞÖ¡ÒËÔý¾Ý
¿â»·¾³ÖУ¬ÄʽÉÒÔÇÑ©»¨Ê½µÄÉè¼Æ£¬«ÕⲢDZØÐë¡
¶ÔÓÚÊìϤ²úÆ·µÍ³ÖÐý¾Ý¿âÉè¼õÄÕßÀ´Ë£¬Ò»¸ö
²Ö¿âб...
1 ½éÉÜʱ¼äά
                                                                                                             ...
2 ´ ½¨Ê±¼äάµÄ                                   SQL Óï¾ä
CREATE DIMENSION times_dim
LEVEL day IS TIMES.TIME_ID
LEVEL mont...
•
                                                    •                                                                   ...
ÒѾ-ÓµÐÊýݲֿâÄÕãÉÜß±¸ÁËŪºÍ¹³ÌÀ´Î¬»¤
                                          ͼ¡£Òò´Ë¬ûÃÇÏÀÓ²éѯÖØй¦Äܶø»Â½¨¸Åª
      ...
REFRESH_FAST:Y
                                            REFRESH_FAST_AFTER_ANY_DML:N
                                  ...
ÔÚÀúÊ·ÉÏ£¬¸ÅÒª±í¹ÓÃý³ÌÖеĻöÎâ¾ÍÇõ¼Øº
                                           Ëæºó¸üС£Õâ©ÎÊÌâÏÖÔڵýÁ˾ö£¬Òòª¸Å¹ÜÀíá©...
•
•


¿ìËÙ ¢ÐÂ
ÓÐÒ»©ÊµÌ廯Óͼ¿ÉÄÜá·Ç³£´ó¬²¾ßÐÖÀýêÈ«Ë¢Â
ËùÐèµÄʱ¼ä¡£Ò»ÖÌæ´ú·½¨Çø¿ì٢¬ö«í¯
µÄ²¿·ÖӦýÊÌ廯ͼС£ÔØý¾ÝâÈκ±íÂ...
Êý¾ÝËäÈ»ÒÑ-¢Ð£¬µ«Ìå¯ÓͼԿÉò¹¨
                                            ÃüÁîALTER SYSTEM SET QUERY_REWRITE_ENABLED =
 ...
/½ûÓòéѯÖØд
                                       µ±ÉèÖÃÁËÏÂвÎÊý£¬éѯش½«·¢úº
ALTER SESSION SET QUERY_REWRITE_ENABLED ...
ÓÐʱºò²éѯ¿ÉÄÜ»áμ³ÒÁ£¬¸Ã´æ¢ÔÚŪíÖµ«Ç
¿ÉÒÔͨ                                     ¹ý½«   ʵÌå   »¯ÊÓ    ͼÁª           ½Ó·µ...
6 °üº¬Êý¾Ý×Ó¼¯µÄÊ Ìå» Óͼ
CREATE MATERIALIZED VIEW some_cust_sales_mv
BUILD IMMEDIATE
REFRESH COMPLETE
ENABLE QUERY REWRIT...
ENFORCED ·½ÊÇĬÈÏ£ÔÚÕâÖÐÓÅ»¯³Ìò«¹
ÓÃÈ·ÖªÆä°üº¬×îÐÂÊý¾ÝµÄÌ廯ͼ£ÒÔ¹ùÚ
ENABLED VALIDATED Ö÷                                 ...
O9i Mv[1]
O9i Mv[1]
O9i Mv[1]
O9i Mv[1]
Upcoming SlideShare
Loading in...5
×

O9i Mv[1]

1,118

Published on

oracle materialized view

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,118
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "O9i Mv[1]"

  1. 1. Oracle9i ʵÌ廯Óͼ Oracle °×ƤÊé 2001 Äê 5 ÔÂ
  2. 2. Oracle9i ʵÌ廯Óͼ ִиŠҪ ............................................................................................................. 3 ¼ò½é ...................................................................................................................... 3 ΪºÊ¹ ÓøŠҪ¹Ü Àí .......................................................................................... 4 ¸ÅÒª¹Ü Àí×é ¼þ .................................................................................................... 4 ģʽÐè Çó ......................................................................................5 ά¶È ...................................................................................................................... 5 ¶¨Òåά ¶ÈµÄ Ïà¹Ø Ìáʾ ..................................................................6 ʵÌ廯 ÊÓͼ ........................................................................................................ 7 ´½¨Êµ Ì廯 ÊÓͼ ..........................................................................8 ʹÓÃ×Ô ¼ºÔ¤ ÏȽ¨ Á¢µÄ ʵÌå »¯ÊÓͼ ..............................................9 ʵÌ廯 ÊÓͼ µÄË÷ ÒýÑ¡ Ôñ ..............................................................9 ʵÌ廯 ÊÓͼ ¿ÉÒÔ Íê³É ÄЩ ¹¤×÷£¿ ..............................................9 ʵÌ廯 ÊÓͼ ʧР........................................................................10 °²È«º¬ Òå ....................................................................................10 ¼ÓÔØºÍ Ë¢Ð ʵÌå »¯ÊÓ Í¼ ...............................................................................11 ÍêÈ«Ë¢ Р.................................................................................... 11 ¿ìËÙ¢ Р....................................................................................12 Ë¢ÐÂºÍ Ô¼Êø Ìõ¼þ ........................................................................12 Êý¾Ý¿É ÓÃÐÔ ................................................................................13 ·ÖÇøά »¤²Ù ×÷ºÍ ʵÌå »¯ÊÓ Í¼ ....................................................13 ²éѯÖØ Ð´ ...........................................................................................................13 ÆôÓà /½ÃÓû ¯Ñé² ´ÐØÖ ...................................................................14 ²éѯÖØ Ð´µÄ ÀàÐÍ ........................................................................14 ÍêÈ«Æ¥ Åä ............................................................................14 ¸ÅÒªÁ ½Ó·µ »Ø ....................................................................15 ¸ÅÒª»ã ×Ü»ò ¾ÛºÏ ËùÓÐ ........................................................15 Êý¾Ý×Ó ¼¯ ............................................................................15 ²éѯÖØ Ð´Íê ÕûÐÔ ·½Ê ................................................................16 ½á¹ûÊÇ ·ñÕý È· ............................................................................17 ½âÊÍÖØ Ð´ ....................................................................................17 ¸ÅÒª¹Ë ÎÊ ...........................................................................................................18 Ìṩ¸º ÔØ ....................................................................................18 ÍƼöʵ Ì廯 ÊÓͼ ........................................................................19 ʵÏÖÍÆ ¼ö ............................................................................19 ¹ýÂ˸º ÔØ ............................................................................20 ¹À¼Æʵ Ì廯 ÊÓͼ µÄ´ó С ............................................................20 ½áÂÛ ....................................................................................................................20 Oracle9i ʵÌ廯ÊÓͼ µÚ 2 Ò³
  3. 3. Oracle9i ʵÌ廯Óͼ Ö´Ð ¸ÅÒª ñ½ ìÌ Äµ ýÊ ¬£â¿Ý¾ ÛÂÞΠݾýÊÇ Ö² â¿ ¢¡ ýÊ ÐÖݾ ¹»ÄÐ ÇÊ OLTP ϵͳ £¬ ¶¼ °üº¬´óÁ¿µÄÐÅÏ¢ÈýËÃÇ¥·ÖÍÀí½â¡£»¶øçÎÒԼʱ µÄ·½Ê²éÕҺͱí¾âЩÅϢǻ¸öÖØ´óÎÌ£¬ÓÈÆäèªËÑ Ë÷ÅÓ´óÊýÁ¿ÐÏ¢µÄ±ºò¡£ ʵÌ廯ÓͼÄܹ°ïÖú½â¾öոΣ¬ÒòªËüá©Á¿ìÙ·Ã ºÍ±¨¸æÊý¾ÝµÄ·½¡£ ¼ò½é ʵÌ廯Óͼ×ÏÈÔÚ Oracle8i ÖÐÒýÈ룬ÊdzÆΪ¡°¸Å¹ÜÀí±µÄ×é¼þ µÄÒ»²¿·Ö¡£ÉÜú¹«Ë¾Ñ-ÔÚÊÓÃÌå¯Í¼¬ªÀü ÆäËûÃ×Ö£¬ÀýÈç¸ÅÒª»ò¾ÛºÏ±í¡ÔÚÕâïÎÇÌ´½¨Í¹Ü ʵÌ廯 ÊÓͼ £¬ »¹ ÌÖÂÛ ²éѯ ÖØд¹¦ ÄÜÈç ºÎ͸ Ã÷µØ ÖØд SQL ¯Ñé² ¬£ Ó´ ¶øʹÓõÌ廯ͼÀ´Ëõ²éÑÏ즱䡣Õ⽫ý¾Ý¿§êÈ ÎÞÐèÖªµÀ´æÔÚÄ©ÊÌ廯Óͼ¡£ ʵÌ廯Óͼ¦¿´×÷ÇÒÖØâÄ£¬ËüÎïÀíÉÏæÔÚý¾Ý ¿² ¬£ ÔÒÉ¿ ¨Àü° Ó½ªÁ ͺ /»ò¾Û ºÏ¡£ Ëü ÄÜ ¹» ÔÚ Ö´ Ð Ö® Ç° Ô¤ ÏȼÆËã ¿ªÏú ´ó µÄÁª½ÓºÍ¾ÛϲÙ×÷£¬Òò´ËüæÔÚõ¶ÌéѯÖÐʱ¼ä¡ ½ñÌ죬 ʹÓà ×ÔÉí ¸ÅÒª µÄ¹« ˾»¨·Ñ ÁË´ó Á¿µÄ ʱ¼ä ÓÃÚ ÊÖ¹¤´ ½¨¸Å Òª¡¢ ʶ±ð½«´¨ÄЩ¸ÅÒª¡¢ÔøË÷ýºÍü£¬¼°éÓû§¹ ÓÃÄЩ¸ÅÒª¡£ ÏÖÔÚ DBA ½«öÐëÔÚ¿ªÊ¼±´¨µÌ廯ÓÍ£¬¶øÎÞÂÛý¾Ýº ·¢Éú±ä»¯£¬Ëü¶¼½«×ԸС´Íâ¹ÓÒöŪÎÊéþ Ïò DBA ÍƼö´½¨¡¢É¾³ýº±£ÁôÄЩʵÌå»¯Ó Êý¾Ý²Ö¿â»òÓ罫ÉÒÔÌåáµ¹¯Í¼Ä×î´óº¦ Ö®Ò» £¬ DBA ÎÞ Ðë ÔÙ ¸æ Ëß Ëû ÃÇ´æÔÚ ÄЩ ʵÌå »¯ÊÓ Í¼ ¡£ ËûÃÇ¿É ÒÔ¶ Êý ¾Ý¿âÖÐ µÄ±í »òÊÓ Í¼±à д×Ô ¼ºµÄ²é ѯ¡£ È» ºó Oracle ·þ ÎñÆ÷µÄ ²éѯ ÖØ Ð´»úÖÆ ½«×Ô ¶¯ÖØ Ð´ SQL ²éѯÒÔ Ê¹Óà ʵÌå »¯ÊÓ Í¼¡£ Õâ Ñù¾Í´ó ´óËõ ¶Ì Á˲éѯÏìӦʱ¼ä£¬Öնû§ÎÞÐë¡°½â¸ÅÒª Oracle9i ʵÌ廯ÊÓͼ µÚ 3 Ò³
  4. 4. µ±ÏòÊý¾Ý²Ö¿âÕ¶ËÓû§ÎÆðûǣʹÐñ¬ó·È¼ »áش𣺿ìËÙ׼ȷµÄÐÅÏ¢¡«ÊÇÕâÒ²¸øý¾ÝÖÉèÆß³öÁó ÄÑÌ⣺ΪÁ˻شð¡°ÔÚ y µØãÎÒÃÇÂô³ö¶àÉÙ¼þ x ²úÆ·¡±£¬ÍÊÏ Íû±ÜÃâ¶ÁÈ¡íÖеĿһ£¬Ø뽨¢ÌõÊý¾ÝìËÙ·ÓÉ ½â¾ö´ËÎÊÌ×î³£¼ûµÄ°ì·¨Ö®Ò»ÍǸŪ±í¬ Oracle ½«Æä³Îª ʵÌ廯Óͼ¡£ÕâÒ¹¤×÷°üÀ¨ÏȪí½äиºÉ¬ó´æÄ ·Ç³£Ð¡ µÄÊ Ì廯 ÊÓͼ £¬ ʵ Ì廯ÊÓ Í¼ÖÐ ¿ÉÒÔ °üº¬ ËùÐè ÐÅÏ¢µÄ Áª½Ó ºÍ / »ò¾ÛºÏ¡£ÀýÈç¬ÎªÁËØ´ðÇ°ÃæµÄÊÌâå¯ÓͼÖп²úÆ·¶Ô ¦Ó ÚÓ ¬£Ð »Ò ÷Ã¸Ö ö¸¿Ã ĵòÓøÇ úÏ £¡¿ÁÛÊ Ë´òÒ û¹çÈ «¹Ò¼» ¾Ë ÚÔ 5 صö¸ µãÏúÊÛ 2000 ¼þ ²ú Æ· £¬ Ôò ½«Òª¶Á È¡µÄ ×î´ó ÐÊý ʼÖÕ Îª 10000£¬¶ø ÎÞ ÂÛÒѾ-ʳö¶àÉÙÌÆ·¡£ ºÜÃ÷ÏÔ£¬ÊµÌ廯Óͼ±ØÐëÖ¤¾«È·¸õÒâζ×ÅÕ˧ ÔÚÐèÒª¶ÁÈ¡µÄÊýºÜÉÙ£¬ò´Ë¿¼ÖÕìؽÓá¹û¾Ýâ ÈÝÁ¿ÒѾ-Ôö³¤µ½Õ×ÖÚ£¬ò´ËʹÓÃâùÄ·¨Àõ¶Ì²é¯Ïì¦ Ê±¼ä¾ÍÏԵýÀ´ÖØÒª¡£ñÌìÐí¶àÕã¨ÁË׺ĸŬ Òò´ËʹÓà Oracle8 ¸ÅÒª¹ÜÀíËù´øµÄ¶îÍâºÃ¦ÊÇ£ • Oracle ÖÐµÄ ²éѯ ÖØд »úÖÆ ÊÇ͸Ã÷ µÄ²¢ ²ÉÓà ʵÌå »¯ÊÓ Í¼ £¨ ¼´ ʹËü½öÄܲ¿·ÖÂú×ãéѯµÐèÒª£©¡ • • ÊÓͼ¡£ • DBA ²»ÔÙÐèÒª¨Ê±¼äéÕÓ¦´½Ä©µÌå¯Í¡£Ï³ ½«»ùÓÚ ¹ýÈ¥ ¶ÔÊý ¾Ý¿â »òÊý ¾Ý²Ö¿â µÄ²é ѯ£¬ Ïò DBA ÌṩÓÐ ¹ØÐèҪĩ¸ÅµÏ¢¡£ ¸ÅÒª¹ÜÀí×é¼þ ×é³É¸ÅÒª¹ÜÀíµÄÓÐÎåö¼þ£º • • • • • ²¢»ÐèҪʹÓÃËù×é¼þ£¬µ«Ñ¡ÄÔ½¶àñÅÆ¾Í ¶à¡£ÏÖÔÚÎÒÃǽ«ê¸ÌÕâЩ×é¼þ Oracle9i ʵÌ廯ÊÓͼ µÚ 4 Ò³
  5. 5. ÓÃÚʵÌ廯ͼģ½ÀàÐòÉèƲ´ÏÞÖ¡ÒËÔý¾Ý ¿â»·¾³ÖУ¬ÄʽÉÒÔÇÑ©»¨Ê½µÄÉè¼Æ£¬«ÕⲢDZØÐë¡ ¶ÔÓÚÊìϤ²úÆ·µÍ³ÖÐý¾Ý¿âÉè¼õÄÕßÀ´Ë£¬Ò»¸ö ²Ö¿âбØëʹÓûͬµÄæÔòº¼õ¡£ÀýÈçúÆ·¾Ý¨³Ç ·¶»¯µÄ£¬Òò´ËÔÚÕâÖÇé¿öÏÂʱ¼äÎí¾½¨×îºÃ²ÉÓÈý¸ ±í£º ÈÕ¡¢ÔÂÄê ¡£Áª½ÓÌõ¼þ¦¸ÃÂú×㺫¿ö ÈÕÆÚ ÐÁ¬½ÓµÒ»¸ö £¨½öÒ»¸© ÔÂ·Ý Ð£¬Ã¿¸ö ÔÂ·Ý ÐÁ¬½ÓµÒ»¸ö£¨© Äê·Ý С£ Êý¾Ý²Ö¿âµÏͨ³£½«¼ÂÒ»¸öêÈ·Ç¹æ¶¯Ä Ê±¼ä ά±í£ÆäÖÐ ÈÕÆÚ¡¢Ô·ÝÄê À¸¶¼´¦ÓÚͬһö±íÖС£²¹ýÎÞÂÛÉèÆÊÃµÄ Êǹ淶»¯±í£¬¼¿ÉÒÔÓõÌåÍ¡ ά¶È ÔÚ´½¨ Ò»¸ö ʵÌå »¯ÊÓ Í¼Ö® Ç°£¬ µÚ Ò» ²½ Ó¦ ¸Ã ÊÇ »Ø ¹Ë Ä£ ʽ £¬ Ö¸ Ã÷ά ¶È¡£ ά¶È¨ ÒåÁË ÁÐÖ® ¼äµÄ ²ã´Î »¯ £¨ ¸ ¼¶ /×Ó ¼¶ £© ¹Øϵ£¬ Ëù ÓеÄÁ ÎÞÐë À´ ×Ôͬһ¸ö±í¡£ÎÃÇ¿ÁƼ¶¨åÊý¾ÝµÄÈòªÕ⽫ÓÐÖúÚ²é ѯÖØдºÍ¸ÅÒª¹ËÎÊ×ö³ü¼¾²ß¡£ Êý¾Ý¿âÉè¼ÆÕßËùÃæÁÙµÄíÒ»¸öÎÌÇ£¬·±²éѯ½«áÖÓ° ËùÓеÄά¶ÈÁ£ø½ö²¿¼ëÏà¹ØÇÒ»Àýçéѯ ÐÇÆÚ¶þø²»Ê¾ßÌåÈÕ¡£Òò´Ëµ±¨Áά֮ºó¹ØëÃèö ¶ÈÁкͱíÖÆäËü®¼µÄ¹ØÏ¡£ ͼ 1 ¾ÊÔÏ ü°ËÁ ½Á¬º ã²ö¸ ĵδ ±Ê £¡¬Îä¼ »ÒÓ´ ¸Öö ÚÆÕȨ¶ ª¿ ¼Ê ¬£ ÐÓ »Ò ¸ö²ã´ÎæËßÒÃÇÈÕÆÚɼ°ÄЩþÖÜ¡¢Ô»ò꣬¶øÁí ¶¨ÒåÁËÈÕ¡¢Ô¼¾ºÍÄê֮䵹ØÏ£ µ±¶¨ÒåÁË»¸ö²ã´ÎÖ®ºó£¬¿ÉÔàÐÀÃèÊýÈç Èç¹û City ÔÚÿ¸ö State Ö®ÄÚÊÇΨһµ£¬«Ô States Ö®¼ä²»Î¨ Ò»£¬ÄÇô¾ÍÐèªÖ¸¶¨öµØÀí²ãÎÆäʽÈç £¨ Country, State, <State, City£©¬´Ó¶øÂú×ã City ¼¶µ½ State ¼¶Ö®äÑϸñµÄ 1:n ²ã ´Î¹Øϵ¡£ ¿ÉÒÔʹÓÃÁ½Ö·¨À´Î¬¶È£º • SQL CREATE DIMENSION Óï¾ä£¬Èçͼ 2 Ëùʾ • Oracle ÆóÒµ¹ÜÀí÷ÖÐÄά¶ÈÏò¼ Oracle9i ʵÌ廯ÊÓͼ µÚ 5 Ò³
  6. 6. 1 ½éÉÜʱ¼äά °´Õͼ 1 ö³-» ¬£È¶ Î ÔÒÉ¿ °ïÖú DBA Íê³É¶¨Òå¹ý ³Ì¡£Ã¿¸öԲȦ´ú±íά¶ÖÐ µÄÒ»¸ö ¼¶±ð ¹ý LEVEL ×Ó¾äÀ´ÉùÃ÷¡£Î¬ ²ã´Îͨ ¹ý HERARCHY ×Ó¾äÀ´Éù Ã÷¡£¸ÅÒª¹ÜÀí²Í¬Ñùµ ÓÚ DBA ¶¨ÒåÔ¼ÊøÌõþ£¬ ±£Ö¤²ã´Î¼¶ðÐÿһ µÄÁзǿա£ ÔÚͼ 2 ¬£ÐÖ ÇÃÒÎ ÔÒÉ¿ ½µ´¿ ´½¨¸Ãά¶ÈµÄ SQL Óï ¾ä¡£¼¶±ðÃû³ÆÔÓ¦Úάí ÖеÄÁ¡£È»ºóʹÓÃÕ⩼¶ ±ðÃû³ÆÀ´èÊö¿Ò»²ãΡ£ ×îºó £¬ ʹ Óà ATTRIBUTE ×Ó¾äÀ´¶¨ÒåßÐÖ±½¹Øϵ µÄÏî¿ ¡£Òò ´ËÊô ÐÔ calendar_month_name ¶¼ëÓ ð± month ÐÓ £¡µÏع ʹÓà JOIN KEY ×Ó¾äÀ´ÉùÃ÷ά¶ÈÖÐµÄ 1:n Áª½Ó¹Øϵ¡£ÔÚÊÂ±í ºÍά±íÖ®¼ä£Ê¹ÓõÐÄ FOREIGN KEY ºÍ NOT NULL Ô¼ÊøÌõþÀ´±í¾ÕâÖÁª½Ó¹Øϵ¡£ ¶¨Òåά ȵÄÏà¹ØÌáʾ Ϊ°ïÖú´½¨¬¶È£ÇëÕÏÂÃæµÄ¼ò¥²èº 1. Ö¸Ã÷Ä£ ʽÖÐ µÄËù ÓÐά ¶ÈºÍ ά±í¡£ Èç¹û ά¶È ÊÇ¹æ ·¶»¯ ¼´ Ëü ´æ µÄ£¬ ´¢ÔÚ¶à ¸ö±í ÖУ¬ ÄÇô Çë¼ì ²éά±í Ö®¼ä µÄÁª ½Ó£¬ È·±£ ÿ¸ö×Ó ¼¶ Ð Áª½Óµ Ò»¸ö £¨ ½ö Ò» ¸ö £© ¸ ¼¶Ð ¡£ ¶Ô ÓÚ·Ç ¹æ·¶ »¯Î¬ ¶È £¬ Çë ¼ì ²é ×Ó ¼¶ÁÐÊÇ ·ñΨ һȷ ¶¨¸ ¼¶£¨ »òÊôÐÔ £©ÁÐ ¡£Èç ¹û²» ×ñÊØ ÕâЩ¹æ Ôò £¬ ¿ÉÄÜ»áÔÚ²éѯʱµÃ½´íÎó¹û¡£ 2. Ö¸Ã÷¿ һά ¶ÈÖÐ µÄ²ã ´Î¡£ ÀýÈ磬 day ÊÇ month µÄ ×Ó ¼¶ £¨ÎÒ ÃÇ¿É ÒÔ½« day ¼¶±ð¾ÛºÏµ½ month£©£¬ quarter ÊÇ year µÄ×Ó¼¶¡£ 3. Ö¸Ã÷²ã´Îпһ¼¶±ðµÄÊôÔÀ¹ØÏ¡£ýÈç¬ calendar_month_name ÊÇ month µÄÊôÐÔ¡£ 4. Ö¸Ã÷Êý¾Ý²¿âÐöµ±í½Î¬¶È®¼äÄÁªÓ£ìé Ó½ ¬£ ·È £± ¿Ã ö¸ ÂÊ µÊ Ð ½µÓ ªÁ »Ò ö¸ ö½¨£ ©£ö¸»Ò ȶ¬Î £¡Ð ëÐر ÷ÃùÉ ¸ÃÌõ¼þ £¬ ¶ø ÇÒ»¹ ¿ÉÒÔ Ñ¡Ôñ ÊÇ·ñ¿ ÖÆ´ иà Ìõ¼þ £¬ Æä ·½¨ÊÇ Ïò Ê ʵ¹Ø¼üÁÐÌíÓ FOREIGN KEY ºÍ NOT NULL Ô¼ÊøÌõþ£¬ Ïò¸¼¶Áª½ÓüÌí PRIMARY KEY Ô¼ÊøÌõþ¡£¿ÉÒͨ¹ý NOVALIDATE Ñ¡ Ïî À´ Æô ÓÃÕâЩ Ô¼Êø Ìõ¼þ£¬ ´Ó¶ø ÎÞÐë »¨·Ñʱ ¼ä À´ÑéÖ¤ ±íÖÐ µÄÿ һРÊÇ·ñ Âú×ãÕâ ЩԼ ÊøÌõ ¼þ¡£ ¶ÔÓÚ Ëù ÓÐδµÃ µ½ ÑéÖ¤µÄÔ¼ÊøÌõþ£¬»¹ÐèҪ RELY ×Ó¾äÀ´Ê¹ÆÄÜ»ÃÚ²é ѯÖØд¡£ Oracle9i ʵÌ廯ÊÓͼ µÚ 6 Ò³
  7. 7. 2 ´ ½¨Ê±¼äάµÄ SQL Óï¾ä CREATE DIMENSION times_dim LEVEL day IS TIMES.TIME_ID LEVEL month IS TIMES.CALENDAR_MONTH_DESC LEVEL quarter IS TIMES.CALENDAR_QUARTER_DESC LEVEL year IS TIMES.CALENDAR_YEAR LEVEL fis_week IS TIMES.WEEK_ENDING_DAY LEVEL fis_month IS TIMES.FISCAL_MONTH_DESC LEVEL fis_quarter IS TIMES.FISCAL_QUARTER_DESC LEVEL fis_year IS TIMES.FISCAL_YEAR HIERARCHY cal_rollup ( day CHILD OF month CHILD OF quarter CHILD OF year ) HIERARCHY fis_rollup ( day CHILD OF fis_week CHILD OF fis_month CHILD OF fis_quarter CHILD OF fis_year ) ATTRIBUTE day DETERMINES (day_number_in_week, day_name, day_number_in_month, calendar_week_number) ATTRIBUTE month DETERMINES Oracle9i ʵÌ廯ÊÓͼ µÚ 8Ò³ (calendar_month_desc, calendar_month_number, calendar_month_name, days_in_cal_month, end_of_cal_month) ATTRIBUTE quarter DETERMINES (calendar_quarter_desc,calendar_quarter_number, days_in_cal_quarter, end_of_cal_quarter) ATTRIBUTE year DETERMINES (calendar_year, days_in_cal_year, end_of_cal_year) ATTRIBUTE fis_week DETERMINES (week_ending_day, fiscal_week_number) ; ʵÌ廯 Óͼ Ò»µ©¶¨ÒåÁËάȣ¾Í¿ÉÔ´½ÊµÌ»¯Ó¼¡ÏÖÚÃÇ«×ÅØéÜ Ê²Ã´ÇµÌ廯Óͼ£¬ÔÚºóæÎÒ½«¿¨é¹¦ÄÜÈçÆö ЩʵÌ廯Óͼ¡£ ʵÌ廯Óͼ ¶¨Òå¿É°üÀ¾ÛºÏ£¬ýÈç SUM MIN¡¢ MAX¡¢ AVG¡¢ COUNT(*)¡¢ COUNT(x)¡¢ COUNT(DISTINCT)¡¢ VARIANCE »ò STDDEV, »¹¿ÉÒÔ°üÀ¨¸öò¶àÁª½ÓµÆðıíºÍ GROUP BY¡£¿ÉÒÔ½øÐË÷ýºÍ·ÖǬ»¹Ó¦Ãù±¾µÄ DLL ²Ù ×÷£¬ÀýÈç CREATE¡¢ ALTER ºÍ DROP¡£ Oracle9i ʵÌ廯ÊÓͼ µÚ 7 Ò³
  8. 8. • • SQL ÃÓ¦ òÐ̳ ¸ÍÇÊ Äµ÷à ¬£ Ë´òÒ DBA ¿ÉÒÔÚÈκʱò´½¨»¾³ýµÌå¯Óͼ¶ø²°Ïì SQL Ӧà ³ÌÐò¡£ • Ðí¶àÕ¾µã¼ÒÑ-ÓÁËÊýݲֿ⣬¢ÔÚÆä¨å׺ĸŪ¡ò ´Ë¿ÉÒÔͨ¹ý²éѯÖØÐÀ×¢áÏӸŪ£¬¶ø»ÊÇÆÈçÁã¼ ÖØÐÂÉú³¸ÅÒª¡£ ´ ½¨ÊµÌ廯 Óͼ ʹÓà CREATE MATERIALIZED VIEW Óï¾ä ´½¨ ʵÌå »¯ÊÓͼ ¡£ ͼ 3 ½éÉÜÁË ÈçºÎ ´½¨ Ò»¸ö ÃûΪ costs_mv µÄÊ Ì廯 ÊÓͼ £¬ ͨ ¹ý time ºÍ prod_nam À´¼ÆËã costs µÄ×ܺ͡£ µ±¶¨Òå ÁËÒ» ¸öʵ Ì廯 ÊÓͼ Ö®ºó £¬ ëÐر ØÊñ× ©Ð»Ò æ¹¥µò¼ £¡òÔ SELECT ÁбØë°üº¬ËùÓ GROUP BY ÁУ¬¶ø GROUP BY ÁбØëΪ¼òµ¥ ÐÁ £¡ ªÒèРϺ۾ í±Äµ ½Êï´ ÔÒÉ¿ ÇÊ ÎÈ Îº ĵ SQL Öµ±í ´ï ʽ £¬ ²» °üº¬ ×Ó²é ѯ»òÍø×´¾ÛºÏ¹¦ÄÜ¡£ WHERE ×Ó¾ä ½öÔÊ Ðí°ü º¬ »ùÁÐÉÏ µÄÚ ²¿Í¬ µÈÁª ½ÓÎ ´Ê¡£ ʵÌå »¯ÊÓ Í¼¿ÉÒÔÓµÐ׺Ĵ梹·¶£¬ÕâÑù¾Ö¸¨ËüÚö±í ¼äÖÐÒÔ°ÆÇøÓòµÄ´ó¡£ú»¹¿É½«·×¾üÀ¨¬±ã ʵÌ廯ÓͼÄÚÈÝ´æ¢Ô¶à¸ö±í¿ÕäÖС£ ±íºÍÊÓ¼¶¿ÉÒÔÃÚµÌ廯ĨÖС£ò´Ë¬²ÎÇ°æÀý ×Ó£¬³É±¾¿ÒÔ÷Ϊ»¸öí¶ø²úÆ·Êͼ¡Èç¹û ÔÚʹÓà SYSDATE ºÍ USER ÕâÑùµÄ¹¦ÜʱûÓи¾Ý§ä¯ Êý¾Ý£¬ÄÇôͿÉÒÔ¹ÓÈκ¼¡ ͼ 3 ´ ½¨ÊµÌ廯 ÓͼµÄ SQL Óï¾ä CREATE MATERIALIZED VIEW costs_mv PCTFREE 0 STORAGE (initial 8k next 8k pctincrease 0) BUILD IMMEDIATE REFRESH FAST ON DEMAND ENABLE QUERY REWRITE AS SELECT time_id, prod_name, SUM( unit_cost) AS sum_units, COUNT(unit_cost) AS count_units, COUNT(*) AS cnt FROM costs c, products p WHERE c.prod_id = p.prod_id GROUP BY time_id, prod_name; Oracle9i ʵÌ廯ÊÓͼ µÚ 8 Ò³
  9. 9. ÒѾ-ÓµÐÊýݲֿâÄÕãÉÜß±¸ÁËŪºÍ¹³ÌÀ´Î¬»¤ ͼ¡£Òò´Ë¬ûÃÇÏÀÓ²éѯÖØй¦Äܶø»Â½¨¸Åª ¿ÉÒÔʹÓÃCREATE MATERIALIZED VIEW Óï¾äÒÔ¼° ON PREBUILT TABLE ×Ӿ佫ÒÑ-´æÔÚµÄ±í¢²áΪÊÌ廯ͼ¡£ ʵÌ廯ÓͼÄÃû³Æ±ØÐëíÏଣ¹á©èö´ ½¨¸Ã±í µÄ²é ѯµÄ SELECT ×Ó¾ä ¡£ ¿É ÄÜÎÞ ·¨Ê¼ ÖÕ±£ Ö¤ ²éѯµÄ ¾«¶È Óë ±íµÄ¾«¶ÈÆ¥Å䡣ΪÁË¿·þ´ÊÌâ¬Ó¦¸ÃÔÚ¹æÖаüº WITH REDUCED PRECISION ×Ӿ䡣 ʵÌ廯 ÓͼµÄË÷ÒýÑ¡Ôñ ¸ù¾ÝʵÌ廯ÓͼÖÐÄý¿ºÇ·ñ½øÔöÁˢ£¬ÉÜèÒª´ ½¨ÊµÌ廯ÓͼÄË÷Òý¡£ò´¬¦¸Ã¿ÂÇ×ÏÈöα¾Ø Ë÷Òý£¬ÆäÖаüºùÓʵÌ廯ͼ¹Øסû¿ÉÔ »¯ÊÓͼ¹Øü×ÖÁÐÉϵĥÎË÷Òý¡£ µ±´½¨Ë÷ÒýÊ£¬²»ªÍü¼Ç¿Âøö¶Ôæ¢ÕäÄó° Ë¢ÐÂʱ¼äµÄÓ°Ïì¡£ ʵÌ廯 Óͼ¿ÉÒÔ ê³ Ä Ð©¹¤×÷£¿ ÔÚ´½¨ ʵÌå »¯ÊÓ Í¼Ö® Ç°»ò ¸Õ´ ½¨Ê± £¬ DBA ¿ÉÄÜ »á ¶ÔʵÌå »¯ÊÓ Í¼ ²úÉÕâÑùµÄÒÎÊ£ºËü¾¿¹Ü×öôÔìÙ¢ÐÂðÈçû» Ð ¬£ ²ÊªÎ ØÄ´Ã ¿£ DBMS_MVIEW.EXPLAIN_MVIEW ¹ý³Ì¿É ÒÔÌá ¹© ¸ÃÐÅÏ¢¡£ ¿¼ÂÇÎÒÃÔÚÍ 3 Öд½¨µÄÊÌ廯Óͼ£¬Èç¹ûÎÒÃǶɾ³ý COUNT£¨ *£©£¬È»ºóµ÷ÓÃͼ 4 ÖнéÉܵĹý³Ì DBMS_MVIEW.EXPLAIN_MVIEW¡£Ëü½«¸æßÎÒÃǬ·Öøıäú ×Ù (PCT£© ¿ÉÓã¬ÒòΪ³±¾íÑ-»·ÖÇøËùÐÀà͵IJé¯Ø´ ¼¶ ÇÊ É¿ ÜÄ Äµ £¡ »È µÊÚÓÉ ¬£ø¶ åÌ ¯» ÓÊ ¼Í ÐÖ »Ã ÐÓ COUNT£¨ *£© Ë´òÒ¬£ DML Ö®ºó¿ìËÙ¢ÐÂÒѾ-²»ÉÄÜÁ¡£ ͼ 4 ½âÊÍ µÌ廯 ÓͼʾÀý TRUNCATE TABLE mv_capabilities_table; EXEC DBMS_MVIEW.EXPLAIN_MVIEW (‘ SELECT time_id, prod_name,SUM( unit_cost) AS sum_units, COUNT(unit_cost) AS count_units, COUNT(*) AS cnt FROM costs c, products p WHERE c.prod_id = p.prod_id GROUP BY time_id, prod_name’); SELECT capability_name, possible, related_text, msgtxt FROM mv_capabilities_table; PCT_TABLE:Y COSTS: PCT_TABLE:N PRODUCTS: relation is not a partitioned table REFRESH_COMPLETE:Y Oracle9i ʵÌ廯ÊÓͼ µÚ 9 Ò³
  10. 10. REFRESH_FAST:Y REFRESH_FAST_AFTER_ANY_DML:N see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled REFRESH_FAST_AFTER_INSERT:Y REFRESH_FAST_AFTER_ONETAB_DML:N COUNT(*) is not present in the select list REFRESH_FAST_PCT:Y REWRITE:Y REWRITE_FULL_TEXT_MATCH:Y REWRITE_GENERAL:Y REWRITE_PARTIAL_TEXT_MATCH:Y REWRITE_PCT:Y ʵÌ廯 ÓͼʧРʵÌ廯Óͼܽ³ÖÐøà¿Ø£¬È·±Æä°üºÄý¾ÝÇ× Ì廯ÊÓͼ§ÐµÄ¿Ç±£Ö¤²á·ØÒѾ-ýÝ¡ ÊÓͼËù»ÚµÄ¶ÔÏó¸±ä£¬ü½«êÇΪ§Ð¡ ¿ÉÒÔͨ ¹ý²é ѯ±í USER_MVIEWS À´ÅÐ ¶¨Êµ Ì廯 ÊÓͼ µÄ״̬ ¡£ Èç¹û ¶ÔʵÌ廯ÓͼÄ×´¬Ð²ÃÒÉΣ¿·¢³ö ALTER MATERIALIZED VIEW COMPILE ÃüÁî À´È· ±£ÁË ½â Æä×îР״̬ ¡£ °²È«º¬Òå Êý¾Ý¿âÖеij©ÐÅÏ¢¿ÉÄÜèÒªÞÖÆ·ÃÎÊ£¬²éѯشԱ»Ó ÖÓØ»°²È«úÆ¡£¶ø¬ÉÚËùеķϼÊÇÔ Oracle9i ·þ ÎñÆ÷ÖнøµÄ£¬ËùÒÔ¿ÉÏòÊý¾ÝºÍÌ廯Ӽṩ¸üÇ´ó± »¤¡£ ΪÁË ±ÜÃâ ¶Ôʵ Ì廯 ÊÓͼ »òÏêÇé ±íµÄ δÊÚ È¨·Ã ÎÊ £¬ ÃÓ¹Ê CREATE MATERIALIZED VIEW ʱ½«ÐèÒª CREATE MATERIALIZED VIEW ȨÏÞ¡¢¶ÔêÇé±íµÄ SELECT WITH GRANT ȨÏÞ¡¢ÒÔ¼°¶ ʵÌ廯 ÊÓͼ ÈÝÆ÷ ¶ÔÏó µÄ SELECT WITH GRANT ºÍ INSERT ȨÏÞ ¡£ ´ËÍ⣬Èç¹ûÓû§ÔÚijÎÇëóÖж±í½ø·ÊÕ©ÒѾ-¨ ÒåÁË»¸öò¶àʵ̯Óͼ£¬ÄÇô§½«±ÔÐí·Î ͼ£¬¶ø²»¿ÂǸ³ÓèʵÌå¯ÈÝÆ÷±íĨÏÞ¡Òò´ËÎÛéÑÔ× ºÎ´¦£¬Ö»ÓÐͨ¹ýÁË°²È«¼ìéÅÄÜ·Ãʾݡ Oracle9i ʵÌ廯ÊÓͼ µÚ 10 Ò³
  11. 11. ÔÚÀúÊ·ÉÏ£¬¸ÅÒª±í¹ÓÃý³ÌÖеĻöÎâ¾ÍÇõ¼Øº Ëæºó¸üС£Õâ©ÎÊÌâÏÖÔڵýÁ˾ö£¬Òòª¸Å¹ÜÀíá©ÁËһϵ ÁлúÖÆʵÏÂÄ¿£º • • /ºÏ²¢·Éú±ä»¯µÄÚÈÝ • Òò´Ë£¬ DBA ±ØÐ뿼ÂÇèÒª¶àÉÙÊäÀ´½¨ºÍά»¤Ã¸öµÌå¯Ó ͼ£¬²¢ÔÚ»¨·ÑʱäÓë¹Ã¸µÌå¯ËùñÄÐÜÉÆÖ®½ø Ðƽºâ¡£ Oracle 9i ÌṩÁËÏÂÃæµÄ¢Ð·½¨£º • • • ÕâЩ²Ù×÷¿ÉÒÔ • • (DBMS_MVIEW.REFRESH) • (DBMS_MVIEW.REFRESH_DEPENDENT) • (DBMS_MVIEW.REFRESH_ALL_MVIEWS) • °´ÐèË¢Âͨ¹ýµ÷ÓÃÉÏæùÁijÌÖ®Ò»ÀÊ£¬¶ø±å¯ ͼ·¢Éú¸Ä±äʳÓè DBA ÍêÈ«µÄ¿ØÖƨÏÞ¡£ Èç¹ûÑ¡ÔñÁËÌá½»¢Ð£¬ÖҪʵå¯Óͼܴý¾Ý±äÄ°Ïì Ëü¶¼½«×Ô¯¸ÐÂÒ·´Ó³ÃÊý¾Ý¡£È»ø¬ÇëÕâ÷Ϊù ±í·¢Éú¸üĵÊÂÎñÖÐÌá½»´¦ÀÒ²¿¶ø¡£òˬ ÐÂËù»¨·Ñʱ¼äáÉÔ΢³¤Ò©£¬òªè¶-íºÍÓåÖ°ü ¸Ã±íµÄÊÌ廯Óͼ¬×÷³öü¡£ ÍêȫˢРµ±ÊÌå »¯ÊÓ Í¼ê È«Ë¢ ÐÂʱ £¬ Ê× ÏÈ ½Ø ¶Ï ¸Ã ÊÓ Í¼ £¬ È» ºó ¼ÓÔØËù ÓÐÊý ¾Ý¡£ ¼øÓÚʵÌ廯ÍÄ´óС£¬Õâ¿ÉÜáÇÒ¸öº±¹ý³êÈ«Ë¢ ÐÂÔÚÏÁÇé¿öÊÒ»îºÜõļõ£ • 50% ÒÔÉÏ¡£ Oracle9i ʵÌ廯ÊÓͼ µÚ 11 Ò³
  12. 12. • • ¿ìËÙ ¢Ð ÓÐÒ»©ÊµÌ廯Óͼ¿ÉÄÜá·Ç³£´ó¬²¾ßÐÖÀýêȫˢ ËùÐèµÄʱ¼ä¡£Ò»ÖÌæ´ú·½¨Çø¿ì٢¬ö«í¯ µÄ²¿·ÖӦýÊÌ廯ͼС£ÔØý¾ÝâÈκ±í ½«±»Ê¶ð£¬Èºó²Î¿¼¸ÃíµÄÌå¯ÓÍÀûÐÂý¾Ýø×Ô ¶¯¸üС£ ΪÁ˽øпì٢²×÷£¬±Øë«Êý¾ÝÖµÄ仯¼ÇÏÀ´Ó ·½¨¿ÉÒÔʵÏÖÕâ»Ä¡£Èç¹ûø¾-Óà SQL*Loader Ö±½Ó·¾¶« Êý¾Ý²åÈëµ½¿âÖУ¬ÄÇôˢ»úÆ«¼ìÕÒ¹³Ì¸÷ Òª¼ÓÔصÄÐÂÊý¾Ý¡£È»¶ø¬´óà±ä¯Çͨ¹ SQL ÃüÁî INSERT¡¢ UPDATE ºÍ DELETE À´½øС£ÔÚÕâÖÇé¿öϬʵÌå »¯ÊÓͼËùÚµÄÿ¸ö±í¶ÐèÒª·Ý MATERIALIZED VIEW LOG¡£ ÿ¸ö±í½ÐèÒª»·ÝÈÕÖ¾£¬ÊµÌå¯ÓͼÔÚ¶ø² ÓÊ ¼Í ÐÖ £¡ òÒ çÈË´ ýÊû¹ ÐÖâ¿Ý¾ ö½ ÐÓ 6 í±ö¸ úÉ¢· ä±ËÁ ¬£¯» «½òÔ ªÒèÐö½ 6 ·ÝʵÌ廯ÓͼÈÕÖ¾¡£«¿ÉÒÔÃκýÁÄÀ´¹â ЩÈÕÖ¾¡£ Çë×¢Ò⣬²·ËùÓеÄÊÌ廯ͼ¶¿ÉÔ½øìÙ¨¹ý µ÷Óà DBMS_MVIEW.EXPLAIN_MVIEW ¹ý³ÌÀ´È·ÏÆäÊÇñ¿ÉÒÔ ¿ìË٢С£¸Ã¹ý³Ì»½¨Òé誶Ôʵå¯ÓͼøÄ©²×÷¬´ ʹÆä¿ÉÒÔìË٢С£ ˢкÍÔ¼ÊøÌõ þ Ç°ÃæÔø¾-Éù÷¹ý£¬ÀíÏëé¿ö¼ÊÌõþ¨ÓÈÆäÍâ²Øü×Ö ÊøÌõ¼þ£©Ó¦¸ÃÔÚʵ±íÖж¨Ò壬´øÈ·Ä»¿Éë Ò»¸öά¶ÈÏàÆ¥Åä¡£µ±Ìá½´ÊÔ¼øõþЩ DBA »òÐíáÓÊÖ Ê¾Ò⣬ Ðû³Æ ÓÉÚ ¿ÉÄÜ µÄÐÔ ÄÜ¿ªÏú £¬ ¸Ã Êý¾Ý ¿âÖÐ ½«²» ÔÙÓм ÊøÌõ ¼þ ¡£ È»¶ø£¬ DBA ¿ÉÒÔÈ·ÐÅ£¬Í¨¹ýÊÓà ALTER TABLE <table name> ENABLE NOVALIDATE CONSTRAINT <name> ×Ӿ䣬 ¿ÉÒÔÁ¢¼´ÆôÓÃÊøÌõþ¶ÎÞÐèì²éý¾Ý¡£Èç¹û SQL*Loader Ö±½Ó·¾¶«ÊýݼÔصíУ¬ÄÇôÈÏé¿öËùøÌõ ¼þ¶½«±»ûÓá£ÔØʵíÖ®ºó¬·¢³öÆôï¾ä NOVALIDATE »áÁ¢¼´ÆôÓÃÔÊøÌõþ¶ÎÞÐëì²éý¾Ý¡£ÒòËüÚرä ûÓÐÈκ°Ï죬ÆôÔ¼ÊøÌõþ²èÒª¨·Ñ±ä¡¶ÉÚ ¼ÓÔصÄÊý¾ÝûнøÑéÖ¤£¬Òò´ËÈ·±ù²á°Ïì ÈκÍêÕûÐÔ¼ÊøÌõþ¾ÓªÖØÒ¡£ Oracle9i ʵÌ廯ÊÓͼ µÚ 12 Ò³
  13. 13. Êý¾ÝËäÈ»ÒÑ-¢Ð£¬µ«Ìå¯ÓͼԿÉò¹¨ ÃüÁîALTER SYSTEM SET QUERY_REWRITE_ENABLED = FALSE ½ûÓà ²éѯ ÖØд £¬ Ö± ÖÁËùÓРʵÌå »¯ÊÓ Í¼¶ ÒѾ- ˢР¡£ Áí Ò»·½ Ã棬¶ÔÓÚÄÇЩ²»èҪʵÌå¯Í¼·´³×îÂØý¾Ý§¿É Ôڻ᰼¶ÆôÓòéѯÖØд¡£ ·ÖÇøά»¤²Ù×÷ºÍʵÌ廯 Óͼ Oracle9i Ìṩ ÁËÒ» ¸ö³Æ Ϊ ·ÖÇø±ä »¯¸ú ×Ù £¨ PCT£© é×ĵ ¬£þ¼ é×ø þ¼ ¿ÉÒÔ͸Ã÷µØ¼ì²â½·ÖÇø¢ú±ä»¯£¬ÈºóÅж¨Ù×Êñ¹Ìå »¯ÊÓͼÖеÄý¾Ý²Ò¡£ÀÈ笺Ϣ·ÇøòÌí½«á°ì µ½ÊÌ廯Óͼ£¬Ö´ÐÕâÑùĹý³²á±êÇΪ§ Ч¡£ ·ÖÇø±ä»¯¸ú×Ù¹¿ÉÒÔÓÃÚ÷²°ÏìÁËÄЩʵÌåͼ С£ÀýÈ笹ûÒ»¸öÏêÇé±í·Öø½Ø¶òɾ³ PCT ½«Ö¸Ã÷ʵÌå »¯ÊÓͼÖÐÜ°ÏìµÄ²¢½«Æäɾ³ý¡£ DBMS_MVIEW.EXPLAIN_MVIEW ¹ý³Ì½«¨ÒéĻʵå¯Óͼ ÊÇ·ñ¿ÉÒÔ¹Óà PCT¡£ ²éѯÖØд ÔÚ¸ÅÒª¹ÜÀíµÄÖî¶àÓãУ¬ÕËû§æý³ÆÞ÷®ÊÇ ²éѯÖØд¹¦ÄÜ¡£ÕâÊÇÒ»ÏîÓżõ¬×ª¸ù¾Ý±íºÍ À´±àеÄÓû§²éѯ£¬¶øÊÌåͼ־ðÈ¡ýÝá¸ßËÙ ¶È¡£ÓÉÚ Oracle9i ·þÎñÆ÷¿ÉÒÔ׶¯ÖØдȺÏ浀 SQL Ó¦Ã³Ì ÐòÒÔʹÓõÌ廯ͼ£¬´Ëü¶ÚÖէǸ÷ÄÎÞè SQL ӦóÌÐòµÄ¸ÉÔ¤»áʾ¡£ËäȱÎÖùÒý¶¼Ç SQL SELECT ×Ӿ䣬µ«²éѯÖØдÈÔ½ÒÀÚ°üº SELECT ×Ó¾äµÄ INSERT ºÍ CREATE TABLE Óï¾ä¡£ ²éѯÖØд¿ÉÒÔÓÃÚ¸÷¡£Çëע⬶ÎÈÏóùµÄ¹ ϵÎÞÐèÇ¿ÖÆ´£¬«¼ÙÉËüÃÊÕæÄ¡Èç¹ûù÷ØÓë±íý¾Ý ÖдæÔÚµÄʼ¹Øϲ»Æ¥Å䣬ÇñéѯÓÎÌâ ÉùÃ÷À´ÖØвéѯʱ£¬ºÜ¿Ä»áúíÎó½¹û¡ýͨ ¹ý¶¨ÒåØϵºÍÊÓÃÔ¼øÌõþ±ã³È·£¾ÝÄÕЬËùÉú µÄ±¨¸æ½«ÊÇ¿ÉÐÅ£¬ÆäÖ°üºÕýÈ·á¹û¡ÏÁËͳêÔ ºó£¬ ¿É ÒÔ ½ö Ͷ Èë ×î С µÄ Ŭ Á¦ºÍ¿ª Ïú¾Í »ñµÃ ¿ìËÙ ¡¢ ×¼ È·µÄ²é ѯ½á ¹û ¡£ ²éѯµÄ׳ɻÔÙÐèÒªÓëÊÌåͼ¶¨¾«È·Æ¥Å䣬òÎÕâÇó DBA Ê ÏÈ Öª µÀ Ó¦ ¸Ã Õë ¶Ô Êý ¾ÝÖ´Ð ÄЩ ²éѯ ¡£µ± È»Õâ ÊDz»¿É Äܵ £¬ ÓÈÆä¶ÔÊý¾Ý²Ö¿â¸üÇç´Ë£¬ÒòΪڹ«µÄ÷ºÃ¦® Ò»¾ÍÊÇÁ¢¼´ÖеIJéѯ¡£òˬ¹ÓÃÌå½ö¿Éú×ã ²éѯµÄÒ»¿·Ö£¬Øд¹ÊÇá¢Éú¡ Oracle9i ʵÌ廯ÊÓͼ µÚ 13 Ò³
  14. 14. /½ûÓòéѯÖØд µ±ÉèÖÃÁËÏÂвÎÊý£¬éѯش½«·¢úº ALTER SESSION SET QUERY_REWRITE_ENABLED = TRUE »ò ALTER SYSTEM SET QUERY_REWRITE_ENABLED = TRUE »òµ±¶¨ÒåʵÌ廯ÊÓͼʱ£¬Í¨¹ý°üÀ¨×Ó¾ä QUERY ENABLE REWRITE ÒÔʹÆä·ûºÏ²éѯÖØдÌõ¼þ¡£ ÓÐʱ¿ÉÄÜèÒª½ûòéѯÖØ´£¬ÕâÔͨ¹ý«ÏæµÎ¸ FALSE À´ÊµÏÖ£¬Ò²¿ÉÔÚ»¸ö¾ßÌå¯Óͼ¹Ã DISABLE QUERY REWRITE ×Ó¾äÀ´ÊµÏÖ¡£ ²éѯÖØдµÄÀà Í Oracle9i ÖÐ¿É ÒÔÓÐ ¸÷Ö ²é ѯÖØд ÀàÐÍ £¬ ÏÂÃæ µÄÀý ×Ó½é ÉÜÁËʹ ÓÃͼ 5 ÖеÄÊÌ廯Óͼ¿ÉÒÔÏ©¹¦Ü¡£ ͼ 5 ʵÌ廯 Óͼ à ڲéѯÖØдʾÀý CREATE MATERIALIZED VIEW all_cust_sales_mv BUILD IMMEDIATE REFRESH COMPLETE ENABLE QUERY REWRITE AS SELECT c.cust_id, p.prod_id, sum(s.amount_sold) AS dollars, sum(s.quantity_sold) as quantity FROM sales s , customers c, products p WHERE c.cust_id = s.cust_id AND s.prod_id = p.prod_id GROUP BY c.cust_id, p.prod_id; ÍêÈ«Æ¥Åä µ±ÊÌ廯Óͼ¶¨Òë²éÑêÈ«Æ¥Å䣬½á·¢Éú×îòÄ ¯Ñ ØÖ ²Ò£¡´Ð ÇÊ; ¬£µË FROM ×Ó¾ä ÖÐµÄ ±í ¡¢ WHERE ×Ó¾äÖÐ µÄÁª ½Ó ºÍ GROUP BY ×Ó¾äÖеĹؼüÔÚ²éѯºÍÊÌå»®êÈ«Æ¥ Åä¡£ÀýÈ笸ø³öÏÂÃæµÄ²éѯº SELECT c.cust_id, sum(s.quantity_sold) as quantity FROM sales s , customers c, products p WHERE c.cust_id = s.cust_id AND s.prod_id = p.prod_id GROUP BY c.cust_id, p.prod_id; Oracle9i ÖØд˲éѯÒÔʹÓà all_cust_sales_mv Oracle9i ʵÌ廯ÊÓͼ µÚ 14 Ò³
  15. 15. ÓÐʱºò²éѯ¿ÉÄÜ»áμ³ÒÁ£¬¸Ã´æ¢ÔÚŪíÖµ«Ç ¿ÉÒÔͨ ¹ý½« ʵÌå »¯ÊÓ Í¼Áª ½Ó·µ»Ø µ½ºÏ ÊµÄ Î¬±í À´ÖØ Ð»ñµÃ ¡£ Àý Èç £¬ ÔÚÇ°Ãæ µÄÀý ×ÓÖÐ £¬¼Ù É豨 ¸æ²»ÊÇ ¸ù¾Ý Óû§ ID£¬¶ø ÊÇ Ê¹Óà »§Ãû ¡£ SELECT c.cust_last_name, sum(s.quantity_sold) as quantity FROM sales s , customers c, products p WHERE c.cust_id = s.cust_id AND s.prod_id = p.prod_id GROUP BY c.cust_last_name, p.prod_id; ´Ë´Î²éѯ²Î¿¼ c.cust_last_name ÁУ¬¸Ã²»ÔÚʵÌå¯Óͼ all_cust_sales_mv ÖУ¬µ« c.cust_last_name ÔÚ¹¦ÄÜÉÏÈ¡¾öÓ c.cust_id£¬ Õâ ÊÇÓÉ ËüÃÇ Ö®¼ä µÄ²ã´Î ¹Øϵ ¾ö¶¨ µÄ¡£ Õâ Òâ ζ×ű¾ ´Î²é ѯ ¿ÉÒÔͨ ¹ý all_cust_sales_mv À´ÖØ Ð´£¬ ÒòΪ all_cust_sales_mv ªÁ»± ½Ó·µ»Ø¿Í§±íÒÔñà c.cust_last_name ÁС£ ¸ÅÒª»ã×Ü ò¾ÛºÏËùÓÐ µ±²éѯÔÚijһã´ÎÖÐÇëó¾ÛºÏ£¨Èç SUM £¨ÏúÊÛ©¬¸Ã¾ºµÄ ¼¶±ðȾۺÏÔÚʵÌ廯ÓÍÖдæ¢Ä¸üߣ¬¿ÉÒ¨¹ýà Ì廯ÊÓ Í¼ £¬ ÒÔ ¼° ½« ¸Ã ¾Û ºÏ »ã×ܵ½ ËùÐè ¼¶±ð ´Ó¶ø ¶Ô¸Ã ²éѯ½ø ÐÖØ Ð´ ¡£ ÀýÈ磬ÎÒÃǵÄÊÌ廯Óͼ all_cust_sales_mv ÊÇÔÚ¿Í»§¼¶±ðÉÏ×éÖ¯ Êý¾ÝµÄ£¬«ÎÒÃÇ¿ÉܸüÏÍûÔÚ״̼¶±ð½öÓ»§À¨æ¡ ÎÒÃÇѾ-´½¨ÁË¿Í»§¬£üèÊöºøÓòÖ®¼äµÄ¹ØÏ¡ ÏÂÃæµÄ²éѯ¿ÉÒÔʹÓÎÇÌå»Í¼ all_cust_sales_mv À´²úÉ ±¨¸æ£¬½«Ä³Ò»¿Í§µËùÓÐÊý¾Ý¶¼ÛºÏÆðÀ´²¢ã×ÜÌ¡ SELECT c.cust_state_province, sum(s.quantity_sold) as quantity FROM sales s , customers c, products p WHERE c.cust_id = s.cust_id AND s.prod_id = p.prod_id GROUP BY c.cust_state_province; Êý¾Ý×Ó¼¯ Æù½ñΪֹ£¬ÒÃÇѾ-¿´µÄËÓÐÊÌ廯ͼ°üºÁÈ«²ýÝ µ«ÕâÈÔ »áµ¼ ÖÂÒ» ¸ö·Ç ³£ÅÓ ´óµÄÊ Ì廯 ÊÓͼ ¡£ Oracle9i ÔÊÐí¶¨ Òå» ¸ö ʵÌ廯 ÊÓͼ £¬ Ëü½ö °üº¬ ͼ 6 ÖÐËù ʾµÄ ²¿·Ö Êý¾Ý £¬ ¼´ Dublin, Galway, Hamburg ºÍ Istanbul µÄÊý¾Ý¡£ Oracle9i ʵÌ廯ÊÓͼ µÚ 15 Ò³
  16. 16. 6 °üº¬Êý¾Ý×Ó¼¯µÄÊ Ìå» Óͼ CREATE MATERIALIZED VIEW some_cust_sales_mv BUILD IMMEDIATE REFRESH COMPLETE ENABLE QUERY REWRITE AS SELECT c.cust_id, p.prod_id, sum(s.amount_sold) AS dollars, sum(s.quantity_sold) as quantity FROM sales s , customers c, products p WHERE c.cust_id = s.cust_id AND s.prod_id = p.prod_id AND c.cust_state_province IN ('Dublin','Galway','Hamburg','Istanbul') GROUP BY c.cust_id, p.prod_id; ÏÖÔÚ¸ÃʵÌ廯ÊÓͼ¿ÉÒÔÓÃÓÚÂú×ã°üº¬·¶Î§µÄ²éѯ£¬ ÀýÈçÏÂÃæËùʾµÄ IN ºÍ BETWEEN ×Ӿ䡣 SELECT c.cust_state_province, sum(s.quantity_sold) as quantity FROM sales s , customers c, products p WHERE c.cust_id = s.cust_id AND s.prod_id = p.prod_id AND c.cust_state_province IN ('Dublin','Galway') GROUP BY c.cust_state_province; ²éѯÖØдÍêÕû Ô·½Ê ¸ÅÒª¹ÜÀí½«¾¡Á¦Ê¶±ð²»ÖµÄÌå¯Óͼ¢´òÉÏàêÇ£¬ ΪÁË¿·þÕâЩÎÊÌ⣬¿ÉÒÔͨ¹ý² QUERY_REWRITE_INTEGRITY À´Ñ¡ÔñÈýÖÍêÕûм¶±ð£ • STALE_TOLERATED • TRUSTED • ENFORCED (default) ÔÚ STALE_TOLERATED ·½ÊÖÐ ½«Ê¼ ÖÕʹ ÓÃÒ» ¸öʵ Ì廯ÊÓ Í¼£¬ ¼´ ʹËüÒÑ ¾-ʧ Ч ¡£ ÔÚ TRUSTED ½· ¬£ÐÖ½Ê ¯»ÅÓ òÐ̳ ÅÐàÏ µÊ åÌ ¯» ÓÊ ¼Í ÖеÄÊý ¾ÝÊÇ ×îРµÄ £¬ ÔÚ Î¬ ¶ÈÖÐÉù Ã÷µÄ ¹Øϵ ºÍ RELY Ô¼ÊøÌõ ¼þÊÇ Õý È·µÄ¡£ÔÚÕâÒ»½ÊÖЬÓů³Ìò¹«Ã¤Ï¨Á¢åͼ »òùÓÚ ÊÓͼ µÄÊ Ì廯 ÊÓͼ £¬ ²¢ ʹÓÃ Î´Ö ÐºÍ ÒѾ- µÃ µ½Ö´Ð µÄ¹Ø ϵ ¡£ ÔÚÕâÖ·½ÊУ¬ÓÅ»¯³Ìò¹¡°ÈαÒѾ-ÉùÃ÷µ«´ ENABLED VALIDATED µÄÖ÷ /Ψһ¹Ø¼ü×ÖÔÊøÌõþºÍýά¶È¸Ã÷µÄ¾Ý ¹Øϵ¡£ Oracle9i ʵÌ廯ÊÓͼ µÚ 16 Ò³
  17. 17. ENFORCED ·½ÊÇĬÈÏ£ÔÚÕâÖÐÓÅ»¯³Ìò«¹ ÓÃÈ·ÖªÆä°üº¬×îÐÂÊý¾ÝµÄÌ廯ͼ£ÒÔ¹ùÚ ENABLED VALIDATED Ö÷ /Ψһ /Íⲿ¹Ø¼ü×ÖÔÊøÌõþµÄÏ¡£ Òò´ËÄú½«·¢ÏÖ£¬Èç¹û»Ð©Ô¼ÊøÌõþÉΧÇÃÓ¸¨µ ²éѯÖØд½«»á·¢Éú¡£µÈç¹ûÊÓÃÄÇÔ¼øÏ TRUSTED »ò STALE_TOLERATED ģʽ¬ÕâЩ²éѯÖØ´Ôò»á·¢Éú¡ ½á¹ûÊÇ·ñÕýÈ µ± SQL ²éѯ ʹÓà ʵÌå »¯ ÊÓͼ¶ø ·ÇÕæ 浀 Êý¾Ý Դʱ£¬ ÓÐʱºò »á³ö ÏÖ ·µ»Ø½á¹û²Í¬ÄÇé¿ö¡£ 1. ʵÌ廯 ÊÓͼ ¿ÉÄÜ ÓëÏê ϸÊý ¾Ý²»Í¬ ²½¡£ ·¢Éú ÕâÖ Çé¿ö µÄ Ô-Òòͨ ³£ ÊÇˢйý³ÌÒÆð£¬Í±Ñ¡ÔñÁ STALE_TOLERATED ÍêÕûÐÔ ·½Ê¡£ 2. Áª½ÓÐ ¿ÉÄÜ Ó°Ïì ²Î¿¼ µÄÍê ÕûÐÔ¡£ ÔÚÕâ ÖÇé ¿öÏ £¬ Ò» Щ ×Ó¶ËµÄ Ð Ã»ÓÐÕý È·»ã ×ܵ½ ¸¶Ë µÄÐ ¡£ Ϊ ÁË ±Ü Ãâ ·¢ Éú Õâ Ö Çé ¿ö £¬ ÇëʹÓà ϵ ͳǿÖÆ Ö´Ð µÄÍê ÕûÐÔ £¬ Õâ Ñù¿ªÏú ÊÇ΢ ²»×ã µÀÄ £¬ ¶ø »ñÀûÈ´ ºÜ ÏÔ Öø¡£ µ±ÊÌ廯Óͼ°üº¬Á˲ÔÙÎÚÏêÇéý¾ÝÖÐÄÅ¢£¿ÉÒ´ ½¨Ò»¸ö rolling materialized view¡£ÀýÈç¬ÊµÌ廯Óͼ¿ÉÄÜ°üº 18 ¸öÔ嵀 Êý¾Ý £¬ µ« ÏêÇé ±íÖÐ ½ö°üº¬ ×î½ü 6 ÂÔö¸ £¡Äµ Ë´òÒ ¬£ çÈ û¹ é² ¯Ñ ½«ÔÚ»ù±íÖжø²ÊǵÌå¯Óͼ£¬ÄôϾá¹û¡ ½âÊÍÖØд µ±Ê¹ÓòéѯÖØд£¬Î×îÆ·ÄÌâÇ¡°Ë»áÂð¿ »ò¡°ÎªÊ²Ã´ËéѯÖØУ¿± Oracle9i ͨ¹ý DBMS_MVIEW.EXPLAIN_REWRITE ¹ý³Ì¸øöÁËÕâÒ»ÎʵĽ ¾ö°ì·¨ £¬ ͼ 7 ÖÐ ÏÔ Ê¾ ÁË Ò» ¸öʹÓà ·¶Àý ¡£ Òò ´Ë£¬ ¿ÉÒÔ ÔÚ²éѯ ÔËÐ Ö® Ç°Á˽â¸ÃÐÅÏ¢¡£ ²éѯÎı¾×÷ªÒ»¸ö³¤Ö·û´®«µÝ£¬Ã¹ýÌæ¢ÆäÔÚí REWRITE_TABLE ÖÐ ÕÒ µ½ µÄ½á¹û £¬ ±Ø Ðë²é ѯ¸Ã ±í²Å Äܲ鿴 ¹ý³Ì µÄ ½á¹û¡£ÔÚÏÂÃæµÄÀý×ÓÖЬÎÒÇ«¿´ some_cust_sales_mv ÓÃÚ´Ë ´Î²éѯ¡£ Oracle9i ʵÌ廯ÊÓͼ µÚ 17 Ò³

×