Ëæ×Å»¥ÁªÍøµÄ²»¶Ï·¢Õ¹£¬ÈÕ³£Éú»îÖÐÔ½À´Ô½¶àµÄÐèÇóͨ¹ýÍøÂçÀ´ÊµÏÖ£¬´ÓÒÂʳסÐе½½ðÈÚ½ÌÓý£¬´Ó¿Ú´üµ½Éí·Ý£¬ÈËÃÇÎÞʱÎ޿̲»ÒÀÀµ×ÅÍøÂ磬¶øÇÒÔ½À´Ô½¶àµÄÈËͨ¹ýÍøÂçÀ´Íê³É×Ô¼ºµÄÐèÇó¡£
×÷Ϊֱ½ÓÃæ¶ÔÀ´×Ô¿Í»§ÇëÇóµÄWeb·þÎñ¶Ë£¬ÎÞÒÉҪͬʱ³ÐÊܸü¶àµÄÇëÇ󣬲¢ÎªÓû§Ìṩ¸üºÃµÄÌåÑé¡£Õâ¸öʱºòWeb¶ËµÄÐÔÄܳ£³£»á³ÉΪҵÎñ·¢Õ¹µÄÆ¿¾±£¬ÌáÉýÐÔÄ̲ܿ»ÈÝ»º¡£±¾ÎÄ×÷ÕßÔÚ¿ª·¢¹ý³ÌÖÐ×ܽáÁËһЩÌáÉýWeb·þÎñ¶ËÐÔÄܵľÑ飬Óë´ó¼Ò·ÖÏí¡£
ÎÊÌâ·ÖÎö
¶ÔÓÚWeb·þÎñ¶ËÐÔÄÜ£¬Ê×ÏÈÎÒÃÇ·ÖÎöÒ»ÏÂÏà¹ØÖ¸±ê¡£´ÓÓû§½Ç¶È½²£¬Óû§µ÷ÓÃWeb·þÎñʱ£¬ÇëÇó·µ»Øʱ¼äÔ½¶Ì£¬Óû§ÌåÑéÔ½ºÃ¡£´Ó·þÎñ¶Ë½Ç¶È½²£¬Í¬Ò»Ê±¼äÄܳÐÔØÓû§ÇëÇóÁ¿Ô½´ó£¬·þÎñ¶ËÐÔÄܾÍԽǿ¡£×ÛºÏÁ½·½Ã棬ÎÒÃÇ×ܽáÐÔÄÜÓÅ»¯µÄÁ½¸ö·½Ïò£º
1. Ôö¼Ó·þÎñ¶ËËùÄÜÖ§³Å²¢·¢ÇëÇóµÄ×î´óÊýÁ¿£»
2. Ìá¸ßÿ¸öÇëÇó´¦ÀíËٶȡ£
Ã÷È·ÁËÓÅ»¯·½Ïò£¬Ê×ÏȽéÉÜÒ»ÖÖ·þÎñ¶Ëͨ³£µÄ¼Ü¹¹Ä£Ê½£¬¼´À´×Ôä¯ÀÀÆ÷»òÕßAppµÄWebÒ»¸öÇëÇó£¬ÔÚ·þÎñ¶Ë¾¹ýÄļ¸²ã½á¹¹±»´¦Àí²¢·µ»ØµÄ¡£
¼Ü¹¹Ä£Ê½£ºIP¸ºÔؾùºâ->»º´æ·þÎñÆ÷->·´Ïò´úÀí->Ó¦Ó÷þÎñÆ÷->Êý¾Ý¿â
Èçͼ1Ëùʾ£¬ÎªÁË˵Ã÷·½±ã£¬ÎÒÃÇÀ´¾Ù¸öʵ¼ÊµÄÀý×Ó: LVS(Keepalived)->Squid->nginx->Go->MySQL
Web·þÎñ¶ËÐÔÄÜÌáÉý·½·¨Web·þÎñ¶ËÐÔÄÜÌáÉý·½·¨
ͼ1£º·þÎñ¶Ë¼Ü¹¹
ÎÒÃǶÔÇëÇóÔÚÿ²ã×ö·Ö·¢´¦Àí£¬ÕâÑù¿ÉÒÔʹÏÂÒ»¼¶½á¹¹Óжà¸ö·Ö֧ͬʱ¹¤×÷£¬À´Ìá¸ß×ÜÌåµÄ×î´ó²¢·¢Êý¡£
½áºÏ¼Ü¹¹£¬ÎÒÃÇÀ´·ÖÎöͨ³£ÓÐÄÄЩÎÊÌâÔÚÍÏÁËÐÔÄܵĺóÍÈ£¬ÒÔ¼°ÕÒ³ö¶ÔÓ¦µÄ½â¾ö·½·¨¡£
Õý³£Çé¿öÏ£¬IP¸ºÔؾùºâ£¬»º´æ·þÎñÆ÷ºÍnginx´úÀíÕ⼸²ãÖ÷ÒªÊǼ¯ÈºÎȶ¨ÐÔÎÊÌâ¡£ÈÝÒ׳öÏÖÐÔÄÜÆ¿¾±µÄµØ·½ÍùÍùÊÇÓ¦Ó÷þÎñÆ÷²ãºÍÊý¾Ý¿â²ã£¬ÎÒÃÇÏÂÃæÀ´Áоټ¸¸öÀý×Ó£º
1. ×èÈûµÄÓ°Ïì
£¨1£©ÎÊÌ⣺
´ó²¿·ÖWebÇëÇó¶¼ÊÇ×èÈûÐÔÖʵģ¬µ±Ò»¸öÇëÇó±»´¦Àíʱ£¬½ø³Ì¾Í»á±»¹ÒÆð£¨Õ¼ÓÃCPU£©Ö±ÖÁÇëÇóÍê³É¡£ÔÚ´ó¶àÊýÇé¿öÏ£¬WebÇëÇóÍê³ÉµÄ×ã¹»¿ì£¬ËùÒÔÕâ¸öÎÊÌâ²¢²»±»¹Ø×¢¡£È»¶ø£¬¶ÔÓÚÄÇЩÏìӦʱ¼äÀ´Íê³ÉµÄÇëÇó£¨Ïñ·µ»ØÊý¾ÝÁ¿´óµÄÇëÇó»òÍⲿAPI£©£¬ÕâÒâζ×ÅÓ¦ÓóÌÐò±»Ëø¶¨Ö±ÖÁ´¦Àí½áÊø£¬ÕâÆڼ䣬ÆäËûµÄÇëÇ󲻻ᱻ´¦Àí£¬ºÜÃ÷ÏÔ£¬ÕâЩÎÞЧµÄµÈ´ýʱ¼äÀ˷ѵôÁË£¬²¢ÇÒÕ¼ÓÃϵͳ×ÊÔ´£¬ÑÏÖصÄÓ°ÏìÁËÎÒÃÇ¿ÉÒÔ¸ºµ£µÄ²¢·¢ÇëÇóµÄÊýÁ¿¡£
£¨2£©½â¾ö°ì·¨£º
Web·þÎñ¶ËÔڵȴýÉÏÒ»¸öÇëÇó´¦ÀíµÄ¹ý³ÌÖУ¬ÎÒÃÇ¿ÉÒÔÈÃI/OÑ»·´ò¿ªÒԱ㴦ÀíÆäËûÓ¦ÓÃÇëÇó£¬Ö±µ½´¦ÀíÍê³ÉʱÆô¶¯Ò»¸öÇëÇ󲢸øÓè·´À¡£¬¶ø²»ÔÙÊǵȴýÇëÇóÍê³ÉµÄ¹ý³ÌÖйÒÆð½ø³Ì¡£ÕâÑù£¬ÎÒÃÇ¿ÉÒÔ½ÚʡһЩûÓбØÒªµÄµÈ´ýʱ¼ä£¬ÓÃÕâЩʱ¼äÈ¥´¦Àí¸ü¶àµÄÇëÇó£¬ÕâÑùÎÒÃǾͿÉÒÔ´ó´óÔö¼ÓÇëÇóµÄÍÌÍÂÁ¿£¬Ò²¾ÍÊÇÔÚºê¹ÛÉÏÌá¸ßÁËÎÒÃÇ¿É´¦ÀíµÄ²¢·¢ÇëÇóÊý¡£
£¨3£©Àý×Ó
ÕâÀïÎÒÃÇÓÃPythonµÄÒ»¿îWeb¿ò¼ÜTornadoÀ´¾ßÌå˵Ã÷¸Ä±ä×èÈû·½Ê½Ìá¸ß²¢·¢ÐÔÄÜ¡£
³¡¾°£ºÎÒÃǹ¹½¨Ò»¸öÏòÔ¶¶Ë£¨Ä³¸öÊ®·ÖÎȶ¨µÄÍøÕ¾£©·¢ËÍHTTPÇëÇóµÄ¼òµ¥WebÓ¦Óá£ÕâÆڼ䣬ÍøÂç´«ÊäÎȶ¨£¬ÎÒÃDz»¿¼ÂÇÍøÂçÀ´´øµÄÓ°Ïì¡£
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃSiege£¨Ò»¿îѹÁ¦²âÊÔÈí¼þ£©¶Ô·þÎñ¶ËÔÚ10ÃëÄÚÖ´ÐдóÔ¼10¸ö²¢·¢ÇëÇó¡£
Èçͼ2Ëùʾ£¬ÎÒÃÇ¿ÉÒÔºÜÈÝÒ׿´³ö£¬ÕâÀïµÄÎÊÌâÊÇÎÞÂÛÿ¸öÇëÇó×ÔÉí·µ»Ø¶àô¿ì£¬·þÎñÆ÷¶ÔÔ¶¶ËµÄ·ÃÎÊÇëÇóÍù·µ¶¼»á²úÉú×ã¹»´óµÄÖͺó£¬ÒòΪ½ø³ÌÖ±µ½ÇëÇóÍê³É²¢ÇÒÊý¾Ý±»´¦ÀíÇ°¶¼Ò»Ö±´¦ÓÚÇ¿ÖƹÒÆð״̬¡£µ±Ò»Á½¸öÇëÇóʱÕ⻹²»ÊÇÒ»¸öÎÊÌ⣬µ«´ïµ½100¸ö£¨ÉõÖÁ10¸ö£©Óû§Ê±£¬ÕâÒâζ×ÅÕûÌå±äÂý¡£Èçͼ£¬²»µ½10Ãëʱ¼ä10¸öÏàËÆÓû§µÄƽ¾ùÏìӦʱ¼ä´ïµ½ÁË1.99Ã룬¹²¼Æ29´Î¡£Õâ¸öÀý×ÓֻչʾÁ˷dz£¼òµ¥µÄÂß¼¡£Èç¹ûÄãÒªÌí¼ÓÆäËûÒµÎñÂß¼»òÊý¾Ý¿âµÄµ÷ÓõĻ°£¬½á¹û»á¸üÔã¸â¡£Ôö¼Ó¸ü¶àµÄÓû§ÇëÇóʱ£¬Í¬Ê±¿É±»´¦ÀíµÄÇëÇó¾Í»áÔö³¤»ºÂý£¬ÉõÖÁÓÐЩÇëÇó»á·¢Éú³¬Ê±»òʧ°Ü¡£
Web·þÎñ¶ËÐÔÄÜÌáÉý·½·¨Web·þÎñ¶ËÐÔÄÜÌáÉý·½·¨
ͼ2£º×èÈûʽÏìÓ¦
ÏÂÃæÎÒÃÇÓÃTornadoÖ´ÐзÇ×èÈûµÄHTTPÇëÇó¡£
Èçͼ3Ëùʾ£¬ÎÒÃÇ´ÓÿÃë3.20¸öÊÂÎñÌáÉýµ½ÁË12.59£¬ÔÚÏàͬµÄʱ¼äÄÚ×ܹ²ÌṩÁË118´ÎÇëÇó¡£ÕâÕæÊÇÒ»¸ö·Ç³£´óµÄ¸ÄÉÆ£¡ÕýÈçÄãËùÏëÏóµÄ£¬Ëæ×ÅÓû§ÇëÇóÔö¶àºÍ²âÊÔʱ¼äÔö³¤Ê±£¬Ëü½«Äܹ»Ìṩ¸ü¶àÁ¬½Ó£¬²¢ÇÒ²»»áÓöµ½ÉÏÃæ°æ±¾ÔâÊܵıäÂýµÄÎÊÌâ¡£´Ó¶øÎȶ¨µÄÌá¸ßÁ˿ɸºÔصIJ¢·¢ÇëÇóÊý¡£
Web·þÎñ¶ËÐÔÄÜÌáÉýʵ¼ù
Web·þÎñ¶ËÐÔÄÜÌáÉý·½·¨Web·þÎñ¶ËÐÔÄÜÌáÉý·½·¨
ͼ3£º·Ç×èÈûʽÏìÓ¦
2. ¼ÆËãЧÂʶÔÏìӦʱ¼äºÍ²¢·¢ÊýµÄÓ°Ïì
ÏÈÀ´½éÉÜһϻù´¡ÖªÊ¶£ºÒ»¸öÓ¦ÓóÌÐòÊÇÔËÐÐÔÚ»úÆ÷ÉϵÄÒ»¸ö½ø³Ì£»½ø³ÌÊÇÒ»¸öÔËÐÐÔÚ×Ô¼ºÄÚ´æµØÖ·¿Õ¼äÀïµÄ¶ÀÁ¢Ö´ÐÐÌå¡£Ò»¸ö½ø³ÌÓÉÒ»¸ö»ò¶à¸ö²Ù×÷ϵͳÏß³Ì×é³É£¬ÕâЩÏß³ÌÆäʵÊǹ²Ïíͬһ¸öÄÚ´æµØÖ·¿Õ¼äµÄÒ»Æð¹¤×÷µÄÖ´ÐÐÌå¡£
£¨1£©ÎÊÌâ
´«Í³¼ÆË㷽ʽµ¥Ïß³ÌÔËÐУ¬Ð§Âʵͣ¬¼ÆËãÄÜÁ¦Èõ¡£
£¨2£©½â¾ö°ì·¨
Ò»ÖÖ½â¾ö°ì·¨¾ÍÊÇÍêÈ«±ÜÃâʹÓÃÏ̡߳£ÀýÈ磬¿ÉÒÔʹÓöà¸ö½ø³Ì½«Öص£½»¸ø²Ù×÷ϵͳÀ´´¦Àí¡£µ«ÊÇ£¬ÓиöÁÓÊƾÍÊÇ£¬ÎÒÃDZØÐë´¦ÀíËùÓнø³Ì¼äͨÐÅ£¬Í¨³£Õâ±È¹²ÏíÄÚ´æµÄ²¢·¢Ä£ÐÍÓиü¶àµÄ¿ªÏú¡£
ÁíÒ»ÖÖ°ì·¨ÊÇÓöàÏ̹߳¤×÷£¬²»¹ý£¬¹«Èϵģ¬Ê¹ÓöàÏ̵߳ÄÓ¦ÓÃÄÑÒÔ×öµ½×¼È·£¬Í¬²½²»Í¬µÄỊ̈߳¬¶ÔÊý¾Ý¼ÓËø£¬ÕâÑùͬʱ¾ÍÖ»ÓÐÒ»¸öÏ߳̿ÉÒÔ±ä¸üÊý¾Ý¡£²»¹ý¹ýÈ¥µÄÈí¼þ¿ª·¢¾Ñé¸æËßÎÒÃÇÕâ»á´øÀ´¸ü¸ßµÄ¸´ÔӶȣ¬¸üÈÝÒ×ʹ´úÂë³ö´íÒÔ¼°¸üµÍµÄÐÔÄÜ¡£
ÆäÖÐ×îÖ÷ÒªµÄÎÊÌâÊÇÄÚ´æÖеÄÊý¾Ý¹²Ïí£¬ËüÃǻᱻ¶àÏß³ÌÒÔÎÞ·¨Ô¤ÖªµÄ·½Ê½½øÐвÙ×÷£¬µ¼ÖÂһЩÎÞ·¨ÖØÏÖ»òÕßËæ»úµÄ½á¹û£¨³Æ×÷¡°¾ºÌ¬¡±£©¡£ËùÒÔÕâ¸ö¾µäµÄ·½·¨Ã÷ÏÔ²»ÔÙÊʺÏÏÖ´ú¶àºË/¶à´¦ÀíÆ÷±à³Ì£ºthread-per-connection Ä£ÐͲ»¹»ÓÐЧ¡£ÔÚÖî¶à±È½ÏºÏÊʵķ¶Ê½ÖУ¬Óиö±»³Æ×÷ Communicating Sequential Processes£¨Ë³ÐòͨÐÅ´¦Àí£©£¨CSP, C. Hoare ·¢Ã÷µÄ£©»¹ÓÐÒ»¸ö½Ð×ö message-passing-model£¨ÏûÏ¢´«µÝ£©£¨ÒѾÔËÓÃÔÚÁËÆäËûÓïÑÔÖУ¬±ÈÈç Erlang£©¡£
ÎÒÃÇÕâÀïʹÓð취ÊÇÀûÓò¢Ðеļܹ¹À´´¦ÀíÈÎÎñ£¬Ò»¸ö²¢·¢³ÌÐò¿ÉÒÔÔÚÒ»¸ö´¦ÀíÆ÷»òÕßÄÚºËÉÏʹÓöà¸öÏß³ÌÀ´Ö´ÐÐÈÎÎñ£¬µ«ÊÇÖ»ÓÐͬһ¸ö³ÌÐòÔÚij¸öʱ¼äµãͬʱÔËÐÐÔÚ¶àºË»òÕ߶ദÀíÆ÷ÉϲÅÊÇÕæÕýµÄ²¢ÐС£
²¢ÐÐÊÇÒ»ÖÖͨ¹ýʹÓöദÀíÆ÷ÒÔÌá¸ßËٶȵÄÄÜÁ¦¡£ËùÒÔ²¢·¢³ÌÐò¿ÉÒÔÊDz¢Ðеģ¬Ò²¿ÉÒÔ²»ÊÇ¡£
²¢ÐÐģʽ¿ÉÒÔͬʱʹÓöàÏ̡߳¢¶àºË¡¢¶à´¦ÀíÆ÷£¬ÉõÖÁ¶à¼ÆËã»ú£¬ÕâÎÞÒÉ¿ÉÒÔµ÷¶¯¸ü¶à×ÊÔ´£¬´Ó¶øѹËõÏìӦʱ¼ä£¬ÌáÉýÔËËãЧÂÊ£¬¼«´óµØÔöÇ¿ÁË·þÎñ¶ËµÄÐÔÄÜ¡£
£¨3£©Àý×Ó
ÕâÀïÓÃGoÓïÑÔÖеÄGoroutineÀ´¾ßÌå˵Ã÷¡£
ÔÚGoÓïÑÔÖУ¬Ó¦ÓóÌÐò²¢·¢´¦ÀíµÄ²¿·Ö±»³Æ×÷ goroutines£¨Ð³Ì£©£¬Ëü¿ÉÒÔ½øÐиüÓÐЧµÄ²¢·¢ÔËËã¡£ÔÚг̺ͲÙ×÷ϵͳÏß³ÌÖ®¼ä²¢ÎÞÒ»¶ÔÒ»µÄ¹Øϵ£ºÐ³ÌÊǸù¾ÝÒ»¸ö»ò¶à¸öÏ̵߳ĿÉÓÃÐÔ£¬Ó³É䣨¶à·¸´Óã¬Ö´ÐÐÓÚ£©ÔÚËûÃÇÖ®Éϵģ»Ð³Ìµ÷¶ÈÆ÷ÔÚ Go ÔËÐÐʱºÜºÃµÄÍê³ÉÁËÕâ¸ö¹¤×÷¡£Ð³ÌÊÇÇáÁ¿µÄ£¬±ÈÏ̸߳üÇá¡£ËüÃǺۼ£·Ç³£²»Ã÷ÏÔ£¨Ê¹ÓÃÉÙÁ¿µÄÄÚ´æºÍ×ÊÔ´£©£ºÊ¹Óà 4K µÄÕ»ÄÚ´æ¾Í¿ÉÒÔÔÚ¶ÑÖд´½¨ËüÃÇ¡£ÒòΪ´´½¨·Ç³£Á®¼Û£¬±ØÒªµÄʱºò¿ÉÒÔÇáËÉ´´½¨²¢ÔËÐдóÁ¿µÄг̣¨ÔÚͬһ¸öµØÖ·¿Õ¼äÖÐ 100,000 ¸öÁ¬ÐøµÄг̣©¡£²¢ÇÒËüÃǶÔÕ»½øÐÐÁ˷ָ´Ó¶ø¶¯Ì¬µÄÔö¼Ó£¨»òËõ¼õ£©ÄÚ´æµÄʹÓã»Õ»µÄ¹ÜÀíÊÇ×Ô¶¯µÄ£¬µ«²»ÊÇÓÉÀ¬»ø»ØÊÕÆ÷¹ÜÀíµÄ£¬¶øÊÇÔÚгÌÍ˳öºó×Ô¶¯ÊÍ·Å¡£Ð³Ì¿ÉÒÔÔËÐÐÔÚ¶à¸ö²Ù×÷ϵͳÏß³ÌÖ®¼ä£¬Ò²¿ÉÒÔÔËÐÐÔÚÏß³ÌÖ®ÄÚ£¬ÈÃÄã¿ÉÒÔºÜСµÄÄÚ´æÕ¼ÓþͿÉÒÔ´¦Àí´óÁ¿µÄÈÎÎñ¡£ÓÉÓÚ²Ù×÷ϵͳÏß³ÌÉϵÄгÌʱ¼äƬ£¬Äã¿ÉÒÔʹÓÃÉÙÁ¿µÄ²Ù×÷ϵͳÏ߳̾ÍÄÜÓµÓÐÈÎÒâ¶à¸öÌṩ·þÎñµÄг̣¬¶øÇÒ Go ÔËÐÐʱ¿ÉÒÔ´ÏÃ÷µØÒâʶµ½ÄÄЩг̱»×èÈûÁË£¬ÔÝʱ¸éÖÃËüÃDz¢´¦ÀíÆäËûг̡£ÉõÖÁ£¬³ÌÐò¿ÉÒÔÔÚ²»Í¬µÄ´¦ÀíÆ÷ºÍ¼ÆËã»úÉÏͬʱִÐв»Í¬µÄ´úÂë¶Î¡£
ÎÒÃÇͨ³£Ï뽫һ¸ö³¤¼ÆËã¹ý³ÌÇзֳɼ¸¿é£¬È»ºóÈÃÿ¸ögoroutine¸÷×Ô¸ºÔðÒ»¿é¹¤×÷£¬ÕâÑù¶ÔÓÚµ¥Ò»ÇëÇóµÄÏìӦʱ¼äÓгɱ¶µÄÌáÉý¡£
¾Ù¸öÀý×Ó£¬ÓÐÒ»¸öÈÎÎñ·Ö3¸ö½×¶Î£¬a½×¶ÎÈ¥Êý¾Ý¿âaÖÐÈ¡Êý¾Ý£¬b½×¶ÎÈ¥Êý¾Ý¿âbÖÐÈ¡Êý¾Ý£¬c½×¶ÎºÏ²¢Êý¾Ý·µ»Ø¡£ÎÒÃÇÆô¶¯goroutineÒÔºóa¡¢b½×¶Î¿ÉÒÔÒ»Æð½øÐУ¬¼«´óµØËõ¶ÌÁËÏìӦʱ¼ä¡£
˵°×Á˾ÍÊDz¿·Ö¼ÆËã¹ý³ÌÓÉ´®ÐÐת»»Îª²¢ÐУ¬Ò»¸öÈÎÎñ²»ÐèÒªµÈ´ýÆäËûÎ޹صÄÈÎÎñÖ´ÐÐÍêÔÚÖ´ÐУ¬Êµ¼Ê¼ÆËãÖгÌÐòµÄ²¢ÐÐÖ´Ðлá¸üÓÐÓô¦¡£
¹ØÓÚÕⲿ·Ö×ôÖ¤µÄÊý¾Ý¾Í²»ÔÚÕâ±ß¹ý¶àÐðÊöÁË£¬¸ÐÐËȤµÄͬѧ¿ÉÒÔ×Ô¼º¿´Ò»ÏÂÕâ·½ÃæµÄ×ÊÁÏ¡£±ÈÈçWeb·þÎñ¶ËÓÉRubyÇл»ÎªGoÐÔÄÜÌáÉý15±¶µÄÀϹÊÊ£¨RubyʹÓõÄÊÇÂÌÉ«Ị̈߳¬¼´Ö»ÓÐÒ»¸öCPUµÃµ½ÀûÓã©¡£ËäÈ»Õâ¸ö¹ÊÊ¿ÉÄÜÓеã¿ä´ó£¬µ«ÊDz¢ÐдøÀ´µÄÐÔÄÜÌáÉýÊǺÁÎÞÒÉÎʵġ££¨RubyÇл»ÎªGo£ºhttp://www.vaikan.com/how-we-went-from-30-servers-to-2-go/£©¡£
3. ´ÅÅÌI/O¶ÔÐÔÄܵÄÓ°Ïì
(1) ÎÊÌâ
´ÅÅ̶ÁÈ¡Êý¾Ý¿¿µÄÊÇ»úеÔ˶¯£¬Ã¿´Î¶ÁÈ¡Êý¾Ý»¨·ÑµÄʱ¼ä¿ÉÒÔ·ÖΪѰµÀʱ¼ä¡¢ÐýתÑÓ³Ù¡¢´«Êäʱ¼äÈý¸ö²¿·Ö£¬Ñ°µÀʱ¼äÖ¸µÄÊǴűÛÒƶ¯µ½Ö¸¶¨´ÅµÀËùÐèÒªµÄʱ¼ä£¬Ö÷Á÷´ÅÅÌÒ»°ãÔÚ5msÒÔÏ£»ÐýתÑÓ³Ù¾ÍÊÇÎÒÃǾ³£Ìý˵µÄ´ÅÅÌתËÙ£¬±ÈÈçÒ»¸ö´ÅÅÌ7200ת£¬±íʾÿ·ÖÖÓÄÜת7200´Î£¬Ò²¾ÍÊÇ˵1ÃëÖÓÄÜת120´Î£¬ÐýתÑÓ³Ù¾ÍÊÇ1/120/2 = 4.17ms£»´«Êäʱ¼äÖ¸µÄÊÇ´Ó´ÅÅ̶Á³ö»ò½«Êý¾ÝдÈë´ÅÅ̵Äʱ¼ä£¬Ò»°ãÔÚÁãµã¼¸ºÁÃ룬Ïà¶ÔÓÚÇ°Á½¸öʱ¼ä¿ÉÒÔºöÂÔ²»¼Æ¡£ÄÇô·ÃÎÊÒ»´Î´ÅÅ̵Äʱ¼ä£¬¼´Ò»´Î´ÅÅÌI/OµÄʱ¼äÔ¼µÈÓÚ9ms£¨5ms+4.17ms£©×óÓÒ£¬ÌýÆðÀ´»¹Í¦²»´íµÄ£¬µ«ÒªÖªµÀһ̨500 -MIPSµÄ»úÆ÷ÿÃë¿ÉÒÔÖ´ÐÐ5ÒÚÌõÖ¸ÁÒòΪָÁîÒÀ¿¿µÄÊǵçµÄÐÔÖÊ£¬»»¾ä»°ËµÖ´ÐÐÒ»´ÎI/OµÄʱ¼ä¿ÉÒÔÖ´ÐÐ40ÍòÌõÖ¸ÁÊý¾Ý¿â¶¯éüÊ®Íò°ÙÍòÄËÖÁǧÍò¼¶Êý¾Ý£¬Ã¿´Î9ºÁÃëµÄʱ¼ä£¬ÏÔÈ»ÊǸöÔÖÄÑ¡£
(2) ½â¾ö°ì·¨
´ÅÅÌI/O¶Ô·þÎñÆ÷ÐÔÄܵÄÓ°ÏìûÓиù±¾µÄ½â¾ö°ì·¨£¬³ý·ÇÄã°Ñ´ÅÅÌÈÓµô£¬»»³É±ðµÄ¶«Î÷¡£ÎÒÃÇÄÜÔÚÍøÉÏËѵ½¸÷ÖÖ´æ´¢½éÖʵÄÏìÓ¦ËÙ¶ÈÓë¼Û¸ñ£¬Èç¹ûÄãÓÐÇ®£¬Äã¾Í¿ÉÒÔÈÎÐԵĸü»»´æ´¢½éÖÊ¡£
ÔÚ²»¸ü»»´æ´¢½éÖʵÄÌõ¼þÏ£¬ÎÒÃÇ¿ÉÒÔ¼õÉÙÓ¦ÓóÌÐò¶Ô´ÅÅ̵ķÃÎÊ´ÎÊý£¬±ÈÈçÉèÖûº´æ£¬»¹¿ÉÒ԰Ѳ¿·Ö´ÅÅÌI/O·Åµ½ÇëÇóÖÜÆÚÍ⣬±ÈÈçÓöÓÁкÍÕ»À´´¦ÀíÊý¾ÝµÄI/OµÈ¡£
4. ÓÅ»¯Êý¾Ý¿â²éѯ
Ëæ×ÅÒµÎñ¿ª·¢Ä£Ê½µÄ±ä»¯£¬Ãô½Ýʽ¿ª·¢±»Ô½À´Ô½¶àµÄÍŶӲÉÓã¬ÖÜÆÚÔ½À´Ô½¶Ì£¬ºÜ¶àÊý¾Ý¿â²éѯÓï¾ä¶¼ÊÇ°´ÕÕÒµÎñÂß¼À´Ð´£¬Ê±¼ä¾ÃÁ˳£³£¾ÍºöÂÔÁËSQL²éѯµÄ¸ñʽÎÊÌ⣬Ôì³ÉÊý¾Ý¿âѹÁ¦µÄÔö¼Ó£¬Ê¹Êý¾Ý¿â²éѯµÄÏìÓ¦±äÂý¡£ÕâÀï¼òµ¥½éÉÜMySQLÊý¾Ý¿âÖУ¬¼¸Ìõ±»ÎÒÃǺöÂԵij£¼ûÎÊÌâºÍÓÅ»¯·½Ê½£º
×î×óǰ׺ƥÅäÔÔò£¬·Ç³£ÖØÒªµÄÔÔò£¬MySQL»áÒ»Ö±ÏòÓÒÆ¥ÅäÖ±µ½Óöµ½·¶Î§²éѯ(>¡¢<¡¢between¡¢like)¾ÍֹͣƥÅ䣬±ÈÈça = 1 and b = 2 and c > 3 and d = 4 Èç¹û½¨Á¢(a,b,c,d)˳ÐòµÄË÷Òý£¬dÊÇÓò»µ½Ë÷ÒýµÄ£¬Èç¹û½¨Á¢(a,b,d,c)µÄË÷ÒýÔò¶¼¿ÉÒÔÓõ½£¬a,b,dµÄ˳Ðò¿ÉÒÔÈÎÒâµ÷Õû¡£
¾¡Á¿Ñ¡ÔñÇø·Ö¶È¸ßµÄÁÐ×÷ΪË÷Òý£¬Çø·Ö¶ÈµÄ¹«Ê½ÊÇcount(distinct col)/count(*)£¬±íʾ×ֶβ»Öظ´µÄ±ÈÀý£¬±ÈÀýÔ½´óÎÒÃÇɨÃèµÄ¼Ç¼ÊýÔ½ÉÙ£¬Î¨Ò»¼üµÄÇø·Ö¶ÈÊÇ1£¬¶øһЩ״̬¡¢ÐÔ±ð×ֶοÉÄÜÔÚ´óÊý¾ÝÃæÇ°Çø·Ö¶È¾ÍÊÇ0£¬ÄÇ¿ÉÄÜÓÐÈË»áÎÊ£¬Õâ¸ö±ÈÀýÓÐʲô¾ÑéÖµÂð£¿Ê¹Óó¡¾°²»Í¬£¬Õâ¸öÖµÒ²ºÜÄÑÈ·¶¨£¬Ò»°ãÐèÒªjoinµÄ×Ö¶ÎÎÒÃǶ¼ÒªÇóÊÇ0.1ÒÔÉÏ£¬¼´Æ½¾ù1ÌõɨÃè10Ìõ¼Ç¼¡£
¾¡Á¿Ê¹ÓÃÊý×ÖÐÍ×ֶΣ¬ÈôÖ»º¬ÊýÖµÐÅÏ¢µÄ×ֶξ¡Á¿²»ÒªÉè¼ÆΪ×Ö·ûÐÍ£¬Õâ»á½µµÍ²éѯºÍÁ¬½ÓµÄÐÔÄÜ£¬²¢»áÔö¼Ó´æ´¢¿ªÏú¡£ÕâÊÇÒòΪÒýÇæÔÚ´¦Àí²éѯºÍÁ¬½Óʱ»áÖð¸ö±È½Ï×Ö·û´®ÖÐÿһ¸ö×Ö·û£¬¶ø¶ÔÓÚÊý×ÖÐͶøÑÔÖ»ÐèÒª±È½ÏÒ»´Î¾Í¹»ÁË¡£
Ë÷ÒýÁв»ÄܲÎÓë¼ÆË㣬±£³ÖÁС°¸É¾»¡±£¬±ÈÈçfrom_unixtime(create_time) = ¡¯2014-05-29¡¯¾Í²»ÄÜʹÓõ½Ë÷Òý£¬ÔÒòºÜ¼òµ¥£¬b+Ê÷ÖдæµÄ¶¼ÊÇÊý¾Ý±íÖеÄ×Ö¶ÎÖµ£¬µ«½øÐмìË÷ʱ£¬ÐèÒª°ÑËùÓÐÔªËض¼Ó¦Óú¯Êý²ÅÄܱȽϣ¬ÏÔÈ»³É±¾Ì«´ó¡£ËùÒÔÓï¾äÓ¦¸Ãд³Écreate_time = unix_timestamp(¡¯2014-05-29¡¯);Ó¦¾¡Á¿±ÜÃâÔÚ where ×Ó¾äÖжÔ×ֶνøÐÐ null ÖµÅжϣ¬·ñÔò½«µ¼ÖÂÒýÇæ·ÅÆúʹÓá£
Ë÷Òý¶ø½øÐÐÈ«±íɨÃ裬È磺
select id from t where num is null
¿ÉÒÔÔÚnumÉÏÉèÖÃĬÈÏÖµ0£¬È·±£±íÖÐnumÁÐûÓÐnullÖµ£¬È»ºóÕâÑù²éѯ£º
select id from t where num=0
Ó¦¾¡Á¿±ÜÃâÔÚ where ×Ó¾äÖÐʹÓà or À´Á´½ÓÌõ¼þ£¬·ñÔò½«µ¼ÖÂÒýÇæ·ÅÆúʹÓÃË÷Òý¶ø½øÐÐÈ«±íɨÃ裬È磺
select id from t where num=10 or num=20
¿ÉÒÔÕâÑù²éѯ£º
select id from t where num=10 union all select id from t where num=20
ÏÂÃæµÄ²éѯҲ½«µ¼ÖÂÈ«±íɨÃè(²»ÄÜÇ°ÖðٷֺÅ)£º
select id from t where name like ¡®%abc%¡¯
ÈôÒªÌá¸ßЧÂÊ£¬¿ÉÒÔ¿¼ÂÇÈ«ÎļìË÷¡£
in ºÍ not in Ò²ÒªÉ÷Ó㬷ñÔò»áµ¼ÖÂÈ«±íɨÃ裬È磺
select id from t where num in(1,2,3)
¶ÔÓÚÁ¬ÐøµÄÊýÖµ£¬ÄÜÓà between ¾Í²»ÒªÓà in ÁË£º
select id from t where num between 1 and 3
±¾ÎÄתÔØ×Ô£ºhttp://www.linuxprobe.com/web-apache.html
Ãâ·ÑÌṩ×îÐÂLinux¼¼Êõ½Ì³ÌÊé¼®£¬Îª¿ªÔ´¼¼Êõ°®ºÃÕßŬÁ¦×öµÃ¸ü¶à¸üºÃ£¬¿ªÔ´Õ¾µã£ºhttp://www.linuxprobe.com/
Ronny ÓÚ 2017-09-08 10:06:05·¢±í:
°ì·¨ÌرðºÃ£¡ÊÜÓÃÁË