1. ÓÅ»¯ÄãµÄMySQL²éѯ»º´æ
ÔÚMySQL·þÎñÆ÷ÉϽøÐвéѯ£¬¿ÉÒÔÆôÓøßËÙ²éѯ»º´æ¡£ÈÃÊý¾Ý¿âÒýÇæÔÚºǫ́ÇÄÇĵĴ¦ÀíÊÇÌá¸ßÐÔÄܵÄ×îÓÐЧ·½·¨Ö®Ò»¡£µ±Í¬Ò»¸ö²éѯ±»Ö´Ðжà´Îʱ£¬Èç¹û½á¹ûÊÇ´Ó»º´æÖÐÌáÈ¡£¬ÄÇÊÇÏ൱¿ìµÄ¡£
µ«Ö÷ÒªµÄÎÊÌâÊÇ£¬ËüÊÇÄÇôÈÝÒ×±»Òþ²ØÆðÀ´ÒÔÖÁÓÚÎÒÃÇ´ó¶àÊý³ÌÐòÔ±»áºöÂÔËü¡£ÔÚÓÐЩ´¦ÀíÈÎÎñÖУ¬ÎÒÃÇʵ¼ÊÉÏÊÇ¿ÉÒÔ×èÖ¹²éѯ»º´æ¹¤×÷µÄ¡£
1. // query cache does NOT work
2. $r = mysql_query("SELECT username FROM user WHERE signup_date >= CURDATE()");
3.
4. // query cache works!
5. $today = date("Y-m-d");
6. $r = mysql_query("SELECT username FROM user WHERE signup_date >= '$today'");
7.
8. // query cache does NOT work
9. $r = mysql_query("SELECT username FROM user WHERE signup_date >= CURDATE()");
10.
11. // query cache works!
12. $today = date("Y-m-d");
13. $r = mysql_query("SELECT username FROM user WHERE signup_date >= '$today'");
2. ÓÃEXPLAINʹÄãµÄSELECT²éѯ¸ü¼ÓÇåÎú
ʹÓÃEXPLAIN¹Ø¼ü×ÖÊÇÁíÒ»¸öMySQLÓÅ»¯¼¼ÇÉ£¬¿ÉÒÔÈÃÄãÁ˽âMySQLÕýÔÚ½øÐÐʲôÑùµÄ²éѯ²Ù×÷£¬Õâ¿ÉÒÔ°ïÖúÄã·¢ÏÖÆ¿¾±µÄËùÔÚ£¬²¢ÏÔʾ³ö²éѯ»ò±í½á¹¹ÔÚÄÄÀï³öÁËÎÊÌâ¡£
EXPLAIN²éѯµÄ½á¹û£¬¿ÉÒÔ¸æËßÄãÄÇЩË÷ÒýÕýÔÚ±»ÒýÓ㬱íÊÇÈçºÎ±»É¨ÃèºÍÅÅÐòµÄµÈµÈ¡£
ʵÏÖÒ»¸öSELECT²éѯ£¨×îºÃÊDZȽϸ´ÔÓµÄÒ»¸ö£¬´øjoins·½Ê½µÄ£©£¬ÔÚÀïÃæÌí¼ÓÉÏÄãµÄ¹Ø¼ü´Ê½âÊÍ£¬ÔÚÕâÀïÎÒÃÇ¿ÉÒÔʹÓÃphpMyAdmin£¬ Ëû»á¸æËßÄã±íÖеĽá¹û¡£¾ÙÀýÀ´Ëµ£¬¼ÙÈçµ±ÎÒÔÚÖ´ÐÐjoinsʱ£¬ÕýÍü¼ÇÍùÒ»¸öË÷ÒýÖÐÌí¼ÓÁУ¬EXPLAINÄÜ°ïÖúÎÒÕÒµ½ÎÊÌâµÄËùÔÚ¡£
Ìí¼ÓË÷Òýµ½group_id fieldºó
3. ÀûÓÃLIMIT 1È¡µÃΨһÐÐ
ÓÐʱ£¬µ±ÄãÒª²éѯһÕűíÊÇ£¬ÄãÖªµÀ×Ô¼ºÖ»ÐèÒª¿´Ò»ÐС£Äã¿ÉÄÜ»áÈ¥µÄÒ»ÌõÊ®·Ö¶ÀÌصļǼ£¬»òÕßÖ»ÊǸպüì²éÁËÈκδæÔڵļǼÊý£¬ËûÃǶ¼Âú×ãÁËÄãµÄWHERE×Ӿ䡣
ÔÚÕâÖÖÇé¿öÏ£¬Ôö¼ÓÒ»¸öLIMIT 1»áÁîÄãµÄ²éѯ¸ü¼ÓÓÐЧ¡£ÕâÑùÊý¾Ý¿âÒýÇæ·¢ÏÖÖ»ÓÐ1ºó½«Í£Ö¹É¨Ã裬¶ø²»ÊÇȥɨÃèÕû¸ö±í»òË÷Òý¡£¡¢
1. // do I have any users from Alabama?
2. // what NOT to do:
3. $r = mysql_query("SELECT * FROM user WHERE state = 'Alabama'");
4. if (mysql_num_rows($r) > 0) {
5. // ...
6. }
7. // much better:
8. $r = mysql_query("SELECT 1 FROM user WHERE state = 'Alabama' LIMIT 1");
9. if (mysql_num_rows($r) > 0) {
10. // ...
11. }
4. Ë÷ÒýÖеļìË÷×Ö¶Î
Ë÷Òý²»½öÊÇÖ÷¼ü»òΨһ¼ü¡£Èç¹ûÄãÏëËÑË÷±íÖеÄÈκÎÁУ¬ÄãÓ¦¸ÃÒ»Ö±Ö¸ÏòË÷Òý¡£
5. ±£Ö¤Á¬½ÓµÄË÷ÒýÊÇÏàͬµÄÀàÐÍ
Èç¹ûÓ¦ÓóÌÐòÖаüº¬¶à¸öÁ¬½Ó²éѯ£¬ÄãÐèҪȷ±£ÄãÁ´½ÓµÄÁÐÔÚÁ½±ßµÄ±íÉ϶¼±»Ë÷Òý¡£Õâ»áÓ°ÏìMySQLÈçºÎÓÅ»¯ÄÚ²¿Áª½Ó²Ù×÷¡£
´ËÍ⣬¼ÓÈëµÄÁУ¬±ØÐëÊÇͬһÀàÐÍ¡£ÀýÈ磬Äã¼ÓÈëÒ»¸öDECIMALÁУ¬¶øͬʱ¼ÓÈëÁíÒ»¸ö±íÖеÄintÁУ¬MySQL½«ÎÞ·¨Ê¹ÓÃÆäÖÐÖÁÉÙÒ»¸öÖ¸±ê¡£¼´Ê¹×Ö·û±àÂë±ØÐëͬΪ×Ö·û´®ÀàÐÍ¡£
1. // looking for companies in my state
2. $r = mysql_query("SELECT company_name FROM users
3. LEFT JOIN companies ON (users.state = companies.state)
4. WHERE users.id = $user_id");
5.
6. // both state columns should be indexed
7. // and they both should be the same type and character encoding
8. // or MySQL might do full table scans
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿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¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ