¸ßЧSQL²éѯ֮Ë÷Òý¸²¸Ç(index coverage)
½ñÌì×öSQL ÓÅ»¯£¬²éÕÒÖ´Ðмƻ®Ê±£¬Ö´Ðмƻ®£¬·¢ÏÖ´ËÖ´Ðмƻ®ÓëÒÔÍùµÄ¼Æ»®ÓÐËùÇø±ð£»ÕÒ¼»¥ÁªÍø£¬ÖÕÓÚÕÒһƪÓйØÑо¿±È½ÏÉîÈëµÄÎÄÕ£»
ÔÖ´Ðмƻ®Ê¹ÓõÄÊÇË÷ÒýɨÃ裬ͻȻһÏ»áʹÓÃË÷Òý¸²¸Ç¼¼Êõ£¬Ð§ÂÊ´óÔö£»
SELECT * µÄÕæÏࣺË÷Òý¸²¸Ç(index coverage)
SELECT *µÄЧÂʺÜÔã¸âÂ𣿵±È»£¬ËùÓÐÈ˶¼ÖªµÀÕâÒ»µã£¬µ«ÊÇÎªÊ²Ã´ÄØ£¿
ÊÇÒòΪ·µ»ØÁËÌ«¶àµÄÊý¾Ý£¿
ÕâÊÇÒ»¸öÆÕ±éµÄ»Ø´ð£¬µ«ÎÒ²»ÕâÑùÈÏΪ¡£Èç¹ûÄãµÄÊý¾Ý¿âÉè¼Æ¹æ·¶ºÏÀí£¬ÄÇô´ø¿íÕ¼ÓÃʵ¼ÊÉϷdz£µÄС¡£
ÈÃÎÒÃÇ¿´¿´ÏÂÃæµÄÀý×Ó¡£ÏÂÃæµÄ²éѯ½«»á´ÓAdventureWorks.dbo.TransactionHistoryArchive£¨×ܹ²´óÔ¼Óнü9ÍòÐÐÊý¾Ý£©ÖÐÑ¡Ôñ³ö326ÐÐÊý¾Ý¡£µÚÒ»¸öʹÓÃÁËSELECT * ²éѯ£¬ºóÒ»¸ö²éѯÔòÓÐÃ÷È·µÄ×ֶΡ£
SELECT * from Production.TransactionHistoryArchive
WHERE ReferenceOrderID < 100
SELECT ReferenceOrderLineID from Production.TransactionHistoryArchive
WHERE ReferenceOrderID < 100
ÔÚÕâÖÖÇé¿öÏ£¬Á½ÕßÔÚÍøÂç´ø¿íµÄÇø±ðÖ»ÓÐ15K£¨180K-165K£©£¬´óÔ¼10%µÄ´ø¿í²îÒì¡£µÄÈ·ÖµµÃÈ¥ÓÅ»¯£¬µ«²»»áÓкܴóµÄЧ¹û¡£
SELECT * ½«Ôì³É±í/Ë÷ÒýɨÃè
SELECT * µÄ×î´óÎÊÌâÊǽ«Ó°Ïì²éѯ¼Æ»®¡£SQL ServerÖ÷ҪʹÓÃË÷ÒýÈ¥²éѯÄãÐèÒªµÄÊý¾Ý£¬µ±Ë÷Òý°üÀ¨ËùÓеÄÄãÇëÇó²éѯµÄ×ֶΣ¬SQL Server½«²»ÐèҪȥÔÚ±íÖвéѯ¡£Õâ¸ö¸ÅÄî³Æ×öË÷Òý¸²¸Ç¡£ÔÚÉÏÃæµÄÀý×ÓÖУ¬µÚÒ»¸ö²éѯ½á¹ûÊÇÔÚ¾Û¼¯Ë÷ÒýɨÃèÖУ¬·´¹ýÀ´£¬µÚ¶þ¸öÀý×ÓʹÓÃÁ˸ü¶à¸üÓÐЧÂʵÄË÷ÒýɨÃè¡£ÔÚÕâ¸ö°¸ÀýÖУ¬Ë÷ÒýɨÃè±È¾Û¼¯Ë÷ÒýɨÃè¿ì100±¶ ¡£
³ý·ÇÄãÒѾ½«ÎªÃ¿¸ö×ֶν¨Á¢ÁËË÷Òý£¨ÏÔÈ»²»ÊǸöºÃÖ÷Ò⣩£¬SELECT *ÊDz»Äܹ»ÀûÓõ½Ë÷Òý¸²¸Ç£¬ÄãÖ»ÄÜÈ¥×öɨÃè²Ù×÷£¨·Ç³£µÄûÓÐЧÂÊ£©¡£
Èç¹ûÄãÖ»ÊDzéѯÄãËùÐèÒªµÄ×ֶΣ¬ÄÇÄã¸ü¿ÉÄܵĸ²¸Çµ½ÄãµÄË÷Òý¡£ÎÒÏëÕâ¾ÍÊDz»ÍƼöʹÓÃSELECT *µÄÖ÷ÒªµÄÔÒò¡£
Îȶ¨ÐÔ·½Ãæ
ÔÚά»¤Ò»¸öÓ¦ÓóÌÐòʱ£¬SELECT *Ò²»á´øÀ´Ò»Ð©ÒâÏë²»´óµÄÎÊÌâ¡£Ëü»áÒýÆðÄãµÄ´úÂë·¢ÉúһЩ²»È·¶¨ÐÔ¡£Èç¹ûÄãÔö¼ÓÁËÒ»¸öÐУ¨Òë×¢£ºÎÒ¾õµÃÕâÀïÓ¦¸ÃÊÇ×ֶΣ©µ½Ò»¸ö±íÖУ¬ÄÇôSELECT * ·µ»ØµÄ½á¹ûµ½ÄãµÄÓ¦ÓóÌÐòÖн«»áÔڽṹÉÏ·¢Éú±ä»¯¡£Á¼ºÃµÄÓ¦ÓóÌÐòÓ¦¸ÃÊÇʹÓÃ×Ö¶ÎÃû³ÆµÄ£¬¶ø²»Ó¦¸ÃÊÜ´ËÓ°Ïì¡£µ±Íâ½ç·¢Éú±ä»¯Ê±£¬Á¼ºÃµÄÓ¦ÓóÌÐòÉè¼ÆÒ²Ó¦¸Ã×îС»¯µÄ¸ü¸Ä¡£
Ïà¹ØÎĵµ£º
ÕÒ³öTABLE1ÖÐ
Column1 Âú×ãvalue1
»òÕß
Column2 Âú×ãvalue2
»òÕß
Column3 Âú×ãvalue3
µÄ¼Í¼
SELECT top 10 * from TABLE1
WHERE
CASE WHEN COLUMN1='value1' THEN 1
ELSE
(CASE WHEN COLUMN2='value2' THEN 1
ELSE
(CASE WHEN COLUMN3=value3 THEN 1 ELSE 0 END)
END)
END =1 ......
sql 2005±íµÄ¸´ÖÆÓÐÁ½ÖÖ£ºÒ»ÖÖ¾ÍÊǰÑÕû¸ö±í¸´ÖƹýÈ¥£¬¾ÍºÃÏñ¸´ÖÆÎļþ²¢ÇÒÖØÃüÃû¡£±ðÍâÒ»ÖÖ¾ÍÊǰѱíµÄÄÚÈݸ´Öƹý³ö.
select * into newtable form oldtable;°Ñoldtabel¸´ÖƵ½newtableÇÒnewtable²»´æÔÚ,·ñÔò³ö´í.;
insert into newtable select * from oldtable°ÑoldtableµÄÄÚÈݲåÈëµ½newtable, newtableÒ»¶¨Òª´æÔÚ, ......
¸øÄã¸ö×îÏêϸµÄ°É ¿ÉÄÜÓÐÄãÒªµÄÄÚÈÝ
ËøµÄ¸ÅÊö
Ò». ΪʲôҪÒýÈëËø
¶à¸öÓû§Í¬Ê±¶ÔÊý¾Ý¿âµÄ²¢·¢²Ù×÷ʱ»á´øÀ´ÒÔÏÂÊý¾Ý²»Ò»ÖµÄÎÊÌâ:
¶ªÊ§¸üÐÂ
A,BÁ½¸öÓû§¶ÁͬһÊý¾Ý²¢½øÐÐÐÞ¸Ä,ÆäÖÐÒ»¸öÓû§µÄÐ޸Ľá¹ûÆÆ»µÁËÁíÒ»¸öÐ޸ĵĽá¹û,±ÈÈ綩Ʊϵͳ
Ôà¶Á
AÓû§ÐÞ¸ÄÁËÊý¾Ý,ËæºóBÓû§ÓÖ¶Á³ö¸ÃÊý¾Ý,µ«AÓû§ÒòΪijЩÔÒòÈ¡Ï ......
×÷ÖÐÊý¾Ý¿â¾³£³ö´íËÀËø£¬²¢ÇÒ»¹ÒªÒªÇó½â¾öµ±Ç°µÄËÀËø£¬ÎÊÌâ¶à¶à£»
²ÎÕÕCSDN£¬Öйú·ç(Roy)һƪËÀËøÎÄÕ²¢¸Ä½øÁËÏ£»
/***********************************************************************************************************************
ÕûÀíÈË£ººÚľÑÂÉϵÄÎÏÅ£(lenolotus) ÈÕÆÚ:2009.04.28
************ ......
ÏÂÃæÌá³öÁË10ÌõºÍDB2 SQLÐÔÄÜÏà¹ØµÄ10ÌõÒòËØ£º
1¡¢ÌṩÊʵ±µÄͳ¼ÆÐÅÏ¢
¶ÔDB2ÓÅ»¯¹ÜÀíÆ÷£¨otimizer£©¶øÑÔ£¬ÈçºÎ¸ü¼ÓÓÐЧµÄÖ´ÐÐSQLÓï¾äÊÇÓÉ´æÔÚÓÚDB2 catalogÖеÄͳ¼ÆÐÅÏ¢¾ö¶¨µÄ£¬ÓÅ»¯Æ÷ÀûÓÃÕâЩÐÅÏ¢¾ö¶¨×îÓÅ»¯µÄ·¾¶¡£
Òò´Ë£¬ÎªÁ˱£³ÖϵͳÄܹ»×ö³ö×î¼ÑÑ¡Ôñ£¬ÐèÒª¾³£µÄÔËÐÐRunstatsÃüÁÀ´±£³Öϵͳͳ¼ÆÐÅÏ¢µÄ¼°Ê±ÓÐЧ¡£Ô ......