ºìÁªLinuxÃÅ»§
Linux°ïÖú

mysqlÖÐselect * for updateËø±íµÄÎÊÌâ

·¢²¼Ê±¼ä:2008-08-21 00:04:20À´Ô´:ºìÁª×÷Õß:Osllgre
ÏÈÇ°½éÉܹýSELECT ... FOR UPDATEµÄÓ÷¨£¬²»¹ýËø¶¨(Lock)µÄ×ÊÁÏÊÇÅбð¾ÍµÃҪעÒâÒ»ÏÂÁË¡£ÓÉÓÚInnoDBÔ¤ÉèÊÇRow-Level Lock£¬ËùÒÔÖ»ÓС¸Ã÷È·¡¹µÄÖ¸¶¨Ö÷¼ü£¬MySQL²Å»áÖ´ÐÐRow lock (Ö»Ëøס±»Ñ¡È¡µÄ×ÊÁÏÀý) £¬·ñÔòMySQL½«»áÖ´ÐÐTable Lock (½«Õû¸ö×ÊÁÏ±íµ¥¸øËøס)¡£

¾Ù¸öÀý×Ó:

¼ÙÉèÓиö±íµ¥products £¬ÀïÃæÓÐid¸úname¶þ¸öÀ¸Î»£¬idÊÇÖ÷¼ü¡£

Àý1: (Ã÷È·Ö¸¶¨Ö÷¼ü£¬²¢ÇÒÓд˱Ê×ÊÁÏ£¬row lock)

SELECT * FROM products WHERE id='3' FOR UPDATE;

Àý2: (Ã÷È·Ö¸¶¨Ö÷¼ü£¬Èô²éÎ޴˱Ê×ÊÁÏ£¬ÎÞlock)

SELECT * FROM products WHERE id='-1' FOR UPDATE;

Àý2: (ÎÞÖ÷¼ü£¬table lock)

SELECT * FROM products WHERE name='Mouse' FOR UPDATE;

Àý3: (Ö÷¼ü²»Ã÷È·£¬table lock)

SELECT * FROM products WHERE id<>'3' FOR UPDATE;

Àý4: (Ö÷¼ü²»Ã÷È·£¬table lock)

SELECT * FROM products WHERE id LIKE '3' FOR UPDATE;

×¢1: FOR UPDATE½öÊÊÓÃÓÚInnoDB£¬ÇÒ±ØÐëÔÚ½»Ò×Çø¿é(BEGIN/COMMIT)ÖвÅÄÜÉúЧ¡£

×¢2: Òª²âÊÔËø¶¨µÄ×´¿ö£¬¿ÉÒÔÀûÓÃMySQLµÄCommand Mode £¬¿ª¶þ¸öÊÓ´°À´×ö²âÊÔ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ