ÔÚÏÈÇ°µÄÎÄÕ¡¶ÓÖÅÄÍø²ÉÓÃÁËÈ«¾ÖΨһµÄ×Ö¶Î×÷ΪÖ÷¼ü¡£±ÈÈçÄÃÕÕƬ±íΪÀý£¬ ËäÈ»²»Í¬Óû§µÄÕÕƬÊý¾Ý´æ·ÅÔÚ²»Í¬µÄShard£¨»òÕß˵MySQL½Úµã/ʵÀý, Çë²Î¿¼¡¶ÕâƪÎÄÕÂÀïµÄ½â¾ö°ì·¨¡£
¿ÉÄܵĽâ¾ö·½°¸
- UUID
»òÐí¿ÉÒÔ²ÉÓÃUUID×÷ΪÖ÷¼ü£¬µ«ÊÇUUIDºÃ³¤µÄÒ»´®£¬·ÅÔÚURLÀïºÃÄÑ¿´°¡£¬ÓÐľÓÐ? µ±È»Õâ¸ö²»ÊǹؼüËùÔÚ£¬¸üÖØÒªµÄÔÒò»¹ÊÇÐÔÄÜ¡£UUIDµÄÉú³ÉûÓÐ˳ÐòÐÔ£¬ËùÒÔÔÚдÈëʱ£¬ ÐèÒªËæ»ú¸ü¸ÄË÷ÒýµÄ²»Í¬Î»Öã¬Õâ¾ÍÐèÒª¸ü¶àµÄIO²Ù×÷£¬Èç¹ûË÷ÒýÌ«´ó¶ø²»ÄÜ´æ·ÅÔÚÄÚ´æÖеĻ°¾Í¸üÊÇÈç´Ë¡£ ¶øUUIDË÷Òýʱ£¬Ò»¸ökeyÐèÒª32¸ö×Ö½Ú(µ±È»Èç¹û²ÉÓöþ½øÖÆÐÎʽ´æ´¢µÄ»°¿ÉÒÔѹËõµ½16¸ö×Ö½Ú)£¬ Òò´ËÕû¸öË÷ÒýÒ²»áÏà¶Ô±È½Ï´ó¡£
- MySQL×ÔÔö×Ö¶Î
ÔÚµ¥¸öMySQLÊý¾Ý¿âµÄÓ¦ÓÃÖÐÒ»°ãÉèÖÃÒ»¸ö×ÔÔöµÄ×ֶξͿÉÒÔÁË£¬¶øÔÚˮƽ·Ö¿âµÄÉè¼Æµ±ÖУ¬ÕâÖÖ·½·¨ÏÔÈ»²»Äܱ£Ö¤È«¾ÖΨһ¡£ ÄÇôÎÒÃÇ¿ÉÒÔµ¥¶À½¨Á¢Ò»¸ö¿âÓÃÀ´Éú³ÉID£¬ÔÚShardÖеÄÿÕűíÔÚÕâ¸öID¿âÖж¼ÓÐÒ»¸ö¶ÔÓ¦µÄ±í£¬¶øÕâ¸ö¶ÔÓ¦µÄ±íÖ»ÓÐÒ»¸ö×ֶΣ¬ Õâ¸ö×Ö¶ÎÊÇ×ÔÔöµÄ¡£µ±ÎÒÃÇÐèÒª²åÈëеÄÊý¾Ý£¬ÎÒÃÇÊ×ÏÈÔÚID¿âÖеÄÏàÓ¦±íÖвåÈëÒ»Ìõ¼Ç¼£¬ÒԴ˵õ½Ò»¸öеÄID£¬ È»ºó½«Õâ¸öID×÷Ϊ²åÈëµ½ShardÖеÄÊý¾ÝµÄÖ÷¼ü¡£Õâ¸ö·½·¨µÄȱµã¾ÍÊÇÐèÒª¶îÍâµÄ²åÈë²Ù×÷£¬Èç¹ûID¿â±äµÄºÜ´ó£¬ ÐÔÄÜÒ²»áËæÖ®½µµÍ¡£ËùÒÔÒ»¶¨Òª±£Ö¤ID¿âµÄÊý¾Ý¼¯²»ÒªÌ«´ó£¬Ò»¸ö°ì·¨ÊǶ¨ÆÚÇåÀíÇ°ÃæµÄ¼Ç¼¡£
- ÒýÈëÆäËü¹¤¾ß
Redis¡¢MemcachedµÈ¶¼Ö§³ÖÔ×ÓÐÔµÄincrement²Ù×÷£¬¶øÇÒÒòΪËüÃǵÄÓÅÐãÐÔÄÜ¿ÉÒÔ¼õÉÙдÈëʱµÄ¶îÍ⿪Ïú£¬ Ò²ÐíÎÒÃÇ¿ÉÒÔÄÃËüÃǵ±×÷ÐòÁÐÉú³ÉÆ÷¡£MemcachedµÄÎÊÌâÔÚÓÚ²»³Ö¾ÃÐÔ£¬ËùÒÔÎÒÃDz»»á¿¼ÂÇ¡£ ¶øRedisÒ²²»ÊÇʵʱ³Ö¾ÃµÄ£¬µ±È»Ò²¿ÉÒÔÅäÖóÉʵʱµÄ£¬µ«ÄÇÑù¹Ö¹ÖµÄ¡£µ±È»Ò²ÓÐһЩ³Ö¾ÃµÄ¹¤¾ß£¬ ±ÈÈçKyoto Cabinet¡¢Tokyo Cabinet¡¢MongoDBµÈµÈ£¬´«ËµÖÐÐÔÄܶ¼²»´í£¬µ«ÊÇÒýÈëÆäËü¹¤¾ß»áÔö¼Ó¼Ü¹¹µÄ¸´Ôӳ̶ȣ¬ Ò²»áÔö¼Óά»¤³É±¾¡£ÎÒÃǵÄÍŶӺÜС£¬¾«Á¦ÓÐÏÞ£¬ÎÒÃÇ·îÐй»ÓþͺõÄÔÔò£¬Ò²¾ÍÊÇûÓÐÌرðµÄÔÒò£¬ ÔÚ¿ÉÒÔ½ÓÊܵÄÇé¿öÏ£¬¾¡Á¿ÓÃÎÒÃÇÊìϤµÄ¹¤¾ß½â¾öÎÊÌâ¡£ËùÒÔ£¬ÎÒÃÇ»¹ÊÇÀ´¿¼ÂÇÒ»ÏÂÔõôÑùÓÃMySQLÀ´½â¾öÕâ¸öÎÊÌâ°É¡£
¸üºÃµÄ·½°¸
ÎÒÃÇÒ»¿ªÊ¼¾ÍÊDzÉÓÃÁËÉÏÃæËùÃèÊöµÄMySQL×ÔÔö×ֶεķ½·¨£¬ ºóÀ´¿´µ½¡¶Ticket Servers: Distributed Unique Primary Keys on the Cheap¡· ÕâƪÎÄÕÂÀïËùÃèÊöµÄ·½·¨£¬»íÈ»¿ªÀÊ¡£ÎÒ¾³£ÕâÑùÏ룺Èç¹ûûÓÐÄÇЩ¿ªÔ´²úÆ·¡¢Ã»ÓÐÄÇЩÎÞ˽·ÖÏí¾ÑéµÄÈË£¬ ¹âƾÎÒÃÇ×Ô¼ºµÄÄÜÁ¦ÄÜ×öµ½Ê²Ã´³Ì¶È¡£ºÜ¸ÐлÄÇЩÈË£¬ËùÒÔÎÒÒ²¾¡Á¿¶àµÄ·ÖÏíһЩ×Ô¼ºµÄ¾Ñé¡£
ÎÒÏÈÃèÊöÒ»ÏÂFlickrÄÇƪÎÄÕÂÀïËùÃèÊöµÄ·½·¨£¬ËûÃÇʹÓÃÁËREPLACE INTOÕâ¸öMySQLµÄÀ©Õ¹¹¦ÄÜ¡£REPLACE INTOºÍINSERTµÄ¹¦ÄÜÒ»Ñù£¬µ«Êǵ±Ê¹ÓÃREPLACE INTO²åÈëÐÂÊý¾ÝÐÐʱ£¬ Èç¹ûвåÈëµÄÐеÄÖ÷¼ü»òΨһ¼ü(UNIQUE Key)ÒÑÓеÄÐÐÖظ´Ê±£¬ÒÑÓеÄÐлáÏȱ»É¾³ý£¬È»ºóÔÙ½«ÐÂÊý¾ÝÐвåÈë¡£ Äã¿ÉÒÔ·ÅÐÄ£¬ÕâÊÇÔ×Ó²Ù×÷¡£
½¨Á¢ÀàËÆÏÂÃæµÄ±í£º
CREATE TABLE `tickets64` ( `id` bigint(20) unsigned NOT NULL auto_increment, `stub` char(1) NOT NULL default '', PRIMARY KEY (`id`), UNIQUE KEY `stub` (`stub`) ) ENGINE=MyISAM;
µ±ÐèÒª»ñµÃÈ«¾ÖΨһIDʱ£¬Ö´ÐÐÏÂÃæµÄSQLÓï¾ä£º
REPLACE INTO `tickets64` (`stub`) VALUES ('a'); SELECT LAST_INSERT_ID();
µÚÒ»´ÎÖ´ÐÐÕâ¸öÓï¾äºó£¬ticket64±í½«°üº¬ÒÔÏÂÊý¾Ý:
+--------+------+ | id | stub | +--------+------+ | 1 | a | +--------+------+
ÒÔºóÔÙ´ÎÖ´ÐÐÇ°ÃæµÄÓï¾ä£¬stub×Ö¶ÎֵΪ¡¯a¡¯µÄÐÐÒѾ´æÔÚ£¬ËùÒÔMySQL»áÏÈɾ³ýÕâÒ»ÐУ¬ÔÙ²åÈë¡£ Òò´Ë£¬µÚ¶þ´ÎÖ´Ðкó£¬ticket64±í»¹ÊÇÖ»ÓÐÒ»ÐÐÊý¾Ý£¬Ö»ÊÇid×ֶεÄֵΪ2¡£ Õâ¸ö±í½«Ò»Ö±Ö»ÓÐÒ»ÐÐÊý¾Ý¡£
FlickrΪPhoto, Group, Account, Task¸÷×Ô½¨Á¢ÁËÒ»ÕÅticket±íÒÔ±£³Ö¸÷×ÔµÄIDµÄÁ¬ÐøÐÔ¡£ ÆäËüÒµÎñ±íµÄID¶¼Ê¹ÓÃͬһ¸öticket±í²úÉú¡£
²»´í°É£¬Æäʵ»¹¿ÉÒÔ¸ü°ô¡£±ÈÈ磬ֻÐèÒªÒ»ÕÅticket±í¾Í¿ÉÒÔΪËùÓеÄÒµÎñ±íÌṩ¸÷×ÔÁ¬ÐøµÄID¡£ ÏÂÃ棬À´¿´Ò»ÏÂÎÒÃǵķ½·¨¡£Ê×ÏÈÀ´¿´Ò»Ï±í½á¹¹:
CREATE TABLE `sequence` ( `name` varchar(50) NOT NULL, `id` bigint(20) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`name`) ) ENGINE=InnoDB;
×¢ÒâÇø±ð£¬id×ֶβ»ÊÇ×ÔÔöµÄ£¬Ò²²»ÊÇÖ÷¼ü¡£ÔÚʹÓÃÇ°£¬ÎÒÃÇÐèÒªÏȲåÈëһЩ³õʼ»¯Êý¾Ý£º
INSERT INTO `sequence` (`name`) VALUES ('users'), ('photos'), ('albums'), ('comments');
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÖ´ÐÐÏÂÃæµÄSQLÓï¾äÀ´»ñµÃеÄÕÕƬID£º
UPDATE `sequence` SET `id` = LAST_INSERT_ID(`id` + 1) WHERE `name` = 'photos'; SELECT LAST_INSERT_ID();
ÎÒÃÇÖ´ÐÐÁËÒ»¸ö¸üвÙ×÷£¬½«id×Ö¶ÎÔö¼Ó1£¬²¢½«Ôö¼ÓºóµÄÖµ´«µÝµ½LAST_INSERT_IDº¯Êý£¬ ´Ó¶øÖ¸¶¨ÁËLAST_INSERT_IDµÄ·µ»ØÖµ¡£
ʵ¼ÊÉÏ£¬ÎÒÃDz»Ò»¶¨ÐèÒªÔ¤ÏÈÖ¸¶¨ÐòÁеÄÃû×Ö¡£Èç¹ûÎÒÃÇÏÖÔÚÐèÒªÒ»ÖÖеÄÐòÁУ¬ÎÒÃÇ¿ÉÒÔÖ±½ÓÖ´ÐÐÏÂÃæµÄSQLÓï¾ä£º
INSERT INTO `sequence` (`name`) VALUES('new_business') ON DUPLICATE KEY UPDATE `id` = LAST_INSERT_ID(`id` + 1); SELECT LAST_INSERT_ID();
ÕâÀÎÒÃDzÉÓÃÁËINSERT ¡ ON DUPLICATE KEY UPDATEÕâ¸öMySQLÀ©Õ¹£¬ Õâ¸öÀ©Õ¹µÄ¹¦ÄÜÒ²ºÍINSERTÒ»Ñù²åÈëÒ»ÐÐеļǼ£¬µ«Êǵ±Ð²åÈëµÄÐеÄÖ÷¼ü»òΨһ¼ü(UNIQUE Key)ºÍÒÑÓеÄÐÐÖظ´Ê±£¬ »á¶ÔÒÑÓÐÐнøÐÐUPDATE²Ù×÷¡£
ÐèҪעÒâµÄÊÇ£¬µ±ÎÒÃǵÚÒ»´ÎÖ´ÐÐÉÏÃæµÄÓï¾äʱ£¬ÒòΪ»¹Ã»ÓÐnameΪ¡¯new_business¡¯µÄ×ֶΣ¬ËùÒÔÕý³£µÄÖ´ÐÐÁ˲åÈë²Ù×÷£¬ ûÓÐÖ´ÐÐUPDATE£¬ËùÒÔҲûÓÐΪLAST_INSERT_ID´«µÝÖµ¡£ËùÒÔÖ®ºóÖ´ÐÐSELECT LAST_INSERT_ID()·µ»ØµÄÖµ²»¿ÉÈ·¶¨£¬ Òª¿´µ±Ç°Á¬½ÓÔÚ´Ë֮ǰִÐйýʲô²Ù×÷£¬Èç¹ûûÓÐÖ´Ðйý»áÓ°ÏìLAST_INSERT_IDÖµµÄ²Ù×÷£¬ÄÇô·µ»ØÖµ½«ÊÇ0£¬ ²»È»¾ÍÊǸòÙ×÷²úÉúµÄÖµ¡£ËùÒÔ£¬ÎÒÃÇÓ¦¸Ã¾¡Á¿±ÜÃâʹÓÃÕâÖÖ·½Ê½¡£
UPDATE: Õâ¸ö·½·¨¸üÈÝÒ×½â¾öµ¥µãÎÊÌ⣬Ҳ²»¾ÖÏÞÓÚÁ½¸ö·þÎñÆ÷£¬Ö»Òª¶Ô²»Í¬µÄ·þÎñÆ÷ÉèÖò»Í¬µÄ³õʼֵ£¨µ«±ØÐëÊÇÁ¬ÐøµÄ£©£¬ È»ºó½«ÔöÁ¿±äΪ·þÎñÆ÷Êý¾ÍÐÐÁË¡£
×ܽáÒ»ÏÂ
ÎÒ»¹ÊÇÄǾ仰£¬¹»Óþͺᣵ±È»£¬Ò²²»ÊÇ˵¾Í²»ÒªÈ¥Á˽âÆäËü²úÆ·¡¢·½°¸ÁË¡£ÓÖÅÄÍøÒ²ÔÚʹÓÃһЩÐÂÐ˵IJúÆ·£¬ ±ÈÈçRedis£¨ÔÚ10Äê3Ô¾ͿªÊ¼ÔÚÕýʽ»·¾³ÏÂʹÓÃÁË£¬ËãÊDZȽÏÔçµÄʹÓÃÕߣ©£¬ ÒòΪËüµÄÒýÈëµÄÈ·Äܹ»¸üºÃ¡¢¸ü·½±ã¡¢¸ü¸ßЧµÄ½â¾öÎÒÃǵÄijЩÎÊÌâ¡£ ¹Ø¼ü»¹ÊÇÐèÒªÔÚʹÓÃÇ°¶ÔÆä½øÐÐ×ã¹»µÄÁ˽⡣ÎÒ»áÔÚºóÃæµÄÎÄÕÂÖнéÉÜÒ»ÏÂRedisµÄʹÓÃÇé¿ö¡£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿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¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ