ÔÎÄ£ºhttp://www.cnblogs.com/hustcat/archive/2009/10/18/1585626.html
дÔÚÇ°Ã棺×îÔç½Ó´¥µÄMySQLÊÇÔÚÈýÄêÇ°£¬ÄÇʱºòMySQL»¹ÊÇ4.x°æ±¾£¬ºÜ¶à¹¦Äܶ¼²»Ö§³Ö£¬±ÈÈ磬´æ´¢¹ý³Ì£¬ÊÓͼ£¬´¥·¢Æ÷£¬¸ü±ð˵·Ö²¼Ê½ÊÂÎñµÈ¸´ÔÓÌØÐÔÁË¡£µ«´Ó5.0(2005Äê10ÔÂ)¿ªÊ¼£¬MySQL½¥½¥²½ÈëÆóÒµ¼¶Êý¾Ý¿âµÄÐÐÁÐÁË£»¸´ÖÆ¡¢¼¯Èº¡¢·ÖÇø¡¢·Ö²¼Ê½ÊÂÎñ£¬ÕâЩÆóÒµ¼¶µÄÌØÐÔ£¬Ê¹µÃÏÖÔÚµÄMySQL£¬ÍêÈ«¿ÉÒÔÓ¦ÓÃÓÚÆóÒµ¼¶Ó¦Óû·¾³(ºÜ¶à»¥ÁªÍø¹«Ë¾¶¼ÓÃÆä×÷ΪÊý¾Ý¿â·þÎñÆ÷£¬¾¡¹Ü½ÚÔ¼³É±¾ÊÇÒ»¸öÒòËØ£¬µ«ÊÇûÓÐÇ¿´ó¹¦ÄÜ×÷ºó¶Ü£¬ÔòÊDz»¿ÉÏëÏóµÄ)¡£ËäÈ»£¬MySQL»¹Óкܶ಻×㣬±ÈÈ磬¸´ÖÆ¡¢·ÖÇøµÄÖ§³Ö¶¼Ê®·ÖÓÐÏÞ¡¢²éѯÓÅ»¯ÈÔÐèÒª¸Ä½ø£¬µ«ÊÇMySQLÒѾÊÇÒ»¸ö×ã¹»ºÃµÄDBMSÁË£¬¸üºÎ¿öËüÊÇopensourceµÄ¡£Õâ¶Îʱ¼äûÓÐÊ£¬³öÓÚºÃÆ棬ÂÔ΢µÄÑо¿ÁËÒ»ÏÂMySQL£¬»ýÀÛÁËһЩ×ÊÁÏ£¬Óû×ܽá³öÀ´¡£ÕâЩ×ÊÁÏ´òËã·ÖΪÁ½²¿·Ö£¬Éϲ¿Ö÷ÒªÌÖÂÛMySQLµÄÓÅ»¯£¬ÆäÖÐÖ÷Òª²Î¿¼ÁË¡¶MySQL Manual¡·ºÍ¡¶High Performance MySQL¡·£¬Èç¹ûÓÐʱ¼ä£¬ÒÔºóÔÚϲ¿·ÖÎöÒ»ÏÂMySQLµÄÔ´Âë¡£Èç¹ûÄãÊÇMySQL¸ßÊÖ£¬Ï£ÍûÄã²»Áߴͽ̣»Èç¹ûÄãÊÇÐÂÊÖ£¬Ï£Íû¶ÔÄãÓÐÓá£
µÚÒ»Õ¡¢MySQL¼Ü¹¹Óë¸ÅÄî
1¡¢MySQLµÄÂß¼¼Ü¹¹
×îÉÏÃæ²»ÊÇMySQLÌØÓеģ¬ËùÓлùÓÚÍøÂçµÄC/SµÄÍøÂçÓ¦ÓóÌÐò¶¼Ó¦¸Ã°üÀ¨Á¬½Ó´¦Àí¡¢ÈÏÖ¤¡¢°²È«¹ÜÀíµÈ¡£
Öмä²ãÊÇMySQLµÄºËÐÄ£¬°üÀ¨²éѯ½âÎö¡¢·ÖÎö¡¢ÓÅ»¯ºÍ»º´æµÈ¡£Í¬Ê±Ëü»¹Ìṩ¿ç´æ´¢ÒýÇæµÄ¹¦ÄÜ£¬°üÀ¨´æ´¢¹ý³Ì¡¢´¥·¢Æ÷ºÍÊÓͼµÈ¡£
×îÏÂÃæÊÇ´æ´¢ÒýÇ棬Ëü¸ºÔð´æÈ¡Êý¾Ý¡£·þÎñÆ÷ͨ¹ýstorage engine API¿ÉÒԺ͸÷ÖÖ´æ´¢ÒýÇæ½øÐн»»¥¡£
1.1¡¢²éѯÓÅ»¯ºÍÖ´ÐÐ(Optimization and Execution)
MySQL½«Óû§µÄ²éѯÓï¾ä½øÐнâÎö£¬²¢´´½¨Ò»¸öÄÚ²¿µÄÊý¾Ý½á¹¹¡ª¡ª·ÖÎöÊ÷£¬È»ºó½øÐи÷ÖÖÓÅ»¯£¬ÀýÈçÖØд²éѯ¡¢Ñ¡Ôñ¶ÁÈ¡±íµÄ˳Ðò£¬ÒÔ¼°Ê¹ÓÃÄĸöË÷ÒýµÈ¡£²éѯÓÅ»¯Æ÷²»¹ØÐÄÒ»¸ö±íËùʹÓõĴ洢ÒýÇ棬µ«ÊÇ´æ´¢ÒýÇæ»áÓ°Ïì·þÎñÆ÷ÈçºÎÓÅ»¯²éѯ¡£ÓÅ»¯Æ÷ͨ¹ý´æ´¢ÒýÇæ»ñȡһЩ²ÎÊý¡¢Ä³¸ö²Ù×÷µÄÖ´Ðдú¼Û¡¢ÒÔ¼°Í³¼ÆÐÅÏ¢µÈ¡£ÔÚ½âÎö²éѯ֮ǰ£¬·þÎñÆ÷»áÏÈ·ÃÎʲéѯ»º´æ(query cache)¡ª¡ªËü´æ´¢SELECTÓï¾äÒÔ¼°ÏàÓ¦µÄ²éѯ½á¹û¼¯¡£Èç¹ûij¸ö²éѯ½á¹ûÒѾλÓÚ»º´æÖУ¬·þÎñÆ÷¾Í²»»áÔÙ¶Ô²éѯ½øÐнâÎö¡¢ÓÅ»¯¡¢ÒÔ¼°Ö´ÐС£Ëü½ö½ö½«»º´æÖеĽá¹û·µ»Ø¸øÓû§¼´¿É£¬Õ⽫´ó´óÌá¸ßϵͳµÄÐÔÄÜ¡£
1.2¡¢²¢·¢¿ØÖÆ
MySQLÌṩÁ½¸ö¼¶±ðµÄ²¢·¢¿ØÖÆ£º·þÎñÆ÷¼¶(the server level)ºÍ´æ´¢ÒýÇ漶(the storage engine level)¡£¼ÓËøÊÇʵÏÖ²¢·¢¿ØÖƵĻù±¾·½·¨£¬MySQLÖÐËøµÄÁ£¶È£º
(1) ±í¼¶Ëø£ºMySQL¶ÀÁ¢ÓÚ´æ´¢ÒýÇæÌṩ±íËø£¬ÀýÈ磬¶ÔÓÚALTER TABLEÓï¾ä£¬·þÎñÆ÷Ìṩ±íËø(table-level lock)¡£
(2) Ðм¶Ëø£ºInnoDBºÍFalcon´æ´¢ÒýÇæÌṩÐм¶Ëø£¬´ËÍ⣬BDBÖ§³ÖÒ³¼¶Ëø¡£InnoDBµÄ²¢·¢¿ØÖÆ»úÖÆ£¬Ï½ÚÏêϸÌÖÂÛ¡£
ÁíÍ⣬ֵµÃÒ»ÌáµÄÊÇ£¬MySQLµÄһЩ´æ´¢ÒýÇ棨ÈçInnoDB¡¢BDB£©³ýÁËʹÓ÷âËø»úÖÆÍ⣬»¹Í¬Ê±½áºÏMVCC»úÖÆ£¬¼´¶à°æ±¾Á½½×¶Î·âËøÐÒé(Multiversion two-phrase locking protocal)£¬À´ÊµÏÖÊÂÎñµÄ²¢·¢¿ØÖÆ£¬´Ó¶øʹµÃÖ»¶ÁÊÂÎñ²»ÓõȴýËø£¬Ìá¸ßÁËÊÂÎñµÄ²¢·¢ÐÔ¡£
×¢£º²¢·¢¿ØÖÆÊÇDBMSµÄºËÐļ¼ÊõÖ®Ò»(ʵ¼ÊÉÏ£¬¶ÔÓÚOSÒ²Ò»Ñù)£¬Ëü¶ÔϵͳÐÔÄÜÓÐ×ÅÖÁ¹ØÖØÒªµÄÓ°Ï죬ÒÔºóÔÙÏêϸÌÖÂÛ¡£
1.3¡¢ÊÂÎñ´¦Àí
MySQLÖУ¬InnoDBºÍBDB¶¼Ö§³ÖÊÂÎñ´¦Àí¡£ÕâÀïÖ÷ÒªÌÖÂÛInnoDBµÄÊÂÎñ´¦Àí(¹ØÓÚBDBµÄÊÂÎñ´¦Àí£¬Ò²Ê®·Ö¸´ÔÓ£¬ÒÔÇ°Ôø½ÏΪÏêϸ¿´¹ýÆäÔ´Â룬ÒÔºóÓлú»áÔÙÌÖÂÛ)¡£
1.3.1¡¢ÊÂÎñµÄACIDÌØÐÔ
ÊÂÎñÊÇÓÉÒ»×éSQLÓï¾ä×é³ÉµÄÂß¼´¦Àíµ¥Ôª£¬ÊÂÎñ¾ßÓÐÒÔÏÂ4¸öÊôÐÔ£¬Í¨³£¼ò³ÆΪÊÂÎñµÄACIDÊôÐÔ(Jim GrayÔÚ¡¶ÊÂÎñ´¦Àí£º¸ÅÄîÓë¼¼Êõ¡·ÖжÔÊÂÎñ½øÐÐÁËÏ꾡µÄÌÖÂÛ)¡£
(1)Ô×ÓÐÔ£¨Atomicity£©£ºÊÂÎñÊÇÒ»¸öÔ×Ó²Ù×÷µ¥Ôª£¬Æä¶ÔÊý¾ÝµÄÐ޸ģ¬ÒªÃ´È«¶¼Ö´ÐУ¬ÒªÃ´È«¶¼²»Ö´ÐС£
(2)Ò»ÖÂÐÔ£¨Consistent£©£ºÔÚÊÂÎñ¿ªÊ¼ºÍÍê³Éʱ£¬Êý¾Ý¶¼±ØÐë±£³ÖÒ»ÖÂ״̬¡£ÕâÒâζ×ÅËùÓÐÏà¹ØµÄÊý¾Ý¹æÔò¶¼±ØÐëÓ¦ÓÃÓÚÊÂÎñµÄÐ޸ģ¬ÒÔ±£³ÖÊý¾ÝµÄÍêÕûÐÔ£»ÊÂÎñ½áÊøʱ£¬ËùÓеÄÄÚ²¿Êý¾Ý½á¹¹£¨ÈçBÊ÷Ë÷Òý»òË«ÏòÁ´±í£©Ò²¶¼±ØÐëÊÇÕýÈ·µÄ¡£
(3)¸ôÀëÐÔ£¨Isolation£©£ºÊý¾Ý¿âϵͳÌṩһ¶¨µÄ¸ôÀë»úÖÆ£¬±£Ö¤ÊÂÎñÔÚ²»ÊÜÍⲿ²¢·¢²Ù×÷Ó°ÏìµÄ¡°¶ÀÁ¢¡±»·¾³Ö´ÐС£ÕâÒâζ×ÅÊÂÎñ´¦Àí¹ý³ÌÖеÄÖмä״̬¶ÔÍⲿÊDz»¿É¼ûµÄ£¬·´Ö®ÒàÈ»¡£
(4)³Ö¾ÃÐÔ£¨Durable£©£ºÊÂÎñÍê³ÉÖ®ºó£¬Ëü¶ÔÓÚÊý¾ÝµÄÐÞ¸ÄÊÇÓÀ¾ÃÐԵģ¬¼´Ê¹³öÏÖϵͳ¹ÊÕÏÒ²Äܹ»±£³Ö¡£
1.3.2¡¢ÊÂÎñ´¦Àí´øÀ´µÄÏà¹ØÎÊÌâ
ÓÉÓÚÊÂÎñµÄ²¢·¢Ö´ÐУ¬´øÀ´ÒÔÏÂһЩÖøÃûµÄÎÊÌ⣺
(1)¸üжªÊ§£¨Lost Update£©£ºµ±Á½¸ö»ò¶à¸öÊÂÎñÑ¡ÔñͬһÐУ¬È»ºó»ùÓÚ×î³õÑ¡¶¨µÄÖµ¸üиÃÐÐʱ£¬ÓÉÓÚÿ¸öÊÂÎñ¶¼²»ÖªµÀÆäËûÊÂÎñµÄ´æÔÚ£¬¾Í»á·¢Éú¶ªÊ§¸üÐÂÎÊÌ⣣×îºóµÄ¸üи²¸ÇÁËÓÉÆäËûÊÂÎñËù×öµÄ¸üС£
(2)Ôà¶Á£¨Dirty Reads£©£ºÒ»¸öÊÂÎñÕýÔÚ¶ÔÒ»Ìõ¼Ç¼×öÐ޸ģ¬ÔÚÕâ¸öÊÂÎñÍê³É²¢Ìύǰ£¬ÕâÌõ¼Ç¼µÄÊý¾Ý¾Í´¦ÓÚ²»Ò»ÖÂ״̬£»Õâʱ£¬ÁíÒ»¸öÊÂÎñÒ²À´¶ÁȡͬһÌõ¼Ç¼£¬Èç¹û²»¼Ó¿ØÖÆ£¬µÚ¶þ¸öÊÂÎñ¶ÁÈ¡ÁËÕâЩ¡°ÔࡱÊý¾Ý£¬²¢¾Ý´Ë×ö½øÒ»²½µÄ´¦Àí£¬¾Í»á²úÉúδÌá½»µÄÊý¾ÝÒÀÀµ¹Øϵ¡£ÕâÖÖÏÖÏó±»ÐÎÏóµØ½Ð×ö"Ôà¶Á"¡£
(3)²»¿ÉÖظ´¶Á£¨Non-Repeatable Reads£©£ºÒ»¸öÊÂÎñÔÚ¶ÁȡijЩÊý¾ÝºóµÄij¸öʱ¼ä£¬ÔٴζÁÈ¡ÒÔÇ°¶Á¹ýµÄÊý¾Ý£¬È´·¢ÏÖÆä¶Á³öµÄÊý¾ÝÒѾ·¢ÉúÁ˸ı䡢»òijЩ¼Ç¼ÒѾ±»É¾³ýÁË£¡ÕâÖÖÏÖÏó¾Í½Ð×ö¡°²»¿ÉÖظ´¶Á¡±¡£
(4)»Ã¶Á£¨Phantom Reads£©£ºÒ»¸öÊÂÎñ°´ÏàͬµÄ²éѯÌõ¼þÖØжÁÈ¡ÒÔÇ°¼ìË÷¹ýµÄÊý¾Ý£¬È´·¢ÏÖÆäËûÊÂÎñ²åÈëÁËÂú×ãÆä²éѯÌõ¼þµÄÐÂÊý¾Ý£¬ÕâÖÖÏÖÏó¾Í³ÆΪ¡°»Ã¶Á¡±¡£
1.3.3¡¢ÊÂÎñµÄ¸ôÀëÐÔ
SQL2±ê×¼¶¨ÒåÁËËĸö¸ôÀ뼶±ð¡£¶¨ÒåÓï¾äÈçÏ£º
SET TRANSACTION ISOLATION LEVEL
[READ UNCOMMITTED |
READ COMMITTED |
REPEATABLE READ |
SERIALIZABLE ]
ÕâÓëJim GrayËùÌá³öµÄ¸ôÀ뼶±ðÓеã²îÒì¡£ÆäÖÐREAD UNCOMMITTED¼´JimµÄ10£¨ä¯ÀÀ£©£»READ COMMITTED¼´20£¬ÓαêÎȶ¨ÐÔ£»REPEATABLE READΪ2.99990¸ôÀë(ûÓлÃÏñ±£»¤)£»SERIALIZABLE¸ôÀ뼶±ðΪ30£¬ÍêÈ«¸ôÀë¡£SQL2±ê׼ĬÈÏΪÍêÈ«¸ôÀë(30)¡£¸÷¸ö¼¶±ð´æÔÚÎÊÌâÈçÏ£º
¸ôÀ뼶 |
Ôà¶Á |
²»¿ÉÖظ´¶Á |
»ÃÏó¶Á |
¶ÁδÌá½» (Read uncommitted) |
¿ÉÄÜ |
¿ÉÄÜ |
¿ÉÄÜ |
¶ÁÌá½» (Read committed) |
²»¿ÉÄÜ |
¿ÉÄÜ |
¿ÉÄÜ |
¿ÉÖظ´¶Á (Repeatable read) |
²»¿ÉÄÜ |
²»¿ÉÄÜ |
¿ÉÄÜ |
¿É´®Ðл¯ (Serializable) |
²»¿ÉÄÜ |
²»¿ÉÄÜ |
²»¿ÉÄÜ |
¸÷¸ö¾ßÌåÊý¾Ý¿â²¢²»Ò»¶¨ÍêȫʵÏÖÁËÉÏÊö4¸ö¸ôÀ뼶±ð£¬ÀýÈ磬OracleÖ»ÌṩREAD COMMITTEDºÍSerializableÁ½¸ö±ê×¼¸ôÀ뼶±ð£¬ÁíÍ⻹Ìṩ×Ô¼º¶¨ÒåµÄRead only¸ôÀ뼶±ð£»SQL Server³ýÖ§³ÖÉÏÊöISO/ANSI SQL92¶¨ÒåµÄ4¸ö¸ôÀ뼶±ðÍ⣬»¹Ö§³ÖÒ»¸ö½Ð×ö¡°¿ìÕÕ¡±µÄ¸ôÀ뼶±ð£¬µ«ÑϸñÀ´ËµËüÊÇÒ»¸öÓÃMVCCʵÏÖµÄSerializable¸ôÀ뼶±ð¡£MySQL Ö§³ÖÈ«²¿4¸ö¸ôÀ뼶±ð£¬ÆäĬÈϼ¶±ðΪRepeatable read£¬µ«ÔÚ¾ßÌåʵÏÖʱ£¬ÓÐһЩÌص㣬±ÈÈçÔÚһЩ¸ôÀ뼶±ðÏÂÊDzÉÓÃMVCCÒ»ÖÂÐÔ¶Á¡£¹ú²úÊý¾Ý¿âDMÒ²Ö§³ÖËùÓ춱ð£¬ÆäĬÈϼ¶±ðΪREAD COMMITTED¡£
1.3.4¡¢InnoDBµÄËøÄ£ÐÍ
InnoDBµÄÐм¶ËøÓÐÁ½ÖÖÀàÐÍ£º
(1)¹²ÏíËø(shared lock£¬S)£ºÔÊÐíÒ»¸öÊÂÎñÈ¥¶ÁÒ»ÐУ¬×èÖ¹ÆäËûÊÂÎñ»ñµÃÏàͬÊý¾Ý¼¯µÄÅÅËûËø¡£
(2)ÅÅËüËø(exclusive lock£¬X)£ºÔÊÐí»ñµÃÅÅËüËøµÄÊÂÎñ¸üÐÂÊý¾Ý£¬×èÖ¹ÆäËûÊÂÎñÈ¡µÃÏàͬÊý¾Ý¼¯µÄ¹²Ïí¶ÁËøºÍÅÅËûдËø¡£
´ËÍ⣬InnoDBÖ§³Ö¶àÁ£¶È¼ÓËø(multiple granularity locking)£¬´Ó¶øÔÊÐí¶Ô¼Ç¼ºÍ±íͬʱ¼ÓËø¡£Îª´Ë£¬InnoDBÒýÈëÒâÏòËø(intention locks)£¬ÒâÏòËøÊÇÕë¶Ô±íµÄ£º
(1)ÒâÏò¹²ÏíËø£¨IS£©£ºÊÂÎñ´òËã¸øÊý¾ÝÐмÓÐй²ÏíËø£¬ÊÂÎñÔÚ¸øÒ»¸öÊý¾ÝÐмӹ²ÏíËøÇ°±ØÐëÏÈÈ¡µÃ¸Ã±íµÄISËø¡£
(2)ÒâÏòÅÅËûËø£¨IX£©£ºÊÂÎñ´òËã¸øÊý¾ÝÐмÓÐÐÅÅËûËø£¬ÊÂÎñÔÚ¸øÒ»¸öÊý¾ÝÐмÓÅÅËûËøÇ°±ØÐëÏÈÈ¡µÃ¸Ã±íµÄIXËø¡£
ÀýÈ磬SELECT ... LOCK IN SHARE MODE¼ÓISËø£¬SELECT ... FOR UPDATE¼ÓIXËø£¬ÒâÏòËøµÄ¹æÔòÈçÏ£º
(1)ÊÂÎñÔÚ¶Ô±íTÖеļǼ»ñÈ¡SËøÇ°£¬ÏÈÒª»ñÈ¡±íTµÄISËø»òÕ߸üÇ¿µÄËø£»
(2)ÊÂÎñÔÚ»ñÈ¡±íTÖмǼµÄXËøÇ°£¬ÏÈÒª»ñÈ¡±íTµÄIXËø¡£
InnoDBµÄËøÏàÈÝÐÔ¾ØÕó£º
Èç¹ûÒ»¸öÊÂÎñÇëÇóµÄËøģʽÓ뵱ǰµÄËø¼æÈÝ£¬InnoDB¾Í½«ÇëÇóµÄËøÊÚÓè¸ÃÊÂÎñ£»·´Ö®£¬Èç¹ûÁ½Õß²»¼æÈÝ£¬¸ÃÊÂÎñ¾ÍÒªµÈ´ýËøÊÍ·Å¡£ÒâÏòËøÖ»»á×èÈûÆäËüÊÂÎñ¶Ô±íµÄÇëÇó£¬ÀýÈ磬LOCK TABLES ¡WRITE£¬ÒâÏòËøµÄÖ÷ҪĿµÄÊDZíÃ÷¸ÃÊÂÎñ½«Òª»òÕßÕýÔÚ¶Ô±íÖеļǼ¼ÓËø¡£Ê¹Ó÷âËø»úÖÆÀ´½øÐв¢·¢¿ØÖÆ£¬Ò»¸ö±È½ÏÖØÒªµÄÎÊÌâ¾ÍÊÇËÀËø¡£
À´¿´Ò»¸öËÀËøµÄÀý×Ó£º
Àý1-1
Session 1 |
Session 2 |
mysql> CREATE TABLE t (i INT) ENGINE = InnoDB; Query OK, 0 rows affected (0.22 sec)
mysql> INSERT INTO t (i) VALUES(1); Query OK, 1 row affected (0.08 sec)
mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM t WHERE i = 1 LOCK IN SHARE MODE; +------+ | i | +------+ | 1 | +------+ 1 row in set (0.01 sec)
mysql> |
|
|
mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec)
mysql> DELETE FROM t WHERE i = 1; µÈ´ý¡ |
mysql> DELETE FROM t WHERE i = 1; µÈ´ý¡ |
|
|
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction |
Query OK, 1 row affected (0.00 sec) |
|
1.3.5¡¢Ò»ÖÂÐÔ·Ç×èÈû¶Á
Ò»ÖÂÐÔ¶ÁÊÇMySQLµÄÖØÒªÌصãÖ®Ò»£¬InnoDBͨ¹ýMVCC»úÖƱíʾÊý¾Ý¿âijһʱ¿ÌµÄ²éѯ¿ìÕÕ£¬²éѯ¿ÉÒÔ¿´¸Ãʱ¿Ì֮ǰÌá½»µÄÊÂÎñËù×öµÄ¸Ä±ä£¬µ«ÊDz»ÄÜ¿´µ½¸Ãʱ¿ÌÖ®ºó»òÕßδÌá½»ÊÂÎñËù×öµÄ¸Ä±ä¡£µ«ÊÇ£¬²éѯ¿ÉÒÔ¿´µ½Í¬Ò»ÊÂÎñÖÐ֮ǰÓï¾äËù×öµÄ¸Ä±ä£¬ÀýÈ磺
Àý1-2
Session 1 |
Session 2 |
mysql> select * from t; Empty set (0.00 sec)
mysql> INSERT INTO t (i) VALUES(1); Query OK, 1 row affected (0.00 sec)
mysql> select * from t; +------+ | i | +------+ | 1 | +------+ 1 row in set (0.00 sec)
mysql> set autocommit = 0; Query OK, 0 rows affected (0.01 sec)
mysql> update t set i=3; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t; +------+ | i | +------+ | 3 | +------+ 1 row in set (0.00 sec) |
|
|
mysql> set autocommit = 0; Query OK, 0 rows affected (0.00 sec)
mysql> select * from t; +------+ | i | +------+ | 1 | +------+ 1 row in set (0.00 sec) |
mysql> commit; Query OK, 0 rows affected (0.06 sec) |
|
|
mysql> select * from t; +------+ | i | +------+ | 1 | +------+ 1 row in set (0.00 sec) |
|
mysql> commit; Query OK, 0 rows affected (0.00 sec)
mysql> select * from t; +------+ | i | +------+ | 3 | +------+ 1 row in set (0.00 sec) |
Èç¹ûÊÂÎñµÄ¸ôÀ뼶±ðΪREPEATABLE READ£¨Ä¬ÈÏ£©£¬Í¬Ò»¸öÊÂÎñÖеÄËùÓÐÒ»ÖÂÐÔ¶Á¶¼ÊǶÁµÄÊÂÎñµÄµÚÒ»´Î¶Á²Ù×÷´´½¨µÄ¿ìÕÕ¡£Äã¿ÉÒÔÌá½»µ±Ç°ÊÂÎñ£¬È»ºóÔÚеIJéѯÖм´¿É¿´µ½×îеĿìÕÕ£¬ÈçÉÏËùʾ¡£
Èç¹ûÊÂÎñµÄ¸ôÀ뼶±ðΪREAD COMMITTED£¬Ò»ÖÂÐÔ¶ÁÖ»ÊǶÔÊÂÎñÄÚ²¿µÄ¶Á²Ù×÷ºÍËü×Ô¼ºµÄ¿ìÕÕ¶øÑԵģ¬½á¹ûÈçÏ£º
Àý1-3
Session 1 |
Session 2 |
mysql> SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; Query OK, 0 rows affected (0.01 sec)
mysql> set autocommit = 0; Query OK, 0 rows affected (0.00 sec)
mysql> select * from t; +------+ | i | +------+ | 3 | +------+ 1 row in set (0.00 sec) |
|
|
mysql> SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; Query OK, 0 rows affected (0.01 sec)
mysql> set autocommit = 0; Query OK, 0 rows affected (0.00 sec)
mysql> select * from t; +------+ | i | +------+ | 3 | +------+ 1 row in set (0.00 sec) |
mysql> update t set i=5; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 |
|
|
mysql> select * from t; +------+ | i | +------+ | 3 | +------+ 1 row in set (0.00 sec) |
mysql> commit; Query OK, 0 rows affected (0.06 sec) |
|
|
mysql> select * from t; +------+ | i | +------+ | 5 | +------+ 1 row in set (0.00 sec) |
×¢Ò⣬session 2·¢ÉúÁ˲»¿ÉÖظ´¶Á¡£
µ±InnoDBÔÚREAD COMMITTED ºÍREPEATABLE READ¸ôÀ뼶±ðÏ´¦ÀíSELECTÓï¾äʱ£¬Ò»ÖÂÐÔ¶ÁÊÇĬÈϵÄģʽ¡£Ò»ÖÂÐÔ¶Á²»»á¶Ô±í¼ÓÈκÎËø£¬ËùÒÔ£¬ÆäËüÁ¬½Ó¿ÉÒÔͬʱ¸Ä±ä±í¡£
¼ÙÉèÊÂÎñ´¦ÓÚREPEATABLE READ¼¶±ð£¬µ±ÄãÕýÔÚ½øÐÐÒ»ÖÂÐÔ¶Áʱ£¬InnoDB¸ù¾Ý²éѯ¿´µ½µÄÊý¾Ý¸øÄãÒ»¸öʱ¼äµã¡£Èç¹ûÆäËüµÄÊÂÎñÔÚ¸Ãʱ¼äµãÖ®ºóɾ³ýÒ»ÐУ¬ÇÒÌá½»ÊÂÎñ£¬Äã²»»á¿´µ½ÐÐÒѾ±»É¾³ý£¬²åÈëºÍ¸üвÙ×÷Ò»Ñù¡£µ«ÊÇ£¬InnoDBÓëÆäËüDBMSµÄ²»Í¬ÊÇ£¬ÔÚREPEATABLE READ¸ôÀ뼶±ðϲ¢²»»áÔì³É»ÃÏñ¡£
Ò»ÖÂÐÔ¶Á²»ÓëDROP TABLE »òÕß ALTER TABLEÒ»Æð¹¤×÷¡£
ÔÚnodb_locks_unsafe_for_binlog±äÁ¿±»ÉèÖûòÕßÊÂÎñµÄ¸ôÀ뼶±ð²»ÊÇSERIALIZABLEµÄÇé¿öÏ£¬InnoDB¶ÔÓÚûÓÐÖ¸¶¨FOR UPDATE »ò LOCK IN SHARE MODEµÄINSERT INTO ... SELECT, UPDATE ... (SELECT), ºÍCREATE TABLE ... SELECTÓï¾äʹÓÃÒ»ÖÂÐÔ¶Á£¬ÔÚÕâÖÖÇé¿öÏ£¬²éѯÓï¾ä²»»á¶Ô±íÖеÄÔª×é¼ÓËø¡£·ñÔò£¬InnoDB½«Ê¹ÓÃËø¡£
1.3.6¡¢SELECT ... FOR UPDATEºÍSELECT ... LOCK IN SHARE MODEµÄ¼ÓËø¶Á(locking read)
ÔÚһЩ³¡ºÏ£¬Ò»ÖÂÐÔ¶Á²¢²»ÊǺܷ½±ã£¬´Ëʱ£¬¿ÉÒÔÓüÓËø¶Á¡£InnoDBÖ§³ÖÁ½ÖÖ¼ÓËø¶Á£º
(1) SELECT ... LOCK IN SHARE MODE£º¶Ô¶ÁÈ¡µÄÔª×é¼ÓSËø¡£
(2) SELECT ... FOR UPDATE£ºÔÚɨÃèË÷Òý¼Ç¼µÄ¹ý³ÌÖУ¬»á×èÈûÆäËüÁ¬½ÓµÄSELECT ...LOCK IN SHARE MODEºÍÒ»¶¨ÊÂÎñ¸ôÀ뼶±ðϵĶÁ²Ù×÷¡£
InnoDBʹÓÃÁ½½×¶Î·âËøÐÒ飬ÊÂÎñÖ±µ½Ìá½»»ò»Ø¹öʱ²Å»áÊÍ·ÅËùÓеÄËø£¬ÕⶼÊÇϵͳ×Ô¶¯Ö´Ðеġ£´ËÍ⣬MySQLÖ§³ÖLOCK TABLESºÍUNLOCK TABLES£¬µ«ÕâЩ¶¼ÊÇÔÚ·þÎñÆ÷²ãʵÏֵģ¬¶ø²»ÊÇÔÚ´æ´¢ÒýÇæ¡£ËüÃÇÓÐÓô¦£¬µ«ÊDz»ÄÜÈ¡´ú´æ´¢ÒýÇæÍê³ÉÊÂÎñ´¦Àí£¬Èç¹ûÄãÐèÒªÊÂÎñ¹¦ÄÜ£¬ÇëʹÓÃÊÂÎñÐÍ´æ´¢ÒýÇæ¡£
À´¿¼ÂÇlocking readµÄÓ¦Ó㬼ÙÉèÄãÒªÔÚ±íchild²åÈëÒ»¸öеÄÔª×飬²¢±£Ö¤childÖеļǼÔÚ±íparentÓÐÒ»Ìõ¸¸¼Ç¼¡£Èç¹ûÄãÓÃÒ»ÖÂÐÔ¶ÁÀ´¶Áparent±í£¬È·Êµ¿ÉÒÔ½«Òª²åÈëµÄchild rowµÄparent row£¬µ«ÊÇ¿ÉÒÔ°²È«µÄ²åÈëÂ𣿲»£¬ÒòΪÔÚÄã¶Áparent±íʱ£¬ÆäËüÁ¬½Ó¿ÉÄÜÒѾɾ³ý¸Ã¼Ç¼¡££¨Ò»ÖÂÐÔ¶ÁÊÇÕë¶ÔÊÂÎñÄÚ¶øÑԵģ¬¶ÔÓÚÊý¾Ý¿âµÄ״̬£¬ËüÓ¦¸Ã½Ð×ö¡°²»Ò»ÖÂÐÔ¶Á¡±£©
´Ëʱ£¬¾Í¿ÉÒÔʹÓÃSELECT LOCK IN SHARE MODE£¬Ëü»á¶Ô¶ÁÈ¡µÄÔª×é¼ÓSËø£¬´Ó¶ø·ÀÖ¹ÆäËüÁ¬½Óɾ³ý»ò¸üÐÂÔª×é¡£ÁíÍ⣬Èç¹ûÄãÏëÔÚ²éѯµÄͬʱ£¬½øÐиüвÙ×÷£¬¿ÉÒÔʹÓÃSELECT ... FOR UPDATE£¬Ëü¶ÁÈ¡×îеÄÊý¾Ý£¬È»ºó¶Ô¶Áµ½µÄÔª×é¼ÓXËø¡£´Ëʱ£¬Ê¹ÓÃSELECT ... LOCK IN SHARE MODE²»ÊÇÒ»¸öºÃÖ÷Ò⣬ÒòΪ´ËʱÈç¹ûÓÐÁ½¸öÊÂÎñ½øÐÐÕâÑùµÄ²Ù×÷£¬¾Í»áÔì³ÉËÀËø¡£
×¢£ºSELECT ... FOR UPDATE½öÔÚ×Ô¶¯Ìá½»¹Ø±Õ(¼´ÊÖ¶¯Ìá½»)ʱ²Å»á¶ÔÔª×é¼ÓËø£¬¶øÔÚ×Ô¶¯Ìύʱ£¬·ûºÏÌõ¼þµÄÔª×é²»»á±»¼ÓËø¡£
1.3.7¡¢¼Ç¼Ëø(record lok)¡¢¼ä϶Ëø(gap lock)ºÍºóÂëËø(next-key lock)
InnoDBÓÐÒÔϼ¸ÖÖÐм¶Ëø£º
(1)¼Ç¼Ëø£º¶ÔË÷Òý¼Ç¼(index records)¼ÓËø£¬InnoDBÐм¶ËøÊÇͨ¹ý¸øË÷ÒýµÄË÷ÒýÏî¼ÓËøÀ´ÊµÏֵģ¬¶ø²»ÊǶԼǼʵÀý±¾Éí¼ÓËø¡£Èç¹û±íûÓж¨ÒåË÷Òý£¬InnoDB´´½¨Ò»¸öÒþ²ØµÄ¾Û´ØË÷Òý£¬È»ºóÓÃËüÀ´ÊµÏּǼ¼ÓËø£¨¹ØÓÚË÷ÒýÓë¼ÓËøÖ®¼äµÄ¹ØϵµÄÏêϸ½éÉÜÇë¿´ÏÂÒ»Õ£©¡£
(2)¼ä϶Ëø£º¶ÔË÷Òý¼Ç¼֮¼äµÄÇø¼ä£¬»òÕßµÚÒ»¸öË÷Òý¼Ç¼֮ǰµÄÇø¼äºÍ×îºóÒ»¸öË÷ÒýÖ®ºóµÄÇø¼ä¼ÓËø¡£
(3)ºóÂëËø£º¶ÔË÷Òý¼Ç¼¼Ó¼Ç¼Ëø£¬ÇÒ¶ÔË÷Òý¼Ç¼֮ǰµÄÇø¼ä¼ÓËø¡£
ĬÈÏÇé¿öÏ£¬InnoDBµÄÊÂÎñ¹¤×÷ÔÚREPEATABLE READµÄ¸ôÀ뼶±ð£¬¶øÇÒϵͳ±äÁ¿innodb_locks_unsafe_for_binlogΪ¹Ø±Õ״̬¡£´Ëʱ£¬InnoDBʹÓÃnext-keyËø½øÐвéÕÒºÍË÷ÒýɨÃ裬´Ó¶ø´ïµ½·ÀÖ¹¡°»ÃÏñ¡±µÄÄ¿µÄ¡£
Next-keyËøÊǼǼËøºÍ¼ä϶µÄ½áºÏÌå¡£µ±InnoDB²éÕÒ»òɨÃè±íµÄË÷Òýʱ£¬¶ÔËüÓöµ½µÄË÷Òý¼Ç¼¼ÓSËø»òÕßXËø£¬ËùÒÔ£¬Ðм¶Ëø(row-level lock)ʵ¼ÊÉϾÍÊÇË÷Òý¼Ç¼Ëø(index-record lock)£»´ËÍ⣬Ëü»¹¶ÔË÷Òý¼Ç¼֮ǰµÄÇø¼ä¼ÓËø¡£Ò²¾ÍÊÇ˵£¬next-keyËøÊÇË÷Òý¼Ç¼Ëø£¬Íâ¼ÓË÷Òý¼Ç¼֮ǰµÄÇø¼äµÄ¼ä϶Ëø¡£Èç¹ûÒ»¸öÁ¬½Ó¶ÔË÷ÒýÖеļǼR³ÖÓÐS»òXËø£¬ÆäËüµÄÁ¬½Ó²»ÄÜ°´ÕÕË÷ÒýµÄ˳ÐòÔÚR֮ǰµÄÇø¼ä²åÈëÒ»¸öË÷Òý¼Ç¼¡£
¼ÙÉèË÷Òý°üº¬ÒÔÏÂÖµ£º10, 11,13ºÍ20£¬ÔòË÷ÒýµÄnext-keyËø»á¸²¸ÇÒÔÏÂÇø¼ä(¡°(¡±±íʾ²»°üº¬£¬¡°[¡±±íʾ°üº¬)£º
(negative infinity, 10]
(10, 11]
(11, 13]
(13, 20]
(20, positive infinity)
¶ÔÓÚ×îºóÒ»¸öÇø¼ä£¬next-keyËø½«ËøסË÷Òý×î´óÖµÒÔÉϵÄÇø¼ä£¬ÉϽçÐé¼Ç¼(¡°supremum¡± pseudo-record)µÄÖµ±ÈË÷ÒýÖеÄÈκÎÖµ¶¼´ó£¬Æäʵ£¬ÉϽ粻ÊÇÒ»¸öÕæʵµÄË÷Òý¼Ç¼£¬ËùÒÔ£¬next-lock½«¶ÔË÷ÒýµÄ×î´óÖµÖ®ºóµÄÇø¼ä¼ÓËø¡£
¼ä϶Ëø¶Ô²éѯΨһË÷ÒýÖеÄΨһֵÊÇûÓбØÒªµÄ£¬ÀýÈ磬idÁÐÓÐΨһË÷Òý£¬ÔòÏÂÃæµÄ²éѯ½ö¶Ôid=100µÄÔª×é¼ÓË÷Òý¼Ç¼Ëø(index-record lock)£¬¶ø²»¹ÜÆäËüÁ¬½ÓÊÇ·ñÔÚ֮ǰµÄÇø¼ä²åÈëÔª×é¡£
SELECT * FROM child WHERE id = 100;
Èç¹ûidûÓÐË÷Òý£¬»òÕß·ÇΨһË÷Òý£¬ÔòÓï¾ä»áËøס֮ǰµÄ¿Õ¼ä¡£
Àý1-4
Session 1 |
Session 2 |
mysql> create unique index i_index on t(i); Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> select * from t; +------+ | i | +------+ | 4 | | 10 | +------+ 2 rows in set (0.00 sec) |
|
mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec) mysql> select i from t where i =10 lock in share mode; +------+ | i | +------+ | 10 | +------+ 1 row in set (0.00 sec) |
|
mysql> insert into t(i) values(9); Query OK, 1 row affected (0.03 sec) |
|
mysql> select * from t; +------+ | i | +------+ | 4 | | 9 | | 10 | +------+ 3 rows in set (0.00 sec) |
ÉÏÀýÖУ¬²úÉúÁË»ÃÏñÎÊÌâ¡£Èç¹û½«Î¨Ò»²éѯ±ä³É·¶Î§²éѯ£¬½á¹ûÈçÏÂ(½ÓÉÏÀýµÄË÷Òý)£º
Àý1-5
Session 1 |
Session 2 |
mysql> select * from t; +------+ | i | +------+ | 4 | | 9 | | 10 | +------+ 3 rows in set (0.00 sec) |
|
|
mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec) mysql> select i from t where i>4 lock in share mode; +------+ | i | +------+ | 9 | | 10 | +------+ 2 rows in set (0.00 sec) |
mysql> insert into t(i) values(1); ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> insert into t(i) values(8); ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction |
|
¿ÉÒÔ¿´µ½£¬session 2 µÄnext-keyʹµÃÔÚi=4֮ǰµÄÇø¼äºÍÖ®ºóµÄ²åÈ붼±»×èÈû¡£
ÁíÍ⣬Èç¹ûɾ³ýË÷Òýi_index£¬Ôò½á¹ûÈçÏ£º
Àý1-6
Session 1 |
Session 2 |
mysql> drop index i_index on t; Query OK, 3 rows affected (0.25 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from t; +------+ | i | +------+ | 4 | | 10 | | 9 | +------+ 3 rows in set (0.00 sec) |
|
|
mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec)
mysql> select i from t lock in share mode; +------+ | i | +------+ | 4 | | 10 | | 9 | +------+ 3 rows in set (0.00 sec) |
mysql> insert into t(i) values(8); µÈ´ý¡£¡£¡£ |
|
ÁíÍ⣬Õë¶Ô²åÈë(INSERT)²Ù×÷£¬Ö»Òª¶à¸öÊÂÎñ²»»áÔÚͬһË÷ÒýÇø¼äµÄͬһ¸öλÖòåÈë¼Ç¼£¬ËüÃǾͲ»Óû¥ÏàµÈ´ý£¬ÕâÖÖÇé¿ö¿ÉÒÔ³ÆΪ²åÈëÒâÏò¼ä϶Ëø(insertion intention gap lock)¡£ÀýÈ磬Ë÷Òý¼Ç¼µÄֵΪ4ºÍ7£¬Á½¸ö¶ÀÁ¢µÄÊÂÎñ·Ö±ð²åÈë5ºÍ6£¬½ö¹ÜËüÃǶ¼³ÖÓÐ4¡ª7Ö®¼äµÄ¼ä϶Ëø£¬µ«ÊÇËüÃDz»»áÏ໥×èÈû¡£Õâ¿ÉÒÔÌá¸ßÊÂÎñµÄ²¢·¢ÐÔ¡£
Àý1-7
Session 1 |
Session 2 |
mysql> select * from t; +------+ | i | +------+ | 4 | | 10 | | 9 | | 8 | +------+ 4 rows in set (0.00 sec)
mysql> create unique index i_index on t(i); Query OK, 4 rows affected (0.34 sec) Records: 4 Duplicates: 0 Warnings: 0
mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec) |
|
|
mysql> set autocommit=0; Query OK, 0 rows affected (0.00 sec) |
mysql> insert into t(i) values(5); Query OK, 1 row affected (0.00 sec) |
|
|
mysql> insert into t(i) values(5); ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> insert into t(i) values(6); Query OK, 1 row affected (0.00 sec) |
¼ä϶ËøÊÇ¿ÉÒÔÏÔʾ¹Ø±ÕµÄ£¬Èç¹ûÄ㽫ÊÂÎñµÄ¸ôÀ뼶±ðÉèΪREAD COMMITTED£¬»òÕß´ò¿ªinnodb_locks_unsafe_for_binlogϵͳ±äÁ¿£¬¼ä϶Ëø¾Í»á¹Ø±Õ¡£ÔÚÕâÖÖÇé¿öÏ£¬²éÕÒ»òɨÃèË÷Òý½ö»á½øÐÐÍâ¼üÔ¼Êø¼ì²éºÍÖظ´¼üÖµ¼ì²é¡£
´ËÍ⣬READ COMMITTED¸ôÀ뼶±ðºÍ¹Ø±Õnodb_locks_unsafe_for_binlog»¹ÓÐÁíÍâÒ»¸ö¸º×÷ÓãºMySQL»áÊͷŵô²»Æ¥ÅäWhereÌõ¼þµÄ¼Ç¼Ëø¡£ÀýÈ磬¶ÔÓÚUPDATEÓï¾ä£¬InnoDBÖ»ÄܽøÐС°°ëÒ»ÖÂÐÔ(semi_consistent)¶Á¡±£¬ËùÒÔ£¬Ëü»á·µ»Ø×îÐÂÌá½»ÊÂÎñËù×ö¸Ä±ä£¬´Ó¶ø²úÉú²»¿ÉÖظ´¶ÁºÍ»ÃÏñÎÊÌâ¡£
1.3.8¡¢Ê¹ÓÃnext-key lock·ÀÖ¹»ÃÏñÎÊÌâ
Àý1-4չʾÁËÒ»¸ö»ÃÏñÎÊÌ⡣ʹÓÃnext-keyËøµÄselectÓï¾ä¿ÉÒÔ½â¾ö»ÃÏñÎÊÌ⣬µ«Àý1-4µÄÖ®ËùÒÔ»á²úÉú×ÜÊÇÔÚÓÚΨһË÷Òý£¬Ê¹µÃselectÓï¾äûÓÐʹÓÃgap lock£¬¶øֻʹÓÃÁËindex-record lock¡£
1.4¡¢´æ´¢ÒýÇæ
²å¼þʽ´æ´¢ÒýÇæÊÇMySQL×îÖØÒªÌØÐÔÖ®Ò»£¬Ò²ÊÇ×ͬÓÚÆäËüDBMSµÄµØ·½¡£MySQLÖ§³ÖºÜ¶à´æ´¢ÒýÇ棬ÒÔÊÊÓÃÓÚ²»Í¬µÄÓ¦ÓÃÐèÇ󣬳£ÓõİüÀ¨MyISAM¡¢InnoDB¡¢BDB¡¢MEMORY¡¢MERGE¡¢NDB ClusterµÈ¡£ÆäÖУ¬BDBºÍNDB ClusterÌṩÊÂÎñÖ§³Ö¡£
MySQLĬÈϵĴ洢ÒýÇæΪMyISAM£¬µ±È»£¬´´½¨±íµÄʱºò¿ÉÒÔÖ¸¶¨ÆäËüµÄ´æ´¢ÒýÇ棬Äã¿ÉÒÔÔÚͬһ¸öÊý¾Ý¿âÖжԲ»Í¬µÄ±íʹÓò»Í¬µÄ´æ´¢ÒýÇæ(ÕâÊǷdz£Ç¿´ó¶ø¶ÀÌصÄÌØÐÔ)¡£¿ÉÒÔͨ¹ýSHOW TABLE STATUSÃüÁî²éѯ±íËùʹÓõĴ洢ÒýÇ棬ÀýÈ磬²é¿´mysqlÊý¾Ý¿âµÄuser±í£º
mysql> SHOW TABLE STATUS LIKE 'user' \G *************************** 1. row *************************** Name: user Engine: MyISAM Version: 10 Row_format: Dynamic Rows: 4 Avg_row_length: 61 Data_length: 244 Max_data_length: 281474976710655 Index_length: 2048 Data_free: 0 Auto_increment: NULL Create_time: 2009-06-16 21:50:34 Update_time: 2009-09-30 14:59:08 Check_time: NULL Collation: utf8_bin Checksum: NULL Create_options: Comment: Users and global privileges 1 row in set (0.00 sec) |
Name£º±íµÄÃû³Æ£»
Engine£º±íʹÓõĴ洢ÒýÇ棻
Row_format£º¼Ç¼µÄ¸ñʽ¡£MyISAMÖ§³ÖÈýÖÖ²»Í¬µÄ´æ´¢¸ñʽ£º¾²Ì¬(¹Ì¶¨³¤¶È)±í(ĬÈϸñʽ)¡¢¶¯Ì¬±í¼°Ñ¹Ëõ±í¡£¾²Ì¬±íµÄ×ֶζ¼Êǹ̶¨³¤¶ÈµÄ£¬ÀýÈçCHARºÍINTEGER£»¶¯Ì¬±íµÄ×ֶοÉÒÔÊDZ䳤µÄ£¬ÀýÈ磬VARCHAR»òÕßBLOB¡£
Rows£º±íÖмǼµÄÊýÁ¿¡£
Avg_row_length£º¼Ç¼µÄƽ¾ù³¤¶È(×Ö½ÚÊý)£»
Data_length£º±íÖÐÊý¾ÝµÄÈ«²¿×Ö½ÚÊý£»
Max_data_length£º±íÖÐÊý¾Ý×î´óµÄ×Ö½ÚÊý£»
Index_length£ºË÷ÒýÏûºÄµÄ´ÅÅ̿ռ䣻
Data_free£º¶ÔÓÚMyISAM±í£¬±íʾÒѾ·ÖÅ䵫»¹Ã»ÓÐʹÓõĿռ䣻¸Ã¿Õ¼ä°üº¬ÒÔǰɾ³ýµÄ¼Ç¼ÁôϵĿռ䣬¿ÉÒÔ±»INSERT²Ù×÷ÖØÓá£
Auto_increment£ºÏÂÒ»¸ö×ÔÔöµÄÖµ¡£
Check_time£ºÉÏ´ÎʹÓÃCHECK TABLE»òmyisamchk¼ì²é±íµÄʱ¼ä¡£
1.4.1¡¢MyISAM
1.4.1.1¡¢´æ´¢
MySQLµÄĬÈÏ´æ´¢ÒýÇ棬ÐÔÄÜÓ빦ÄܵÄÕÛÖУ¬°üÀ¨È«ÎÄË÷Òý(full-text index)¡¢Êý¾ÝѹËõ£¬Ö§³Ö¿Õ¼ä(GIS)Êý¾Ý£¬µ«ÊÇ£¬²»Ö§³ÖÊÂÎñºÍÐм¶Ëø¡£Ò»°ãÀ´Ëµ£¬MyISAM¸üÊÊÓÃÓÚ´óÁ¿²éѯ²Ù×÷¡£Èç¹ûÄãÓдóÁ¿µÄ²åÈ롢ɾ³ý²Ù×÷£¬ÄãÓ¦¸ÃÑ¡ÔñInnoDB¡£
ÿ¸ö±í°üº¬3¸öÎļþ£º
(1).frm£º±í¶¨ÒåÎļþ£¬¶ÔÓÚÆäËü´æ´¢ÒýÇæÒ²Ò»Ñù¡£
(2).MYDÎļþ£ºÊý¾ÝÎļþ¡£
(3).MYIÎļþ£ºË÷ÒýÎļþ¡£
¿ÉÒÔÔÚ´´½¨±íʱͨ¹ýDATA DIRECTORYºÍINDEX DIRECTORYΪÊý¾ÝÎļþºÍË÷ÒýÎļþÖ¸¶¨Â·¾¶£¬ËüÃÇ¿ÉÒÔλÓÚ²»Í¬Ä¿Â¼¡£ÁíÍ⣬MyISAMµÄ´æ´¢¸ñʽÊÇ¿çƽ̨µÄ£¬Äã¿ÉÒÔ½«Êý¾ÝÎļþºÍË÷ÒýÎļþ´ÓIntelƽ̨¿½±´µ½PPC»òÕßSPARCƽ̨¡£
5.0ÖУ¬MyISAMµÄ±ä³¤¼Ç¼±íĬÈÏ´¦Àí256TBÊý¾Ý£¬Ê¹ÓÃ6×Ö½ÚµÄÖ¸ÕëÀ´Ö¸ÏòÊý¾Ý¼Ç¼£»¶ø֮ǰµÄ°æ±¾Ê¹ÓÃĬÈϵÄ4×Ö½ÚÖ¸Õ룬ËùÒÔÖ»ÄÜ´¦Àí4GBÊý¾Ý¡£ËùÓеİ汾¶¼¿ÉÒÔ½«Ö¸ÕëÔö¼Óµ½8×Ö½ÚÖ¸Õ룬Èç¹ûÄãÏë¸Ä±äMyISAM±íµÄÖ¸ÕëµÄ´óС£¬¿ÉÒÔͨ¹ýÉèÖÃMAX_ROWSºÍAVG_ROW_LENGTHÀ´ÊµÏÖ£º
CREATE TABLE mytable (
a INTEGER NOT NULL PRIMARY KEY,
b CHAR(18) NOT NULL
) MAX_ROWS = 1000000000 AVG_ROW_LENGTH = 32;
ÉÏÃæµÄÀý×ÓÖУ¬MySQL½«ÖÁÉÙ¿ÉÒÔ´æ´¢32GBµÄÊý¾Ý¡£¿ÉÒԲ鿴һϱíµÄÐÅÏ¢£º
mysql> SHOW TABLE STATUS LIKE 'mytable' \G *************************** 1. row *************************** Name: mytable Engine: MyISAM Row_format: Fixed Rows: 0 Avg_row_length: 0 Data_length: 0 Max_data_length: 98784247807 Index_length: 1024 Data_free: 0 Auto_increment: NULL Create_time: 2002-02-24 17:36:57 Update_time: 2002-02-24 17:36:57 Check_time: NULL Create_options: max_rows=1000000000 avg_row_length=32 Comment: 1 row in set (0.05 sec) |
¿ÉÒÔ¿´µ½£¬Create_optionsÁгöÁË´´½¨Ê±µÄÑ¡Ï¶øÇҸñíµÄ×î´óµÄÊý¾ÝÁ¿Îª91GB¡£Äã¿ÉÒÔÓÃALTER TABLEÀ´¸Ä±äÖ¸ÕëµÄ´óС£¬µ«ÊÇÄǻᵼÖ±íºÍË÷ÒýµÄÖؽ¨£¬Õâ»á»¨·ÑºÜ³¤µÄʱ¼ä¡£
1.4.1.2¡¢MyISAMµÄÌØÐÔ
(1)ËøÓë²¢·¢ÐÔ£ºMyISAMÖ»ÓÐ±í¼¶Ëø£¬²»Ö§³ÖÐм¶Ëø¡£ËùÒÔ²»ÊʺÏÓÚ´óÁ¿µÄд²Ù×÷£¬µ«ÊÇËüÖ§³Ö²¢·¢²åÈë(concurrent inserts)£¬ÕâÊÇÒ»¸ö·Ç³£ÖØÒªÇÒÓÐÓõÄÌØÐÔ¡£
(2)×Ô¶¯ÐÞ¸´£ºMySQLÖ§³Ö×Ô¶¯¼ì²éºÍÐÞ¸´MyISAM±í¡£
(3)ÊÖ¶¯ÐÞ¸´£ºÄã¿ÉÒÔʹÓÃCHECK TABLE¼ì²é±íµÄ״̬£¬²¢ÓÃREPAIR TABLEÐÞ¸´±í¡£
(4)Ë÷Òý£ºÄã¿ÉÒÔΪBLOBºÍTEXTµÄÇ°500¸ö×Ö·û´´½¨Ë÷Òý¡£¶øÇÒ£¬MyISAM»¹Ö§³ÖÈ«ÎÄË÷Òý£¬µ«½öÏÞÓÚCHAR¡¢VARCHAR¡¢ºÍTEXTÁС£
(5)ÑÓ³Ù¼üд(Delayed key writes)£ºÈç¹û´´½¨MyISAM±íʱָ¶¨DELAY_KEY_WRITE£¬MySQLÔÚ²éѯ½áÊøʱ£¬²»»á½«¸Ä±äµÄË÷ÒýÊý¾ÝдÈë´ÅÅÌ£¬¶ø½«Ð޸ı£´æÔÚkey bufferÖС£Ö»ÓÐÒª¸Ä±ä»º´æ»òÕ߹رձíʱ£¬²Å»á°ÑË÷ÒýÊý¾ÝË¢Èë´ÅÅÌ¡£
1.4.2¡¢InnoDB
InnoDBÊÇÒ»¸ö¸ßÐÔÄܵÄÊÂÎñ´æ´¢ÒýÇ棬´ËÍ⣬BDBÒ²Ö§³ÖÊÂÎñ´¦Àí(¹ØÓÚBDB£¬ÒÔÇ°Ôø½ÏΪÏêϸµÄÔĶÁ¹ýÆäÔ´Â룬ÒÔºóÓÐʱ¼äÔÙÌÖÂÛ)£¬ËüÓÐÒÔÏÂһЩÌص㣺
1.4.2.1¡¢±í¿Õ¼ä
InnoDB´æ´¢±íºÍË÷ÒýÓÐÁ½ÖÖ·½Ê½£º
(1)¹²Ïí±í¿Õ¼ä´æ´¢£ºÕâÖÖ·½Ê½Ï£¬±íµÄ¶¨ÒåλÓÚ.frmÎļþÖУ¬Êý¾ÝºÍË÷Òý±£´æÔÚinnodb_data_home_dirºÍinnodb_data_file_pathÖ¸¶¨µÄ±í¿Õ¼äÖС£
(2)¶à±í¿Õ¼ä´æ´¢£º±íµÄ¶¨ÒåÈÔλÓÚ.frmÎļþ£¬µ«ÊÇ£¬Ã¿¸öInnoDB±íºÍËüµÄË÷ÒýÔÚËü×Ô¼ºµÄÎļþ(.idb)ÖУ¬Ã¿¸ö±íÓÐËü×Ô¼ºµÄ±í¿Õ¼ä¡£
¶ÔÄÇЩÏë°ÑÌض¨±í¸ñÒƵ½·ÖÀëÎïÀí´ÅÅ̵ÄÓû§£¬»òÕßÄÇЩϣÍû¿ìËÙ»Ö¸´µ¥¸ö±íµÄ±¸·Ý¶øÎÞÐë´ò¶ÏÆäÓàInnoDB±íµÄʹÓõÄÓû§£¬Ê¹Óöà±í¿Õ¼ä»áÊÇÓÐÒæµÄ¡£Äã¿ÉÒÔÍùmy.cnfµÄ[mysqld]½ÚÌí¼ÓÏÂÃæÐÐÀ´ÔÊÐí¶à±í¿Õ¼ä£º
[mysqld]
innodb_file_per_table
ÖØÆô·þÎñÆ÷Ö®ºó£¬InnoDB´æ´¢Ã¿¸öд´½¨µÄ±íµ½±í¸ñËùÊôÓÚµÄÊý¾Ý¿âĿ¼ÏÂËü×Ô¼ºµÄÎļþtbl_name.ibdÀï¡£ÕâÀàËÆÓÚMyISAM´æ´¢ÒýÇæËù×öµÄ£¬µ«MyISAM °Ñ±í·Ö³ÉÊý¾ÝÎļþtbl_name.MYDºÍË÷ÒýÎļþtbl_name.MYI¡£¶ÔÓÚInnoDB£¬Êý¾ÝºÍËùÒÔ±»Ò»Æð´æµ½.ibdÎļþ¡£tbl_name.frmÎļþÕÕ¾ÉÒÀÈ»±»´´½¨¡£
Èç¹ûÄã´Ómy.cnfÎļþɾ³ýinnodb_file_per_tableÐУ¬²¢ÖØÆô·þÎñÆ÷£¬InnoDBÔÚ¹²ÏíµÄ±í¿Õ¼äÎļþÀïÔٴδ´½¨±í¡£
innodb_file_per_tableÖ»Ó°Ïì±íµÄ´´½¨¡£Èç¹ûÄãÓÃÕâ¸öÑ¡ÏîÆô¶¯·þÎñÆ÷£¬ÐÂ±í±»ÓÃ.ibdÎļþÀ´´´½¨£¬µ«ÊÇÄãÈÔ¾ÉÄÜ·ÃÎÊÔÚ¹²Ïí±í¿Õ¼äÀïµÄ±í¡£Èç¹ûÄãɾµôÕâ¸öÑ¡ÏбíÔÚ¹²Ïí±í¿Õ¼äÄÚ´´½¨£¬µ«ÄãÈԾɿÉÒÔ·ÃÎÊÈκÎÓöà±í¿Õ¼ä´´½¨µÄ±í¡£
InnoDB×ÜÊÇÐèÒª¹²Ïí±í¿Õ¼ä£¬.ibdÎļþ¶ÔInnoDB²»×ãÒÔÈ¥ÔËÐУ¬¹²Ïí±í¿Õ¼ä°üº¬ÊìϤµÄibdataÎļþ£¬InnoDB°ÑÄÚ²¿Êý¾Ý´ÊµäºÍundoÈÕÖ¾·ÅÔÚÕâ¸öÎļþÖС£
1.4.2.2¡¢Íâ¼üÔ¼Êø
MySQLÖУ¬Ö§³ÖÍâ¼üµÄ´æ´¢ÒýÇæÖ»ÓÐInnoDB£¬ÔÚ´´½¨Íâ¼üʱ£¬ÒªÇó±»²ÎÕÕ±í±ØÐëÓжÔÓ¦µÄË÷Òý£¬²ÎÕÕ±íÔÚ´´½¨Íâ¼üʱҲ»á×Ô¶¯´´½¨¶ÔÓ¦µÄË÷Òý¡£
1.4.2.3¡¢MVCCÓëºóÂëËø(next-key locking)
InnoDB½«MVCC»úÖÆÓënext-key lock½áºÏÆðÀ´£¬ÊµÏÖÊÂÎñµÄ¸÷¸ö¸ôÀ뼶±ð£¬ÕâÊǷdz£ÓÃÒâ˼µÄ¡£ÔÚnodb_locks_unsafe_for_binlog±äÁ¿±»ÉèÖûòÕßÊÂÎñµÄ¸ôÀ뼶±ð²»ÊÇSERIALIZABLEµÄÇé¿öÏ£¬InnoDB¶ÔÓÚûÓÐÖ¸¶¨FOR UPDATE »ò LOCK IN SHARE MODEµÄINSERT INTO ... SELECT, UPDATE ... (SELECT), ºÍCREATE TABLE ... SELECTÓï¾äʹÓÃÒ»ÖÂÐÔ¶Á(²ÎÕÕÇ°Ãæ)£¬ÔÚÕâÖÖÇé¿öÏ£¬²éѯÓï¾ä²»»á¶Ô±íÖеÄÔª×é¼ÓËø¡£·ñÔò£¬InnoDB½«Ê¹ÓÃËø¡£
Ö÷Òª²Î¿¼£º
¡¶MySQL Manual¡·
¡¶High Performance MySQL¡·
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ