{"id":870,"date":"2013-07-31T10:21:37","date_gmt":"2013-07-31T02:21:37","guid":{"rendered":"http:\/\/hesiwei.cn\/?p=870"},"modified":"2013-07-31T10:21:37","modified_gmt":"2013-07-31T02:21:37","slug":"mysql%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e7%9a%84%e6%9c%80%e4%bd%b320%e6%9d%a1%e7%bb%8f%e9%aa%8c","status":"publish","type":"post","link":"http:\/\/hesiwei.cn\/?p=870","title":{"rendered":"MySQL\u6027\u80fd\u4f18\u5316\u7684\u6700\u4f7320+\u6761\u7ecf\u9a8c"},"content":{"rendered":"<p align=\"center\"><span style=\"font-size: small; font-family: Arial;\">\u539f\u6587\uff1a<\/span><a style=\"font-size: small; font-family: Arial;\" href=\"http:\/\/net.tutsplus.com\/tutorials\/other\/top-20-mysql-best-practices\/\" target=\"_blank\" rel=\"noopener noreferrer\">tutsplus<\/a><span style=\"font-size: small; font-family: Arial;\">\u00a0\u8bd1\u6587\uff1a<\/span><a style=\"font-size: small; font-family: Arial;\" href=\"http:\/\/coolshell.cn\/articles\/1846.html\" target=\"_blank\" rel=\"noopener noreferrer\">\u9177\u58f3<\/a><\/p>\n<p>\u4eca\u5929\uff0c\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u8d8a\u6765\u8d8a\u6210\u4e3a\u6574\u4e2a\u5e94\u7528\u7684\u6027\u80fd\u74f6\u9888\u4e86\uff0c\u8fd9\u70b9\u5bf9\u4e8eWeb\u5e94\u7528\u5c24\u5176\u660e\u663e\u3002\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6027\u80fd\uff0c\u8fd9\u5e76\u4e0d\u53ea\u662fDBA\u624d\u9700\u8981\u62c5\u5fc3\u7684\u4e8b\uff0c\u800c\u8fd9\u66f4\u662f\u6211\u4eec\u7a0b\u5e8f\u5458\u9700\u8981\u53bb\u5173\u6ce8\u7684\u4e8b\u60c5\u3002\u5f53\u6211\u4eec\u53bb\u8bbe\u8ba1\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff0c\u5bf9\u64cd\u4f5c\u6570\u636e\u5e93\u65f6\uff08\u5c24\u5176\u662f\u67e5\u8868\u65f6\u7684SQL\u8bed\u53e5\uff09\uff0c\u6211\u4eec\u90fd\u9700\u8981\u6ce8\u610f\u6570\u636e\u64cd\u4f5c\u7684\u6027\u80fd\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u4e0d\u4f1a\u8bb2\u8fc7\u591a\u7684SQL\u8bed\u53e5\u7684\u4f18\u5316\uff0c\u800c\u53ea\u662f\u9488\u5bf9MySQL\u8fd9\u4e00Web\u5e94\u7528\u6700\u591a\u7684\u6570\u636e\u5e93\u3002\u5e0c\u671b\u4e0b\u9762\u7684\u8fd9\u4e9b\u4f18\u5316\u6280\u5de7\u5bf9\u4f60\u6709\u7528\u3002<br \/>\n<!--more--><br \/>\n<strong>1. \u4e3a\u67e5\u8be2\u7f13\u5b58\u4f18\u5316\u4f60\u7684\u67e5\u8be2<\/strong><\/p>\n<p>\u5927\u591a\u6570\u7684MySQL\u670d\u52a1\u5668\u90fd\u5f00\u542f\u4e86\u67e5\u8be2\u7f13\u5b58\u3002\u8fd9\u662f\u63d0\u9ad8\u6027\u6700\u6709\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u800c\u4e14\u8fd9\u662f\u88abMySQL\u7684\u6570\u636e\u5e93\u5f15\u64ce\u5904\u7406\u7684\u3002\u5f53\u6709\u5f88\u591a\u76f8\u540c\u7684\u67e5\u8be2\u88ab\u6267\u884c\u4e86\u591a\u6b21\u7684\u65f6\u5019\uff0c\u8fd9\u4e9b\u67e5\u8be2\u7ed3\u679c\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u7f13\u5b58\u4e2d\uff0c\u8fd9\u6837\uff0c\u540e\u7eed\u7684\u76f8\u540c\u7684\u67e5\u8be2\u5c31\u4e0d\u7528\u64cd\u4f5c\u8868\u800c\u76f4\u63a5\u8bbf\u95ee\u7f13\u5b58\u7ed3\u679c\u4e86\u3002<\/p>\n<p>\u8fd9\u91cc\u6700\u4e3b\u8981\u7684\u95ee\u9898\u662f\uff0c\u5bf9\u4e8e\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u8fd9\u4e2a\u4e8b\u60c5\u662f\u5f88\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u3002\u56e0\u4e3a\uff0c\u6211\u4eec\u67d0\u4e9b\u67e5\u8be2\u8bed\u53e5\u4f1a\u8ba9MySQL\u4e0d\u4f7f\u7528\u7f13\u5b58\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u793a\u4f8b\uff1a<\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=%2F%2F%20%E6%9F%A5%E8%AF%A2%E7%BC%93%E5%AD%98%E4%B8%8D%E5%BC%80%E5%90%AF%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20WHERE%20signup_date%20%26gt%3B%3D%20CURDATE()%22)%3B%0A%20%0A%2F%2F%20%E5%BC%80%E5%90%AF%E6%9F%A5%E8%AF%A2%E7%BC%93%E5%AD%98%0A%24today%20%3D%20date(%22Y-m-d%22)%3B%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20WHERE%20signup_date%20%26gt%3B%3D%20'%24today'%22)%3B%0A\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=%2F%2F%20%E6%9F%A5%E8%AF%A2%E7%BC%93%E5%AD%98%E4%B8%8D%E5%BC%80%E5%90%AF%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20WHERE%20signup_date%20%26gt%3B%3D%20CURDATE()%22)%3B%0A%20%0A%2F%2F%20%E5%BC%80%E5%90%AF%E6%9F%A5%E8%AF%A2%E7%BC%93%E5%AD%98%0A%24today%20%3D%20date(%22Y-m-d%22)%3B%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20WHERE%20signup_date%20%26gt%3B%3D%20'%24today'%22)%3B%0A\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>\/\/ \u67e5\u8be2\u7f13\u5b58\u4e0d\u5f00\u542f<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT username FROM user WHERE signup_date &gt;= CURDATE()&#8221;);<\/li>\n<li><\/li>\n<li>\/\/ \u5f00\u542f\u67e5\u8be2\u7f13\u5b58<\/li>\n<li>$today\u00a0=\u00a0date(&#8220;Y-m-d&#8221;);<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT username FROM user WHERE signup_date &gt;= &#8216;$today'&#8221;);<\/li>\n<\/ol>\n<\/div>\n<p>\u4e0a\u9762\u4e24\u6761SQL\u8bed\u53e5\u7684\u5dee\u522b\u5c31\u662f CURDATE() \uff0cMySQL\u7684\u67e5\u8be2\u7f13\u5b58\u5bf9\u8fd9\u4e2a\u51fd\u6570\u4e0d\u8d77\u4f5c\u7528\u3002\u6240\u4ee5\uff0c\u50cf NOW() \u548c RAND() \u6216\u662f\u5176\u5b83\u7684\u8bf8\u5982\u6b64\u7c7b\u7684SQL\u51fd\u6570\u90fd\u4e0d\u4f1a\u5f00\u542f\u67e5\u8be2\u7f13\u5b58\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u51fd\u6570\u7684\u8fd4\u56de\u662f\u4f1a\u4e0d\u5b9a\u7684\u6613\u53d8\u7684\u3002\u6240\u4ee5\uff0c\u4f60\u6240\u9700\u8981\u7684\u5c31\u662f\u7528\u4e00\u4e2a\u53d8\u91cf\u6765\u4ee3\u66ffMySQL\u7684\u51fd\u6570\uff0c\u4ece\u800c\u5f00\u542f\u7f13\u5b58\u3002<\/p>\n<p><strong>2. EXPLAIN \u4f60\u7684 SELECT \u67e5\u8be2<\/strong><\/p>\n<p>\u4f7f\u7528 EXPLAIN \u5173\u952e\u5b57\u53ef\u4ee5\u8ba9\u4f60\u77e5\u9053MySQL\u662f\u5982\u4f55\u5904\u7406\u4f60\u7684SQL\u8bed\u53e5\u7684\u3002\u8fd9\u53ef\u4ee5\u5e2e\u4f60\u5206\u6790\u4f60\u7684\u67e5\u8be2\u8bed\u53e5\u6216\u662f\u8868\u7ed3\u6784\u7684\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p>EXPLAIN \u7684\u67e5\u8be2\u7ed3\u679c\u8fd8\u4f1a\u544a\u8bc9\u4f60\u4f60\u7684\u7d22\u5f15\u4e3b\u952e\u88ab\u5982\u4f55\u5229\u7528\u7684\uff0c\u4f60\u7684\u6570\u636e\u8868\u662f\u5982\u4f55\u88ab\u641c\u7d22\u548c\u6392\u5e8f\u7684\u2026\u2026\u7b49\u7b49\uff0c\u7b49\u7b49\u3002<\/p>\n<p>\u6311\u4e00\u4e2a\u4f60\u7684SELECT\u8bed\u53e5\uff08\u63a8\u8350\u6311\u9009\u90a3\u4e2a\u6700\u590d\u6742\u7684\uff0c\u6709\u591a\u8868\u8054\u63a5\u7684\uff09\uff0c\u628a\u5173\u952e\u5b57EXPLAIN\u52a0\u5230\u524d\u9762\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528phpmyadmin\u6765\u505a\u8fd9\u4e2a\u4e8b\u3002\u7136\u540e\uff0c\u4f60\u4f1a\u770b\u5230\u4e00\u5f20\u8868\u683c\u3002\u4e0b\u9762\u7684\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5fd8\u8bb0\u52a0\u4e0a\u4e86group_id\u7d22\u5f15\uff0c\u5e76\u4e14\u6709\u8868\u8054\u63a5\uff1a<\/p>\n<p>&lt;ignore_js_op&gt;<img decoding=\"async\" id=\"aimg_6300\" title=\"unoptimized_explain.jpg\" alt=\"unoptimized_explain.jpg\" src=\"http:\/\/devbbs-discuzx.stor.sinaapp.com\/data\/attachment\/forum\/201307\/30\/162654zg7xyhxhzbdxhkk1.jpg\" width=\"540\" \/><\/p>\n<p>\u5f53\u6211\u4eec\u4e3a group_id \u5b57\u6bb5\u52a0\u4e0a\u7d22\u5f15\u540e\uff1a<br \/>\n&lt;ignore_js_op&gt;<img decoding=\"async\" id=\"aimg_6301\" title=\"optimized_explain2.jpg\" alt=\"optimized_explain2.jpg\" src=\"http:\/\/devbbs-discuzx.stor.sinaapp.com\/data\/attachment\/forum\/201307\/30\/162753l65t7j5hfjglhf7d.jpg\" width=\"540\" \/><br \/>\n\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u524d\u4e00\u4e2a\u7ed3\u679c\u663e\u793a\u641c\u7d22\u4e86 7883 \u884c\uff0c\u800c\u540e\u4e00\u4e2a\u53ea\u662f\u641c\u7d22\u4e86\u4e24\u4e2a\u8868\u7684 9 \u548c 16 \u884c\u3002\u67e5\u770brows\u5217\u53ef\u4ee5\u8ba9\u6211\u4eec\u627e\u5230\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<p><strong>3. \u5f53\u53ea\u8981\u4e00\u884c\u6570\u636e\u65f6\u4f7f\u7528 LIMIT 1<\/strong><\/p>\n<p>\u5f53\u4f60\u67e5\u8be2\u8868\u7684\u6709\u4e9b\u65f6\u5019\uff0c\u4f60\u5df2\u7ecf\u77e5\u9053\u7ed3\u679c\u53ea\u4f1a\u6709\u4e00\u6761\u7ed3\u679c\uff0c\u4f46\u56e0\u4e3a\u4f60\u53ef\u80fd\u9700\u8981\u53bbfetch\u6e38\u6807\uff0c\u6216\u662f\u4f60\u4e5f\u8bb8\u4f1a\u53bb\u68c0\u67e5\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u52a0\u4e0a LIMIT 1 \u53ef\u4ee5\u589e\u52a0\u6027\u80fd\u3002\u8fd9\u6837\u4e00\u6837\uff0cMySQL\u6570\u636e\u5e93\u5f15\u64ce\u4f1a\u5728\u627e\u5230\u4e00\u6761\u6570\u636e\u540e\u505c\u6b62\u641c\u7d22\uff0c\u800c\u4e0d\u662f\u7ee7\u7eed\u5f80\u540e\u67e5\u5c11\u4e0b\u4e00\u6761\u7b26\u5408\u8bb0\u5f55\u7684\u6570\u636e\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\uff0c\u53ea\u662f\u4e3a\u4e86\u627e\u4e00\u4e0b\u662f\u5426\u6709\u201c\u4e2d\u56fd\u201d\u7684\u7528\u6237\uff0c\u5f88\u660e\u663e\uff0c\u540e\u9762\u7684\u4f1a\u6bd4\u524d\u9762\u7684\u66f4\u6709\u6548\u7387\u3002\uff08\u8bf7\u6ce8\u610f\uff0c\u7b2c\u4e00\u6761\u4e2d\u662fSelect *\uff0c\u7b2c\u4e8c\u6761\u662fSelect 1\uff09<\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=%2F%2F%20%E6%B2%A1%E6%9C%89%E6%95%88%E7%8E%87%E7%9A%84%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%20*%20FROM%20user%20WHERE%20country%20%3D%20'China'%22)%3B%0Aif%20(mysql_num_rows(%24r)%20%26gt%3B%200)%20%7B%0A%26nbsp%3B%26nbsp%3B%2F%2F%20...%0A%7D%0A%20%0A%2F%2F%20%E6%9C%89%E6%95%88%E7%8E%87%E7%9A%84%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%201%20FROM%20user%20WHERE%20country%20%3D%20'China'%20LIMIT%201%22)%3B%0Aif%20(mysql_num_rows(%24r)%20%26gt%3B%200)%20%7B%0A%26nbsp%3B%26nbsp%3B%2F%2F%20...%0A%7D\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=%2F%2F%20%E6%B2%A1%E6%9C%89%E6%95%88%E7%8E%87%E7%9A%84%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%20*%20FROM%20user%20WHERE%20country%20%3D%20'China'%22)%3B%0Aif%20(mysql_num_rows(%24r)%20%26gt%3B%200)%20%7B%0A%26nbsp%3B%26nbsp%3B%2F%2F%20...%0A%7D%0A%20%0A%2F%2F%20%E6%9C%89%E6%95%88%E7%8E%87%E7%9A%84%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%201%20FROM%20user%20WHERE%20country%20%3D%20'China'%20LIMIT%201%22)%3B%0Aif%20(mysql_num_rows(%24r)%20%26gt%3B%200)%20%7B%0A%26nbsp%3B%26nbsp%3B%2F%2F%20...%0A%7D\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>\/\/ \u6ca1\u6709\u6548\u7387\u7684\uff1a<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT * FROM user WHERE country = &#8216;China'&#8221;);<\/li>\n<li>if\u00a0(mysql_num_rows($r) &gt; 0) {<\/li>\n<li>\u00a0\u00a0\/\/ &#8230;<\/li>\n<li>}<\/li>\n<li><\/li>\n<li>\/\/ \u6709\u6548\u7387\u7684\uff1a<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT 1 FROM user WHERE country = &#8216;China&#8217; LIMIT 1&#8221;);<\/li>\n<li>if\u00a0(mysql_num_rows($r) &gt; 0) {<\/li>\n<li>\u00a0\u00a0\/\/ &#8230;<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<p><strong>4. \u4e3a\u641c\u7d22\u5b57\u6bb5\u5efa\u7d22\u5f15<\/strong><\/p>\n<p>\u7d22\u5f15\u5e76\u4e0d\u4e00\u5b9a\u5c31\u662f\u7ed9\u4e3b\u952e\u6216\u662f\u552f\u4e00\u7684\u5b57\u6bb5\u3002\u5982\u679c\u5728\u4f60\u7684\u8868\u4e2d\uff0c\u6709\u67d0\u4e2a\u5b57\u6bb5\u4f60\u603b\u8981\u4f1a\u7ecf\u5e38\u7528\u6765\u505a\u641c\u7d22\uff0c\u90a3\u4e48\uff0c\u8bf7\u4e3a\u5176\u5efa\u7acb\u7d22\u5f15\u5427\u3002<\/p>\n<p>&lt;ignore_js_op&gt;<img decoding=\"async\" id=\"aimg_6302\" title=\"search_index.jpg\" alt=\"search_index.jpg\" src=\"http:\/\/devbbs-discuzx.stor.sinaapp.com\/data\/attachment\/forum\/201307\/30\/162957ibnnqicjqbb3qeq9.jpg\" width=\"580\" \/><\/p>\n<p>\u4ece\u4e0a\u56fe\u4f60\u53ef\u4ee5\u770b\u5230\u90a3\u4e2a\u641c\u7d22\u5b57\u4e32 \u201clast_name LIKE \u2018a%\u2019\u201d\uff0c\u4e00\u4e2a\u662f\u5efa\u4e86\u7d22\u5f15\uff0c\u4e00\u4e2a\u662f\u6ca1\u6709\u7d22\u5f15\uff0c\u6027\u80fd\u5dee\u4e864\u500d\u5de6\u53f3\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u4f60\u5e94\u8be5\u4e5f\u9700\u8981\u77e5\u9053\u4ec0\u4e48\u6837\u7684\u641c\u7d22\u662f\u4e0d\u80fd\u4f7f\u7528\u6b63\u5e38\u7684\u7d22\u5f15\u7684\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u5728\u4e00\u7bc7\u5927\u7684\u6587\u7ae0\u4e2d\u641c\u7d22\u4e00\u4e2a\u8bcd\u65f6\uff0c\u5982\uff1a \u201cWHERE post_content LIKE \u2018%apple%\u2019\u201d\uff0c\u7d22\u5f15\u53ef\u80fd\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528MySQL\u5168\u6587\u7d22\u5f15 \u6216\u662f\u81ea\u5df1\u505a\u4e00\u4e2a\u7d22\u5f15\uff08\u6bd4\u5982\u8bf4\uff1a\u641c\u7d22\u5173\u952e\u8bcd\u6216\u662fTag\u4ec0\u4e48\u7684\uff09<\/p>\n<p><strong>5. \u5728Join\u8868\u7684\u65f6\u5019\u4f7f\u7528\u76f8\u5f53\u7c7b\u578b\u7684\u4f8b\uff0c\u5e76\u5c06\u5176\u7d22\u5f15<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u6709\u5f88\u591a JOIN \u67e5\u8be2\uff0c\u4f60\u5e94\u8be5\u786e\u8ba4\u4e24\u4e2a\u8868\u4e2dJoin\u7684\u5b57\u6bb5\u662f\u88ab\u5efa\u8fc7\u7d22\u5f15\u7684\u3002\u8fd9\u6837\uff0cMySQL\u5185\u90e8\u4f1a\u542f\u52a8\u4e3a\u4f60\u4f18\u5316Join\u7684SQL\u8bed\u53e5\u7684\u673a\u5236\u3002<\/p>\n<p>\u800c\u4e14\uff0c\u8fd9\u4e9b\u88ab\u7528\u6765Join\u7684\u5b57\u6bb5\uff0c\u5e94\u8be5\u662f\u76f8\u540c\u7684\u7c7b\u578b\u7684\u3002\u4f8b\u5982\uff1a\u5982\u679c\u4f60\u8981\u628a DECIMAL \u5b57\u6bb5\u548c\u4e00\u4e2a INT \u5b57\u6bb5Join\u5728\u4e00\u8d77\uff0cMySQL\u5c31\u65e0\u6cd5\u4f7f\u7528\u5b83\u4eec\u7684\u7d22\u5f15\u3002\u5bf9\u4e8e\u90a3\u4e9bSTRING\u7c7b\u578b\uff0c\u8fd8\u9700\u8981\u6709\u76f8\u540c\u7684\u5b57\u7b26\u96c6\u624d\u884c\u3002\uff08\u4e24\u4e2a\u8868\u7684\u5b57\u7b26\u96c6\u6709\u53ef\u80fd\u4e0d\u4e00\u6837\uff09<\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=%2F%2F%20%E5%9C%A8state%E4%B8%AD%E6%9F%A5%E6%89%BEcompany%0A%24r%20%3D%20mysql_query(%22SELECT%20company_name%20FROM%20users%0A%26nbsp%3B%26nbsp%3BLEFT%20JOIN%20companies%20ON%20(users.state%20%3D%20companies.state)%0A%26nbsp%3B%26nbsp%3BWHERE%20users.id%20%3D%20%24user_id%22)%3B%0A%20%0A%2F%2F%20%E4%B8%A4%E4%B8%AA%20state%20%E5%AD%97%E6%AE%B5%E5%BA%94%E8%AF%A5%E6%98%AF%E8%A2%AB%E5%BB%BA%E8%BF%87%E7%B4%A2%E5%BC%95%E7%9A%84%EF%BC%8C%E8%80%8C%E4%B8%94%E5%BA%94%E8%AF%A5%E6%98%AF%E7%9B%B8%E5%BD%93%E7%9A%84%E7%B1%BB%E5%9E%8B%EF%BC%8C%E7%9B%B8%E5%90%8C%E7%9A%84%E5%AD%97%E7%AC%A6%E9%9B%86%E3%80%82\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=%2F%2F%20%E5%9C%A8state%E4%B8%AD%E6%9F%A5%E6%89%BEcompany%0A%24r%20%3D%20mysql_query(%22SELECT%20company_name%20FROM%20users%0A%26nbsp%3B%26nbsp%3BLEFT%20JOIN%20companies%20ON%20(users.state%20%3D%20companies.state)%0A%26nbsp%3B%26nbsp%3BWHERE%20users.id%20%3D%20%24user_id%22)%3B%0A%20%0A%2F%2F%20%E4%B8%A4%E4%B8%AA%20state%20%E5%AD%97%E6%AE%B5%E5%BA%94%E8%AF%A5%E6%98%AF%E8%A2%AB%E5%BB%BA%E8%BF%87%E7%B4%A2%E5%BC%95%E7%9A%84%EF%BC%8C%E8%80%8C%E4%B8%94%E5%BA%94%E8%AF%A5%E6%98%AF%E7%9B%B8%E5%BD%93%E7%9A%84%E7%B1%BB%E5%9E%8B%EF%BC%8C%E7%9B%B8%E5%90%8C%E7%9A%84%E5%AD%97%E7%AC%A6%E9%9B%86%E3%80%82\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>\/\/ \u5728state\u4e2d\u67e5\u627ecompany<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT company_name FROM users<\/li>\n<li>\u00a0\u00a0LEFT JOIN companies ON (users.state = companies.state)<\/li>\n<li>\u00a0\u00a0WHERE users.id =\u00a0$user_id&#8221;);<\/li>\n<li><\/li>\n<li>\/\/ \u4e24\u4e2a state \u5b57\u6bb5\u5e94\u8be5\u662f\u88ab\u5efa\u8fc7\u7d22\u5f15\u7684\uff0c\u800c\u4e14\u5e94\u8be5\u662f\u76f8\u5f53\u7684\u7c7b\u578b\uff0c\u76f8\u540c\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n<\/ol>\n<\/div>\n<p><strong>6. \u5343\u4e07\u4e0d\u8981 ORDER BY RAND()<\/strong><\/p>\n<p>\u60f3\u6253\u4e71\u8fd4\u56de\u7684\u6570\u636e\u884c\uff1f\u968f\u673a\u6311\u4e00\u4e2a\u6570\u636e\uff1f\u771f\u4e0d\u77e5\u9053\u8c01\u53d1\u660e\u4e86\u8fd9\u79cd\u7528\u6cd5\uff0c\u4f46\u5f88\u591a\u65b0\u624b\u5f88\u559c\u6b22\u8fd9\u6837\u7528\u3002\u4f46\u4f60\u786e\u4e0d\u4e86\u89e3\u8fd9\u6837\u505a\u6709\u591a\u4e48\u53ef\u6015\u7684\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<p>\u5982\u679c\u4f60\u771f\u7684\u60f3\u628a\u8fd4\u56de\u7684\u6570\u636e\u884c\u6253\u4e71\u4e86\uff0c\u4f60\u6709N\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\u3002\u8fd9\u6837\u4f7f\u7528\u53ea\u8ba9\u4f60\u7684\u6570\u636e\u5e93\u7684\u6027\u80fd\u5448\u6307\u6570\u7ea7\u7684\u4e0b\u964d\u3002\u8fd9\u91cc\u7684\u95ee\u9898\u662f\uff1aMySQL\u4f1a\u4e0d\u5f97\u4e0d\u53bb\u6267\u884cRAND()\u51fd\u6570\uff08\u5f88\u8017CPU\u65f6\u95f4\uff09\uff0c\u800c\u4e14\u8fd9\u662f\u4e3a\u4e86\u6bcf\u4e00\u884c\u8bb0\u5f55\u53bb\u8bb0\u884c\uff0c\u7136\u540e\u518d\u5bf9\u5176\u6392\u5e8f\u3002\u5c31\u7b97\u662f\u4f60\u7528\u4e86Limit 1\u4e5f\u65e0\u6d4e\u4e8e\u4e8b\uff08\u56e0\u4e3a\u8981\u6392\u5e8f\uff09<\/p>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u662f\u968f\u673a\u6311\u4e00\u6761\u8bb0\u5f55<\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=%2F%2F%20%E5%8D%83%E4%B8%87%E4%B8%8D%E8%A6%81%E8%BF%99%E6%A0%B7%E5%81%9A%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20ORDER%20BY%20RAND()%20LIMIT%201%22)%3B%0A%20%0A%2F%2F%20%E8%BF%99%E8%A6%81%E4%BC%9A%E6%9B%B4%E5%A5%BD%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%20count(*)%20FROM%20user%22)%3B%0A%24d%20%3D%20mysql_fetch_row(%24r)%3B%0A%24rand%20%3D%20mt_rand(0%2C%24d%5B0%5D%20-%201)%3B%0A%20%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20LIMIT%20%24rand%2C%201%22)%3B\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=%2F%2F%20%E5%8D%83%E4%B8%87%E4%B8%8D%E8%A6%81%E8%BF%99%E6%A0%B7%E5%81%9A%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20ORDER%20BY%20RAND()%20LIMIT%201%22)%3B%0A%20%0A%2F%2F%20%E8%BF%99%E8%A6%81%E4%BC%9A%E6%9B%B4%E5%A5%BD%EF%BC%9A%0A%24r%20%3D%20mysql_query(%22SELECT%20count(*)%20FROM%20user%22)%3B%0A%24d%20%3D%20mysql_fetch_row(%24r)%3B%0A%24rand%20%3D%20mt_rand(0%2C%24d%5B0%5D%20-%201)%3B%0A%20%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20LIMIT%20%24rand%2C%201%22)%3B\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>\/\/ \u5343\u4e07\u4e0d\u8981\u8fd9\u6837\u505a\uff1a<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT username FROM user ORDER BY RAND() LIMIT 1&#8221;);<\/li>\n<li><\/li>\n<li>\/\/ \u8fd9\u8981\u4f1a\u66f4\u597d\uff1a<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT count(*) FROM user&#8221;);<\/li>\n<li>$d\u00a0= mysql_fetch_row($r);<\/li>\n<li>$rand\u00a0= mt_rand(0,$d[0] &#8211; 1);<\/li>\n<li><\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT username FROM user LIMIT $rand, 1&#8221;);<\/li>\n<\/ol>\n<\/div>\n<p><strong>7. \u907f\u514d SELECT *<\/strong><\/p>\n<p>\u4ece\u6570\u636e\u5e93\u91cc\u8bfb\u51fa\u8d8a\u591a\u7684\u6570\u636e\uff0c\u90a3\u4e48\u67e5\u8be2\u5c31\u4f1a\u53d8\u5f97\u8d8a\u6162\u3002\u5e76\u4e14\uff0c\u5982\u679c\u4f60\u7684\u6570\u636e\u5e93\u670d\u52a1\u5668\u548cWEB\u670d\u52a1\u5668\u662f\u4e24\u53f0\u72ec\u7acb\u7684\u670d\u52a1\u5668\u7684\u8bdd\uff0c\u8fd9\u8fd8\u4f1a\u589e\u52a0\u7f51\u7edc\u4f20\u8f93\u7684\u8d1f\u8f7d\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u4f60\u5e94\u8be5\u517b\u6210\u4e00\u4e2a\u9700\u8981\u4ec0\u4e48\u5c31\u53d6\u4ec0\u4e48\u7684\u597d\u7684\u4e60\u60ef\u3002<\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=%2F%2F%20%E4%B8%8D%E6%8E%A8%E8%8D%90%0A%24r%20%3D%20mysql_query(%22SELECT%20*%20FROM%20user%20WHERE%20user_id%20%3D%201%22)%3B%0A%24d%20%3D%20mysql_fetch_assoc(%24r)%3B%0Aecho%20%22Welcome%20%7B%24d%5B'username'%5D%7D%22%3B%0A%20%0A%2F%2F%20%E6%8E%A8%E8%8D%90%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20WHERE%20user_id%20%3D%201%22)%3B%0A%24d%20%3D%20mysql_fetch_assoc(%24r)%3B%0Aecho%20%22Welcome%20%7B%24d%5B'username'%5D%7D%22%3B\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=%2F%2F%20%E4%B8%8D%E6%8E%A8%E8%8D%90%0A%24r%20%3D%20mysql_query(%22SELECT%20*%20FROM%20user%20WHERE%20user_id%20%3D%201%22)%3B%0A%24d%20%3D%20mysql_fetch_assoc(%24r)%3B%0Aecho%20%22Welcome%20%7B%24d%5B'username'%5D%7D%22%3B%0A%20%0A%2F%2F%20%E6%8E%A8%E8%8D%90%0A%24r%20%3D%20mysql_query(%22SELECT%20username%20FROM%20user%20WHERE%20user_id%20%3D%201%22)%3B%0A%24d%20%3D%20mysql_fetch_assoc(%24r)%3B%0Aecho%20%22Welcome%20%7B%24d%5B'username'%5D%7D%22%3B\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>\/\/ \u4e0d\u63a8\u8350<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT * FROM user WHERE user_id = 1&#8221;);<\/li>\n<li>$d\u00a0= mysql_fetch_assoc($r);<\/li>\n<li>echo\u00a0&#8220;Welcome {$d[&#8216;username&#8217;]}&#8221;;<\/li>\n<li><\/li>\n<li>\/\/ \u63a8\u8350<\/li>\n<li>$r\u00a0= mysql_query(&#8220;SELECT username FROM user WHERE user_id = 1&#8221;);<\/li>\n<li>$d\u00a0= mysql_fetch_assoc($r);<\/li>\n<li>echo\u00a0&#8220;Welcome {$d[&#8216;username&#8217;]}&#8221;;<\/li>\n<\/ol>\n<\/div>\n<p><strong>8. \u6c38\u8fdc\u4e3a\u6bcf\u5f20\u8868\u8bbe\u7f6e\u4e00\u4e2aID<\/strong><\/p>\n<p>\u6211\u4eec\u5e94\u8be5\u4e3a\u6570\u636e\u5e93\u91cc\u7684\u6bcf\u5f20\u8868\u90fd\u8bbe\u7f6e\u4e00\u4e2aID\u505a\u4e3a\u5176\u4e3b\u952e\uff0c\u800c\u4e14\u6700\u597d\u7684\u662f\u4e00\u4e2aINT\u578b\u7684\uff08\u63a8\u8350\u4f7f\u7528UNSIGNED\uff09\uff0c\u5e76\u8bbe\u7f6e\u4e0a\u81ea\u52a8\u589e\u52a0\u7684AUTO_INCREMENT\u6807\u5fd7\u3002<\/p>\n<p>\u5c31\u7b97\u662f\u4f60 users \u8868\u6709\u4e00\u4e2a\u4e3b\u952e\u53eb \u201cemail\u201d\u7684\u5b57\u6bb5\uff0c\u4f60\u4e5f\u522b\u8ba9\u5b83\u6210\u4e3a\u4e3b\u952e\u3002\u4f7f\u7528 VARCHAR \u7c7b\u578b\u6765\u5f53\u4e3b\u952e\u4f1a\u4f7f\u7528\u5f97\u6027\u80fd\u4e0b\u964d\u3002\u53e6\u5916\uff0c\u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528\u8868\u7684ID\u6765\u6784\u9020\u4f60\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u800c\u4e14\uff0c\u5728MySQL\u6570\u636e\u5f15\u64ce\u4e0b\uff0c\u8fd8\u6709\u4e00\u4e9b\u64cd\u4f5c\u9700\u8981\u4f7f\u7528\u4e3b\u952e\uff0c\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e3b\u952e\u7684\u6027\u80fd\u548c\u8bbe\u7f6e\u53d8\u5f97\u975e\u5e38\u91cd\u8981\uff0c\u6bd4\u5982\uff0c\u96c6\u7fa4\uff0c\u5206\u533a\u2026\u2026<\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u53ea\u6709\u4e00\u4e2a\u60c5\u51b5\u662f\u4f8b\u5916\uff0c\u90a3\u5c31\u662f\u201c\u5173\u8054\u8868\u201d\u7684\u201c\u5916\u952e\u201d\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u4e2a\u8868\u7684\u4e3b\u952e\uff0c\u901a\u8fc7\u82e5\u5e72\u4e2a\u522b\u7684\u8868\u7684\u4e3b\u952e\u6784\u6210\u3002\u6211\u4eec\u628a\u8fd9\u4e2a\u60c5\u51b5\u53eb\u505a\u201c\u5916\u952e\u201d\u3002\u6bd4\u5982\uff1a\u6709\u4e00\u4e2a\u201c\u5b66\u751f\u8868\u201d\u6709\u5b66\u751f\u7684ID\uff0c\u6709\u4e00\u4e2a\u201c\u8bfe\u7a0b\u8868\u201d\u6709\u8bfe\u7a0bID\uff0c\u90a3\u4e48\uff0c\u201c\u6210\u7ee9\u8868\u201d\u5c31\u662f\u201c\u5173\u8054\u8868\u201d\u4e86\uff0c\u5176\u5173\u8054\u4e86\u5b66\u751f\u8868\u548c\u8bfe\u7a0b\u8868\uff0c\u5728\u6210\u7ee9\u8868\u4e2d\uff0c\u5b66\u751fID\u548c\u8bfe\u7a0bID\u53eb\u201c\u5916\u952e\u201d\u5176\u5171\u540c\u7ec4\u6210\u4e3b\u952e\u3002<\/p>\n<p><strong>9. \u4f7f\u7528 ENUM \u800c\u4e0d\u662f VARCHAR<\/strong><\/p>\n<p>ENUM \u7c7b\u578b\u662f\u975e\u5e38\u5feb\u548c\u7d27\u51d1\u7684\u3002\u5728\u5b9e\u9645\u4e0a\uff0c\u5176\u4fdd\u5b58\u7684\u662f TINYINT\uff0c\u4f46\u5176\u5916\u8868\u4e0a\u663e\u793a\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7528\u8fd9\u4e2a\u5b57\u6bb5\u6765\u505a\u4e00\u4e9b\u9009\u9879\u5217\u8868\u53d8\u5f97\u76f8\u5f53\u7684\u5b8c\u7f8e\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u6bb5\uff0c\u6bd4\u5982\u201c\u6027\u522b\u201d\uff0c\u201c\u56fd\u5bb6\u201d\uff0c\u201c\u6c11\u65cf\u201d\uff0c\u201c\u72b6\u6001\u201d\u6216\u201c\u90e8\u95e8\u201d\uff0c\u4f60\u77e5\u9053\u8fd9\u4e9b\u5b57\u6bb5\u7684\u53d6\u503c\u662f\u6709\u9650\u800c\u4e14\u56fa\u5b9a\u7684\uff0c\u90a3\u4e48\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 ENUM \u800c\u4e0d\u662f VARCHAR\u3002<\/p>\n<p>MySQL\u4e5f\u6709\u4e00\u4e2a\u201c\u5efa\u8bae\u201d\uff08\u89c1\u7b2c\u5341\u6761\uff09\u544a\u8bc9\u4f60\u600e\u4e48\u53bb\u91cd\u65b0\u7ec4\u7ec7\u4f60\u7684\u8868\u7ed3\u6784\u3002\u5f53\u4f60\u6709\u4e00\u4e2a VARCHAR \u5b57\u6bb5\u65f6\uff0c\u8fd9\u4e2a\u5efa\u8bae\u4f1a\u544a\u8bc9\u4f60\u628a\u5176\u6539\u6210 ENUM \u7c7b\u578b\u3002\u4f7f\u7528 PROCEDURE ANALYSE() \u4f60\u53ef\u4ee5\u5f97\u5230\u76f8\u5173\u7684\u5efa\u8bae\u3002<\/p>\n<p><strong>10. \u4ece PROCEDURE ANALYSE() \u53d6\u5f97\u5efa\u8bae<\/strong><\/p>\n<p>PROCEDURE ANALYSE() \u4f1a\u8ba9 MySQL \u5e2e\u4f60\u53bb\u5206\u6790\u4f60\u7684\u5b57\u6bb5\u548c\u5176\u5b9e\u9645\u7684\u6570\u636e\uff0c\u5e76\u4f1a\u7ed9\u4f60\u4e00\u4e9b\u6709\u7528\u7684\u5efa\u8bae\u3002\u53ea\u6709\u8868\u4e2d\u6709\u5b9e\u9645\u7684\u6570\u636e\uff0c\u8fd9\u4e9b\u5efa\u8bae\u624d\u4f1a\u53d8\u5f97\u6709\u7528\uff0c\u56e0\u4e3a\u8981\u505a\u4e00\u4e9b\u5927\u7684\u51b3\u5b9a\u662f\u9700\u8981\u6709\u6570\u636e\u4f5c\u4e3a\u57fa\u7840\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a INT \u5b57\u6bb5\u4f5c\u4e3a\u4f60\u7684\u4e3b\u952e\uff0c\u7136\u800c\u5e76\u6ca1\u6709\u592a\u591a\u7684\u6570\u636e\uff0c\u90a3\u4e48\uff0cPROCEDURE ANALYSE()\u4f1a\u5efa\u8bae\u4f60\u628a\u8fd9\u4e2a\u5b57\u6bb5\u7684\u7c7b\u578b\u6539\u6210 MEDIUMINT \u3002\u6216\u662f\u4f60\u4f7f\u7528\u4e86\u4e00\u4e2a VARCHAR \u5b57\u6bb5\uff0c\u56e0\u4e3a\u6570\u636e\u4e0d\u591a\uff0c\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u8ba9\u4f60\u628a\u5b83\u6539\u6210 ENUM \u7684\u5efa\u8bae\u3002\u8fd9\u4e9b\u5efa\u8bae\uff0c\u90fd\u662f\u53ef\u80fd\u56e0\u4e3a\u6570\u636e\u4e0d\u591f\u591a\uff0c\u6240\u4ee5\u51b3\u7b56\u505a\u5f97\u5c31\u4e0d\u591f\u51c6\u3002<\/p>\n<p>\u5728phpmyadmin\u91cc\uff0c\u4f60\u53ef\u4ee5\u5728\u67e5\u770b\u8868\u65f6\uff0c\u70b9\u51fb \u201cPropose table structure\u201d \u6765\u67e5\u770b\u8fd9\u4e9b\u5efa\u8bae<\/p>\n<p>&lt;ignore_js_op&gt;<img decoding=\"async\" id=\"aimg_6303\" title=\"a3.jpg\" alt=\"a3.jpg\" src=\"http:\/\/devbbs-discuzx.stor.sinaapp.com\/data\/attachment\/forum\/201307\/30\/1635586ocnyoooooopoz2z.jpg\" width=\"418\" \/><\/p>\n<p>\u4e00\u5b9a\u8981\u6ce8\u610f\uff0c\u8fd9\u4e9b\u53ea\u662f\u5efa\u8bae\uff0c\u53ea\u6709\u5f53\u4f60\u7684\u8868\u91cc\u7684\u6570\u636e\u8d8a\u6765\u8d8a\u591a\u65f6\uff0c\u8fd9\u4e9b\u5efa\u8bae\u624d\u4f1a\u53d8\u5f97\u51c6\u786e\u3002\u4e00\u5b9a\u8981\u8bb0\u4f4f\uff0c\u4f60\u624d\u662f\u6700\u7ec8\u505a\u51b3\u5b9a\u7684\u4eba\u3002<\/p>\n<p><strong>11. \u5c3d\u53ef\u80fd\u7684\u4f7f\u7528 NOT NULL<\/strong><\/p>\n<p>\u9664\u975e\u4f60\u6709\u4e00\u4e2a\u5f88\u7279\u522b\u7684\u539f\u56e0\u53bb\u4f7f\u7528 NULL \u503c\uff0c\u4f60\u5e94\u8be5\u603b\u662f\u8ba9\u4f60\u7684\u5b57\u6bb5\u4fdd\u6301 NOT NULL\u3002\u8fd9\u770b\u8d77\u6765\u597d\u50cf\u6709\u70b9\u4e89\u8bae\uff0c\u8bf7\u5f80\u4e0b\u770b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u95ee\u95ee\u4f60\u81ea\u5df1\u201cEmpty\u201d\u548c\u201cNULL\u201d\u6709\u591a\u5927\u7684\u533a\u522b\uff08\u5982\u679c\u662fINT\uff0c\u90a3\u5c31\u662f0\u548cNULL\uff09\uff1f\u5982\u679c\u4f60\u89c9\u5f97\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u90a3\u4e48\u4f60\u5c31\u4e0d\u8981\u4f7f\u7528NULL\u3002\uff08\u4f60\u77e5\u9053\u5417\uff1f\u5728 Oracle \u91cc\uff0cNULL \u548c Empty \u7684\u5b57\u7b26\u4e32\u662f\u4e00\u6837\u7684\uff01)<\/p>\n<p>\u4e0d\u8981\u4ee5\u4e3a NULL \u4e0d\u9700\u8981\u7a7a\u95f4\uff0c\u5176\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\uff0c\u5e76\u4e14\uff0c\u5728\u4f60\u8fdb\u884c\u6bd4\u8f83\u7684\u65f6\u5019\uff0c\u4f60\u7684\u7a0b\u5e8f\u4f1a\u66f4\u590d\u6742\u3002 \u5f53\u7136\uff0c\u8fd9\u91cc\u5e76\u4e0d\u662f\u8bf4\u4f60\u5c31\u4e0d\u80fd\u4f7f\u7528NULL\u4e86\uff0c\u73b0\u5b9e\u60c5\u51b5\u662f\u5f88\u590d\u6742\u7684\uff0c\u4f9d\u7136\u4f1a\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u9700\u8981\u4f7f\u7528NULL\u503c\u3002<\/p>\n<p>\u4e0b\u9762\u6458\u81eaMySQL\u81ea\u5df1\u7684\u6587\u6863\uff1a<\/p>\n<div>\n<blockquote><p>\u201cNULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.\u201d<\/p><\/blockquote>\n<\/div>\n<p><strong>12. Prepared Statements<\/strong><br \/>\nPrepared Statements\u5f88\u50cf\u5b58\u50a8\u8fc7\u7a0b\uff0c\u662f\u4e00\u79cd\u8fd0\u884c\u5728\u540e\u53f0\u7684SQL\u8bed\u53e5\u96c6\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u4f7f\u7528 prepared statements \u83b7\u5f97\u5f88\u591a\u597d\u5904\uff0c\u65e0\u8bba\u662f\u6027\u80fd\u95ee\u9898\u8fd8\u662f\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<p>Prepared Statements \u53ef\u4ee5\u68c0\u67e5\u4e00\u4e9b\u4f60\u7ed1\u5b9a\u597d\u7684\u53d8\u91cf\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u62a4\u4f60\u7684\u7a0b\u5e8f\u4e0d\u4f1a\u53d7\u5230\u201cSQL\u6ce8\u5165\u5f0f\u201d\u653b\u51fb\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u624b\u52a8\u5730\u68c0\u67e5\u4f60\u7684\u8fd9\u4e9b\u53d8\u91cf\uff0c\u7136\u800c\uff0c\u624b\u52a8\u7684\u68c0\u67e5\u5bb9\u6613\u51fa\u95ee\u9898\uff0c\u800c\u4e14\u5f88\u7ecf\u5e38\u4f1a\u88ab\u7a0b\u5e8f\u5458\u5fd8\u4e86\u3002\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e9bframework\u6216\u662fORM\u7684\u65f6\u5019\uff0c\u8fd9\u6837\u7684\u95ee\u9898\u4f1a\u597d\u4e00\u4e9b\u3002<\/p>\n<p>\u5728\u6027\u80fd\u65b9\u9762\uff0c\u5f53\u4e00\u4e2a\u76f8\u540c\u7684\u67e5\u8be2\u88ab\u4f7f\u7528\u591a\u6b21\u7684\u65f6\u5019\uff0c\u8fd9\u4f1a\u4e3a\u4f60\u5e26\u6765\u53ef\u89c2\u7684\u6027\u80fd\u4f18\u52bf\u3002\u4f60\u53ef\u4ee5\u7ed9\u8fd9\u4e9bPrepared Statements\u5b9a\u4e49\u4e00\u4e9b\u53c2\u6570\uff0c\u800cMySQL\u53ea\u4f1a\u89e3\u6790\u4e00\u6b21\u3002<\/p>\n<p>\u867d\u7136\u6700\u65b0\u7248\u672c\u7684MySQL\u5728\u4f20\u8f93Prepared Statements\u662f\u4f7f\u7528\u4e8c\u8fdb\u5236\u5f62\u52bf\uff0c\u6240\u4ee5\u8fd9\u4f1a\u4f7f\u5f97\u7f51\u7edc\u4f20\u8f93\u975e\u5e38\u6709\u6548\u7387\u3002<br \/>\n\u5f53\u7136\uff0c\u4e5f\u6709\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u907f\u514d\u4f7f\u7528Prepared Statements\uff0c\u56e0\u4e3a\u5176\u4e0d\u652f\u6301\u67e5\u8be2\u7f13\u5b58\u3002\u4f46\u636e\u8bf4\u7248\u672c5.1\u540e\u652f\u6301\u4e86\u3002<\/p>\n<p>\u5728PHP\u4e2d\u8981\u4f7f\u7528prepared statements\uff0c\u4f60\u53ef\u4ee5\u67e5\u770b\u5176\u4f7f\u7528\u624b\u518c\uff1amysqli \u6269\u5c55 \u6216\u662f\u4f7f\u7528\u6570\u636e\u5e93\u62bd\u8c61\u5c42\uff0c\u5982\uff1a PDO.<\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=%2F%2F%20%E5%88%9B%E5%BB%BA%20prepared%20statement%0Aif%20(%24stmt%20%3D%20%24mysqli-%26gt%3Bprepare(%22SELECT%20username%20FROM%20user%20WHERE%20state%3D%3F%22))%20%7B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E7%BB%91%E5%AE%9A%E5%8F%82%E6%95%B0%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bbind_param(%22s%22%2C%20%24state)%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E6%89%A7%E8%A1%8C%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bexecute()%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E7%BB%91%E5%AE%9A%E7%BB%93%E6%9E%9C%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bbind_result(%24username)%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E7%A7%BB%E5%8A%A8%E6%B8%B8%E6%A0%87%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bfetch()%3B%0A%20%0A%26nbsp%3B%26nbsp%3Bprintf(%22%25s%20is%20from%20%25s%5Cn%22%2C%20%24username%2C%20%24state)%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bclose()%3B%0A%7D\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=%2F%2F%20%E5%88%9B%E5%BB%BA%20prepared%20statement%0Aif%20(%24stmt%20%3D%20%24mysqli-%26gt%3Bprepare(%22SELECT%20username%20FROM%20user%20WHERE%20state%3D%3F%22))%20%7B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E7%BB%91%E5%AE%9A%E5%8F%82%E6%95%B0%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bbind_param(%22s%22%2C%20%24state)%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E6%89%A7%E8%A1%8C%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bexecute()%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E7%BB%91%E5%AE%9A%E7%BB%93%E6%9E%9C%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bbind_result(%24username)%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E7%A7%BB%E5%8A%A8%E6%B8%B8%E6%A0%87%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bfetch()%3B%0A%20%0A%26nbsp%3B%26nbsp%3Bprintf(%22%25s%20is%20from%20%25s%5Cn%22%2C%20%24username%2C%20%24state)%3B%0A%20%0A%26nbsp%3B%26nbsp%3B%24stmt-%26gt%3Bclose()%3B%0A%7D\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>\/\/ \u521b\u5efa prepared statement<\/li>\n<li>if\u00a0($stmt\u00a0=\u00a0$mysqli-&gt;prepare(&#8220;SELECT username FROM user WHERE state=?&#8221;)) {<\/li>\n<li><\/li>\n<li>\u00a0\u00a0\/\/ \u7ed1\u5b9a\u53c2\u6570<\/li>\n<li>\u00a0\u00a0$stmt-&gt;bind_param(&#8220;s&#8221;,\u00a0$state);<\/li>\n<li><\/li>\n<li>\u00a0\u00a0\/\/ \u6267\u884c<\/li>\n<li>\u00a0\u00a0$stmt-&gt;execute();<\/li>\n<li><\/li>\n<li>\u00a0\u00a0\/\/ \u7ed1\u5b9a\u7ed3\u679c<\/li>\n<li>\u00a0\u00a0$stmt-&gt;bind_result($username);<\/li>\n<li><\/li>\n<li>\u00a0\u00a0\/\/ \u79fb\u52a8\u6e38\u6807<\/li>\n<li>\u00a0\u00a0$stmt-&gt;fetch();<\/li>\n<li><\/li>\n<li>\u00a0\u00a0printf(&#8220;%s is from %s\\n&#8221;,\u00a0$username,\u00a0$state);<\/li>\n<li><\/li>\n<li>\u00a0\u00a0$stmt-&gt;close();<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<p><strong><br \/>\n13. \u65e0\u7f13\u51b2\u7684\u67e5\u8be2<\/strong><br \/>\n\u6b63\u5e38\u7684\u60c5\u51b5\u4e0b\uff0c\u5f53\u4f60\u5728\u5f53\u4f60\u5728\u4f60\u7684\u811a\u672c\u4e2d\u6267\u884c\u4e00\u4e2aSQL\u8bed\u53e5\u7684\u65f6\u5019\uff0c\u4f60\u7684\u7a0b\u5e8f\u4f1a\u505c\u5728\u90a3\u91cc\u76f4\u5230\u6ca1\u8fd9\u4e2aSQL\u8bed\u53e5\u8fd4\u56de\uff0c\u7136\u540e\u4f60\u7684\u7a0b\u5e8f\u518d\u5f80\u4e0b\u7ee7\u7eed\u6267\u884c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u65e0\u7f13\u51b2\u67e5\u8be2\u6765\u6539\u53d8\u8fd9\u4e2a\u884c\u4e3a\u3002<\/p>\n<p>\u5173\u4e8e\u8fd9\u4e2a\u4e8b\u60c5\uff0c\u5728PHP\u7684\u6587\u6863\u4e2d\u6709\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u8bf4\u660e\uff1a\u00a0<a href=\"http:\/\/php.net\/manual\/en\/function.mysql-unbuffered-query.php\" target=\"_blank\" rel=\"noopener noreferrer\">mysql_unbuffered_query()<\/a>\u00a0\u51fd\u6570\uff1a<\/p>\n<div>\n<blockquote><p>\u201cmysql_unbuffered_query() sends the SQL query query to MySQL without automatically fetching and buffering the result rows as mysql_query() does. This saves a considerable amount of memory with SQL queries that produce large result sets, and you can start working on the result set immediately after the first row has been retrieved as you don\u2019t have to wait until the complete SQL query has been performed.\u201d<\/p><\/blockquote>\n<\/div>\n<p>\u4e0a\u9762\u90a3\u53e5\u8bdd\u7ffb\u8bd1\u8fc7\u6765\u662f\u8bf4\uff0cmysql_unbuffered_query() \u53d1\u9001\u4e00\u4e2aSQL\u8bed\u53e5\u5230MySQL\u800c\u5e76\u4e0d\u50cfmysql_query()\u4e00\u6837\u53bb\u81ea\u52a8fethch\u548c\u7f13\u5b58\u7ed3\u679c\u3002\u8fd9\u4f1a\u76f8\u5f53\u8282\u7ea6\u5f88\u591a\u53ef\u89c2\u7684\u5185\u5b58\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u4f1a\u4ea7\u751f\u5927\u91cf\u7ed3\u679c\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u5e76\u4e14\uff0c\u4f60\u4e0d\u9700\u8981\u7b49\u5230\u6240\u6709\u7684\u7ed3\u679c\u90fd\u8fd4\u56de\uff0c\u53ea\u9700\u8981\u7b2c\u4e00\u884c\u6570\u636e\u8fd4\u56de\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u9a6c\u4e0a\u5f00\u59cb\u5de5\u4f5c\u4e8e\u67e5\u8be2\u7ed3\u679c\u4e86\u3002<br \/>\n\u7136\u800c\uff0c\u8fd9\u4f1a\u6709\u4e00\u4e9b\u9650\u5236\u3002\u56e0\u4e3a\u4f60\u8981\u4e48\u628a\u6240\u6709\u884c\u90fd\u8bfb\u8d70\uff0c\u6216\u662f\u4f60\u8981\u5728\u8fdb\u884c\u4e0b\u4e00\u6b21\u7684\u67e5\u8be2\u524d\u8c03\u7528\u00a0<a href=\"http:\/\/us2.php.net\/manual\/en\/function.mysql-free-result.php\" target=\"_blank\" rel=\"noopener noreferrer\">mysql_free_result()<\/a>\u6e05\u9664\u7ed3\u679c\u3002\u800c\u4e14\uff0c<a href=\"http:\/\/us2.php.net\/manual\/en\/function.mysql-num-rows.php\" target=\"_blank\" rel=\"noopener noreferrer\">mysql_num_rows()<\/a>\u6216<a href=\"http:\/\/us2.php.net\/manual\/en\/function.mysql-data-seek.php\" target=\"_blank\" rel=\"noopener noreferrer\">mysql_data_seek()<\/a>\u00a0\u5c06\u65e0\u6cd5\u4f7f\u7528\u3002\u6240\u4ee5\uff0c\u662f\u5426\u4f7f\u7528\u65e0\u7f13\u51b2\u7684\u67e5\u8be2\u4f60\u9700\u8981\u4ed4\u7ec6\u8003\u8651\u3002<\/p>\n<p><strong>14. \u628aIP\u5730\u5740\u5b58\u6210 UNSIGNED INT<\/strong><br \/>\n\u5f88\u591a\u7a0b\u5e8f\u5458\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a VARCHAR(15) \u5b57\u6bb5\u6765\u5b58\u653e\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684IP\u800c\u4e0d\u662f\u6574\u5f62\u7684IP\u3002\u5982\u679c\u4f60\u7528\u6574\u5f62\u6765\u5b58\u653e\uff0c\u53ea\u9700\u89814\u4e2a\u5b57\u8282\uff0c\u5e76\u4e14\u4f60\u53ef\u4ee5\u6709\u5b9a\u957f\u7684\u5b57\u6bb5\u3002\u800c\u4e14\uff0c\u8fd9\u4f1a\u4e3a\u4f60\u5e26\u6765\u67e5\u8be2\u4e0a\u7684\u4f18\u52bf\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u9700\u8981\u4f7f\u7528\u8fd9\u6837\u7684WHERE\u6761\u4ef6\uff1aIP between ip1 and ip2\u3002<\/p>\n<p>\u6211\u4eec\u5fc5\u9700\u8981\u4f7f\u7528UNSIGNED INT\uff0c\u56e0\u4e3a IP\u5730\u5740\u4f1a\u4f7f\u7528\u6574\u4e2a32\u4f4d\u7684\u65e0\u7b26\u53f7\u6574\u5f62\u3002<\/p>\n<p><span style=\"color: #555555;\">\u800c\u4f60\u7684\u67e5\u8be2\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u00a0<\/span><span style=\"color: #2970a6;\"><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/miscellaneous-functions.html#function_inet-aton\" target=\"_blank\" rel=\"noopener noreferrer\">INET_ATON()<\/a><\/span><span style=\"color: #555555;\">\u00a0\u6765\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32IP\u8f6c\u6210\u4e00\u4e2a\u6574\u5f62\uff0c\u5e76\u4f7f\u7528\u00a0<\/span><span style=\"color: #2970a6;\"><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/miscellaneous-functions.html#function_inet-ntoa\" target=\"_blank\" rel=\"noopener noreferrer\">INET_NTOA()<\/a><\/span><span style=\"color: #555555;\">\u00a0\u628a\u4e00\u4e2a\u6574\u5f62\u8f6c\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32IP\u3002\u5728PHP\u4e2d\uff0c\u4e5f\u6709\u8fd9\u6837\u7684\u51fd\u6570\u00a0<\/span><span style=\"color: #2970a6;\"><a href=\"http:\/\/php.net\/manual\/en\/function.ip2long.php\" target=\"_blank\" rel=\"noopener noreferrer\">ip2long()<\/a><\/span><span style=\"color: #555555;\">\u00a0\u548c\u00a0<\/span><span style=\"color: #2970a6;\"><a href=\"http:\/\/us.php.net\/manual\/en\/function.long2ip.php\" target=\"_blank\" rel=\"noopener noreferrer\">long2ip()<\/a><\/span><span style=\"color: #555555;\">\u3002<\/span><\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=%24r%20%3D%20%22UPDATE%20users%20SET%20ip%20%3D%20INET_ATON('%7B%24_SERVER%5B'REMOTE_ADDR'%5D%7D')%20WHERE%20user_id%20%3D%20%24user_id%22%3B%0A\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=%24r%20%3D%20%22UPDATE%20users%20SET%20ip%20%3D%20INET_ATON('%7B%24_SERVER%5B'REMOTE_ADDR'%5D%7D')%20WHERE%20user_id%20%3D%20%24user_id%22%3B%0A\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>$r\u00a0=\u00a0&#8220;UPDATE users SET ip = INET_ATON(&#8216;{$_SERVER[&#8216;REMOTE_ADDR&#8217;]}&#8217;) WHERE user_id = $user_id&#8221;;<\/li>\n<\/ol>\n<\/div>\n<p><strong><br \/>\n15. \u56fa\u5b9a\u957f\u5ea6\u7684\u8868\u4f1a\u66f4\u5feb<\/strong><br \/>\n<span style=\"color: #555555;\">\u5982\u679c\u8868\u4e2d\u7684\u6240\u6709\u5b57\u6bb5\u90fd\u662f\u201c\u56fa\u5b9a\u957f\u5ea6\u201d\u7684\uff0c\u6574\u4e2a\u8868\u4f1a\u88ab\u8ba4\u4e3a\u662f\u00a0<\/span><span style=\"color: #2970a6;\"><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/static-format.html\" target=\"_blank\" rel=\"noopener noreferrer\">\u201cstatic\u201d \u6216 \u201cfixed-length\u201d<\/a><\/span><span style=\"color: #555555;\">\u3002 \u4f8b\u5982\uff0c\u8868\u4e2d\u6ca1\u6709\u5982\u4e0b\u7c7b\u578b\u7684\u5b57\u6bb5\uff1a VARCHAR\uff0cTEXT\uff0cBLOB\u3002\u53ea\u8981\u4f60\u5305\u62ec\u4e86\u5176\u4e2d\u4e00\u4e2a\u8fd9\u4e9b\u5b57\u6bb5\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8868\u5c31\u4e0d\u662f\u201c\u56fa\u5b9a\u957f\u5ea6\u9759\u6001\u8868\u201d\u4e86\uff0c\u8fd9\u6837\uff0cMySQL \u5f15\u64ce\u4f1a\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u3002<\/span><\/p>\n<p>\u56fa\u5b9a\u957f\u5ea6\u7684\u8868\u4f1a\u63d0\u9ad8\u6027\u80fd\uff0c\u56e0\u4e3aMySQL\u641c\u5bfb\u5f97\u4f1a\u66f4\u5feb\u4e00\u4e9b\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u56fa\u5b9a\u7684\u957f\u5ea6\u662f\u5f88\u5bb9\u6613\u8ba1\u7b97\u4e0b\u4e00\u4e2a\u6570\u636e\u7684\u504f\u79fb\u91cf\u7684\uff0c\u6240\u4ee5\u8bfb\u53d6\u7684\u81ea\u7136\u4e5f\u4f1a\u5f88\u5feb\u3002\u800c\u5982\u679c\u5b57\u6bb5\u4e0d\u662f\u5b9a\u957f\u7684\uff0c\u90a3\u4e48\uff0c\u6bcf\u4e00\u6b21\u8981\u627e\u4e0b\u4e00\u6761\u7684\u8bdd\uff0c\u9700\u8981\u7a0b\u5e8f\u627e\u5230\u4e3b\u952e\u3002<\/p>\n<p>\u5e76\u4e14\uff0c\u56fa\u5b9a\u957f\u5ea6\u7684\u8868\u4e5f\u66f4\u5bb9\u6613\u88ab\u7f13\u5b58\u548c\u91cd\u5efa\u3002\u4e0d\u8fc7\uff0c\u552f\u4e00\u7684\u526f\u4f5c\u7528\u662f\uff0c\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u6bb5\u4f1a\u6d6a\u8d39\u4e00\u4e9b\u7a7a\u95f4\uff0c\u56e0\u4e3a\u5b9a\u957f\u7684\u5b57\u6bb5\u65e0\u8bba\u4f60\u7528\u4e0d\u7528\uff0c\u4ed6\u90fd\u662f\u8981\u5206\u914d\u90a3\u4e48\u591a\u7684\u7a7a\u95f4\u3002<br \/>\n\u4f7f\u7528\u201c\u5782\u76f4\u5206\u5272\u201d\u6280\u672f\uff08\u89c1\u4e0b\u4e00\u6761\uff09\uff0c\u4f60\u53ef\u4ee5\u5206\u5272\u4f60\u7684\u8868\u6210\u4e3a\u4e24\u4e2a\u4e00\u4e2a\u662f\u5b9a\u957f\u7684\uff0c\u4e00\u4e2a\u5219\u662f\u4e0d\u5b9a\u957f\u7684\u3002<\/p>\n<p><strong>16. \u5782\u76f4\u5206\u5272<\/strong><br \/>\n\u201c\u5782\u76f4\u5206\u5272\u201d\u662f\u4e00\u79cd\u628a\u6570\u636e\u5e93\u4e2d\u7684\u8868\u6309\u5217\u53d8\u6210\u51e0\u5f20\u8868\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u53ef\u4ee5\u964d\u4f4e\u8868\u7684\u590d\u6742\u5ea6\u548c\u5b57\u6bb5\u7684\u6570\u76ee\uff0c\u4ece\u800c\u8fbe\u5230\u4f18\u5316\u7684\u76ee\u7684\u3002\uff08\u4ee5\u524d\uff0c\u5728\u94f6\u884c\u505a\u8fc7\u9879\u76ee\uff0c\u89c1\u8fc7\u4e00\u5f20\u8868\u6709100\u591a\u4e2a\u5b57\u6bb5\uff0c\u5f88\u6050\u6016\uff09<\/p>\n<p><strong>\u793a\u4f8b\u4e00<\/strong>\uff1a\u5728Users\u8868\u4e2d\u6709\u4e00\u4e2a\u5b57\u6bb5\u662f\u5bb6\u5ead\u5730\u5740\uff0c\u8fd9\u4e2a\u5b57\u6bb5\u662f\u53ef\u9009\u5b57\u6bb5\uff0c\u76f8\u6bd4\u8d77\uff0c\u800c\u4e14\u4f60\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u65f6\u5019\u9664\u4e86\u4e2a\u4eba\u4fe1\u606f\u5916\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u7ecf\u5e38\u8bfb\u53d6\u6216\u662f\u6539\u5199\u8fd9\u4e2a\u5b57\u6bb5\u3002\u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u628a\u4ed6\u653e\u5230\u53e6\u5916\u4e00\u5f20\u8868\u4e2d\u5462\uff1f \u8fd9\u6837\u4f1a\u8ba9\u4f60\u7684\u8868\u6709\u66f4\u597d\u7684\u6027\u80fd\uff0c\u5927\u5bb6\u60f3\u60f3\u662f\u4e0d\u662f\uff0c\u5927\u91cf\u7684\u65f6\u5019\uff0c\u6211\u5bf9\u4e8e\u7528\u6237\u8868\u6765\u8bf4\uff0c\u53ea\u6709\u7528\u6237ID\uff0c\u7528\u6237\u540d\uff0c\u53e3\u4ee4\uff0c\u7528\u6237\u89d2\u8272\u7b49\u4f1a\u88ab\u7ecf\u5e38\u4f7f\u7528\u3002\u5c0f\u4e00\u70b9\u7684\u8868\u603b\u662f\u4f1a\u6709\u597d\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4e8c<\/strong>\uff1a \u4f60\u6709\u4e00\u4e2a\u53eb \u201clast_login\u201d \u7684\u5b57\u6bb5\uff0c\u5b83\u4f1a\u5728\u6bcf\u6b21\u7528\u6237\u767b\u5f55\u65f6\u88ab\u66f4\u65b0\u3002\u4f46\u662f\uff0c\u6bcf\u6b21\u66f4\u65b0\u65f6\u4f1a\u5bfc\u81f4\u8be5\u8868\u7684\u67e5\u8be2\u7f13\u5b58\u88ab\u6e05\u7a7a\u3002\u6240\u4ee5\uff0c\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5b57\u6bb5\u653e\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u5f71\u54cd\u4f60\u5bf9\u7528\u6237ID\uff0c\u7528\u6237\u540d\uff0c\u7528\u6237\u89d2\u8272\u7684\u4e0d\u505c\u5730\u8bfb\u53d6\u4e86\uff0c\u56e0\u4e3a\u67e5\u8be2\u7f13\u5b58\u4f1a\u5e2e\u4f60\u589e\u52a0\u5f88\u591a\u6027\u80fd\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u4f60\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e9b\u88ab\u5206\u51fa\u53bb\u7684\u5b57\u6bb5\u6240\u5f62\u6210\u7684\u8868\uff0c\u4f60\u4e0d\u4f1a\u7ecf\u5e38\u6027\u5730\u53bbJoin\u4ed6\u4eec\uff0c\u4e0d\u7136\u7684\u8bdd\uff0c\u8fd9\u6837\u7684\u6027\u80fd\u4f1a\u6bd4\u4e0d\u5206\u5272\u65f6\u8fd8\u8981\u5dee\uff0c\u800c\u4e14\uff0c\u4f1a\u662f\u6781\u6570\u7ea7\u7684\u4e0b\u964d\u3002<\/p>\n<p><strong>17. \u62c6\u5206\u5927\u7684 DELETE \u6216 INSERT \u8bed\u53e5<\/strong><br \/>\n\u5982\u679c\u4f60\u9700\u8981\u5728\u4e00\u4e2a\u5728\u7ebf\u7684\u7f51\u7ad9\u4e0a\u53bb\u6267\u884c\u4e00\u4e2a\u5927\u7684 DELETE \u6216 INSERT \u67e5\u8be2\uff0c\u4f60\u9700\u8981\u975e\u5e38\u5c0f\u5fc3\uff0c\u8981\u907f\u514d\u4f60\u7684\u64cd\u4f5c\u8ba9\u4f60\u7684\u6574\u4e2a\u7f51\u7ad9\u505c\u6b62\u76f8\u5e94\u3002\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u662f\u4f1a\u9501\u8868\u7684\uff0c\u8868\u4e00\u9501\u4f4f\u4e86\uff0c\u522b\u7684\u64cd\u4f5c\u90fd\u8fdb\u4e0d\u6765\u4e86\u3002<\/p>\n<p>Apache \u4f1a\u6709\u5f88\u591a\u7684\u5b50\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u3002\u6240\u4ee5\uff0c\u5176\u5de5\u4f5c\u8d77\u6765\u76f8\u5f53\u6709\u6548\u7387\uff0c\u800c\u6211\u4eec\u7684\u670d\u52a1\u5668\u4e5f\u4e0d\u5e0c\u671b\u6709\u592a\u591a\u7684\u5b50\u8fdb\u7a0b\uff0c\u7ebf\u7a0b\u548c\u6570\u636e\u5e93\u94fe\u63a5\uff0c\u8fd9\u662f\u6781\u5927\u7684\u5360\u670d\u52a1\u5668\u8d44\u6e90\u7684\u4e8b\u60c5\uff0c\u5c24\u5176\u662f\u5185\u5b58\u3002<\/p>\n<p>\u5982\u679c\u4f60\u628a\u4f60\u7684\u8868\u9501\u4e0a\u4e00\u6bb5\u65f6\u95f4\uff0c\u6bd4\u598230\u79d2\u949f\uff0c\u90a3\u4e48\u5bf9\u4e8e\u4e00\u4e2a\u6709\u5f88\u9ad8\u8bbf\u95ee\u91cf\u7684\u7ad9\u70b9\u6765\u8bf4\uff0c\u8fd930\u79d2\u6240\u79ef\u7d2f\u7684\u8bbf\u95ee\u8fdb\u7a0b\/\u7ebf\u7a0b\uff0c\u6570\u636e\u5e93\u94fe\u63a5\uff0c\u6253\u5f00\u7684\u6587\u4ef6\u6570\uff0c\u53ef\u80fd\u4e0d\u4ec5\u4ec5\u4f1a\u8ba9\u4f60\u6ccaWEB\u670d\u52a1Crash\uff0c\u8fd8\u53ef\u80fd\u4f1a\u8ba9\u4f60\u7684\u6574\u53f0\u670d\u52a1\u5668\u9a6c\u4e0a\u639b\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5927\u7684\u5904\u7406\uff0c\u4f60\u5b9a\u4f60\u4e00\u5b9a\u628a\u5176\u62c6\u5206\uff0c\u4f7f\u7528 LIMIT \u6761\u4ef6\u662f\u4e00\u4e2a\u597d\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<div>\n<div>\n<div>PHP\u4ee3\u7801\u00a0<object width=\"14\" height=\"15\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" \/><param name=\"flashvars\" value=\"clipboard=while%20(1)%20%7B%0A%26nbsp%3B%26nbsp%3B%2F%2F%E6%AF%8F%E6%AC%A1%E5%8F%AA%E5%81%9A1000%E6%9D%A1%0A%26nbsp%3B%26nbsp%3Bmysql_query(%22DELETE%20FROM%20logs%20WHERE%20log_date%20%26lt%3B%3D%20'2009-11-01'%20LIMIT%201000%22)%3B%0A%26nbsp%3B%26nbsp%3Bif%20(mysql_affected_rows()%20%3D%3D%200)%20%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%2F%2F%20%E6%B2%A1%E5%BE%97%E5%8F%AF%E5%88%A0%E4%BA%86%EF%BC%8C%E9%80%80%E5%87%BA%EF%BC%81%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bbreak%3B%0A%26nbsp%3B%26nbsp%3B%7D%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E6%AF%8F%E6%AC%A1%E9%83%BD%E8%A6%81%E4%BC%91%E6%81%AF%E4%B8%80%E4%BC%9A%E5%84%BF%0A%26nbsp%3B%26nbsp%3Busleep(50000)%3B%0A%7D%0A\" \/><param name=\"quality\" value=\"high\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"pluginspage\" value=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/cloudbbs.org\/source\/plugin\/milu_hightLight\/clipboard_new.swf\" flashvars=\"clipboard=while%20(1)%20%7B%0A%26nbsp%3B%26nbsp%3B%2F%2F%E6%AF%8F%E6%AC%A1%E5%8F%AA%E5%81%9A1000%E6%9D%A1%0A%26nbsp%3B%26nbsp%3Bmysql_query(%22DELETE%20FROM%20logs%20WHERE%20log_date%20%26lt%3B%3D%20'2009-11-01'%20LIMIT%201000%22)%3B%0A%26nbsp%3B%26nbsp%3Bif%20(mysql_affected_rows()%20%3D%3D%200)%20%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%2F%2F%20%E6%B2%A1%E5%BE%97%E5%8F%AF%E5%88%A0%E4%BA%86%EF%BC%8C%E9%80%80%E5%87%BA%EF%BC%81%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bbreak%3B%0A%26nbsp%3B%26nbsp%3B%7D%0A%26nbsp%3B%26nbsp%3B%2F%2F%20%E6%AF%8F%E6%AC%A1%E9%83%BD%E8%A6%81%E4%BC%91%E6%81%AF%E4%B8%80%E4%BC%9A%E5%84%BF%0A%26nbsp%3B%26nbsp%3Busleep(50000)%3B%0A%7D%0A\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/><\/object><\/div>\n<\/div>\n<ol start=\"1\">\n<li>while\u00a0(1) {<\/li>\n<li>\u00a0\u00a0\/\/\u6bcf\u6b21\u53ea\u505a1000\u6761<\/li>\n<li>\u00a0\u00a0mysql_query(&#8220;DELETE FROM logs WHERE log_date &lt;= &#8216;2009-11-01&#8217; LIMIT 1000&#8221;);<\/li>\n<li>\u00a0\u00a0if\u00a0(mysql_affected_rows() == 0) {<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\/\/ \u6ca1\u5f97\u53ef\u5220\u4e86\uff0c\u9000\u51fa\uff01<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0break;<\/li>\n<li>\u00a0\u00a0}<\/li>\n<li>\u00a0\u00a0\/\/ \u6bcf\u6b21\u90fd\u8981\u4f11\u606f\u4e00\u4f1a\u513f<\/li>\n<li>\u00a0\u00a0usleep(50000);<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<p><strong>18. \u8d8a\u5c0f\u7684\u5217\u4f1a\u8d8a\u5feb<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5927\u591a\u6570\u7684\u6570\u636e\u5e93\u5f15\u64ce\u6765\u8bf4\uff0c\u786c\u76d8\u64cd\u4f5c\u53ef\u80fd\u662f\u6700\u91cd\u5927\u7684\u74f6\u9888\u3002\u6240\u4ee5\uff0c\u628a\u4f60\u7684\u6570\u636e\u53d8\u5f97\u7d27\u51d1\u4f1a\u5bf9\u8fd9\u79cd\u60c5\u51b5\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u56e0\u4e3a\u8fd9\u51cf\u5c11\u4e86\u5bf9\u786c\u76d8\u7684\u8bbf\u95ee\u3002\u53c2\u770b MySQL \u7684\u6587\u6863\u00a0<span style=\"color: #2970a6;\"><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/storage-requirements.html\" target=\"_blank\" rel=\"noopener noreferrer\">Storage Requirements<\/a><\/span>\u00a0\u67e5\u770b\u6240\u6709\u7684\u6570\u636e\u7c7b\u578b\u3002\u5982\u679c\u4e00\u4e2a\u8868\u53ea\u4f1a\u6709\u51e0\u5217\u7f62\u4e86\uff08\u6bd4\u5982\u8bf4\u5b57\u5178\u8868\uff0c\u914d\u7f6e\u8868\uff09\uff0c\u90a3\u4e48\uff0c\u6211\u4eec\u5c31\u6ca1\u6709\u7406\u7531\u4f7f\u7528 INT \u6765\u505a\u4e3b\u952e\uff0c\u4f7f\u7528 MEDIUMINT, SMALLINT \u6216\u662f\u66f4\u5c0f\u7684 TINYINT \u4f1a\u66f4\u7ecf\u6d4e\u4e00\u4e9b\u3002\u5982\u679c\u4f60\u4e0d\u9700\u8981\u8bb0\u5f55\u65f6\u95f4\uff0c\u4f7f\u7528 DATE \u8981\u6bd4 DATETIME \u597d\u5f97\u591a\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u9700\u8981\u7559\u591f\u8db3\u591f\u7684\u6269\u5c55\u7a7a\u95f4\uff0c\u4e0d\u7136\uff0c\u4f60\u65e5\u540e\u6765\u5e72\u8fd9\u4e2a\u4e8b\uff0c\u4f60\u4f1a\u6b7b\u7684\u5f88\u96be\u770b\uff0c\u53c2\u770b<span style=\"color: #2970a6;\"><a href=\"http:\/\/news.slashdot.org\/article.pl?sid=06\/11\/09\/1534204\" target=\"_blank\" rel=\"noopener noreferrer\">Slashdot\u7684\u4f8b\u5b50<\/a><\/span>\uff082009\u5e7411\u670806\u65e5\uff09\uff0c\u4e00\u4e2a\u7b80\u5355\u7684ALTER TABLE\u8bed\u53e5\u82b1\u4e863\u4e2a\u591a\u5c0f\u65f6\uff0c\u56e0\u4e3a\u91cc\u9762\u6709\u4e00\u5343\u516d\u767e\u4e07\u6761\u6570\u636e\u3002<\/p>\n<p><strong>19. \u9009\u62e9\u6b63\u786e\u7684\u5b58\u50a8\u5f15\u64ce<\/strong><br \/>\n\u5728 MySQL \u4e2d\u6709\u4e24\u4e2a\u5b58\u50a8\u5f15\u64ce MyISAM \u548c InnoDB\uff0c\u6bcf\u4e2a\u5f15\u64ce\u90fd\u6709\u5229\u6709\u5f0a\u3002\u9177\u58f3\u4ee5\u524d\u6587\u7ae0\u300a<span style=\"color: #2970a6;\"><a href=\"http:\/\/coolshell.cn\/articles\/652.html\" target=\"_blank\" rel=\"noopener noreferrer\">MySQL: InnoDB \u8fd8\u662f MyISAM?<\/a><\/span>\u300b\u8ba8\u8bba\u548c\u8fd9\u4e2a\u4e8b\u60c5\u3002MyISAM \u9002\u5408\u4e8e\u4e00\u4e9b\u9700\u8981\u5927\u91cf\u67e5\u8be2\u7684\u5e94\u7528\uff0c\u4f46\u5176\u5bf9\u4e8e\u6709\u5927\u91cf\u5199\u64cd\u4f5c\u5e76\u4e0d\u662f\u5f88\u597d\u3002\u751a\u81f3\u4f60\u53ea\u662f\u9700\u8981update\u4e00\u4e2a\u5b57\u6bb5\uff0c\u6574\u4e2a\u8868\u90fd\u4f1a\u88ab\u9501\u8d77\u6765\uff0c\u800c\u522b\u7684\u8fdb\u7a0b\uff0c\u5c31\u7b97\u662f\u8bfb\u8fdb\u7a0b\u90fd\u65e0\u6cd5\u64cd\u4f5c\u76f4\u5230\u8bfb\u64cd\u4f5c\u5b8c\u6210\u3002\u53e6\u5916\uff0cMyISAM \u5bf9\u4e8e SELECT COUNT(*) \u8fd9\u7c7b\u7684\u8ba1\u7b97\u662f\u8d85\u5feb\u65e0\u6bd4\u7684\u3002InnoDB \u7684\u8d8b\u52bf\u4f1a\u662f\u4e00\u4e2a\u975e\u5e38\u590d\u6742\u7684\u5b58\u50a8\u5f15\u64ce\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u5c0f\u7684\u5e94\u7528\uff0c\u5b83\u4f1a\u6bd4 MyISAM \u8fd8\u6162\u3002\u4ed6\u662f\u5b83\u652f\u6301\u201c\u884c\u9501\u201d \uff0c\u4e8e\u662f\u5728\u5199\u64cd\u4f5c\u6bd4\u8f83\u591a\u7684\u65f6\u5019\uff0c\u4f1a\u66f4\u4f18\u79c0\u3002\u5e76\u4e14\uff0c\u4ed6\u8fd8\u652f\u6301\u66f4\u591a\u7684\u9ad8\u7ea7\u5e94\u7528\uff0c\u6bd4\u5982\uff1a\u4e8b\u52a1\u3002\u4e0b\u9762\u662fMySQL\u7684\u624b\u518c<\/p>\n<ul>\n<li><span style=\"color: #2970a6;\"><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/myisam-storage-engine.html\" target=\"_blank\" rel=\"noopener noreferrer\">target=\u201d_blank\u201dMyISAM Storage Engine<\/a><\/span><\/li>\n<li><span style=\"color: #2970a6;\"><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/innodb.html\" target=\"_blank\" rel=\"noopener noreferrer\">InnoDB Storage Engine<\/a><\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: Arial, Verdana, 'BitStream vera Sans', Tahoma, Helvetica, sans-serif;\"><span style=\"color: #555555;\"><br \/>\n<\/span><\/span><br \/>\n<strong>20. \u4f7f\u7528\u4e00\u4e2a\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff08Object Relational Mapper\uff09<\/strong><\/p>\n<p>\u4f7f\u7528 ORM (Object Relational Mapper)\uff0c\u4f60\u80fd\u591f\u83b7\u5f97\u53ef\u9760\u7684\u6027\u80fd\u589e\u6da8\u3002\u4e00\u4e2aORM\u53ef\u4ee5\u505a\u7684\u6240\u6709\u4e8b\u60c5\uff0c\u4e5f\u80fd\u88ab\u624b\u52a8\u7684\u7f16\u5199\u51fa\u6765\u3002\u4f46\u662f\uff0c\u8fd9\u9700\u8981\u4e00\u4e2a\u9ad8\u7ea7\u4e13\u5bb6\u3002ORM \u7684\u6700\u91cd\u8981\u7684\u662f\u201cLazy Loading\u201d\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u6709\u5728\u9700\u8981\u7684\u53bb\u53d6\u503c\u7684\u65f6\u5019\u624d\u4f1a\u53bb\u771f\u6b63\u7684\u53bb\u505a\u3002\u4f46\u4f60\u4e5f\u9700\u8981\u5c0f\u5fc3\u8fd9\u79cd\u673a\u5236\u7684\u526f\u4f5c\u7528\uff0c\u56e0\u4e3a\u8fd9\u5f88\u6709\u53ef\u80fd\u4f1a\u56e0\u4e3a\u8981\u53bb\u521b\u5efa\u5f88\u591a\u5f88\u591a\u5c0f\u7684\u67e5\u8be2\u53cd\u800c\u4f1a\u964d\u4f4e\u6027\u80fd\u3002ORM \u8fd8\u53ef\u4ee5\u628a\u4f60\u7684SQL\u8bed\u53e5\u6253\u5305\u6210\u4e00\u4e2a\u4e8b\u52a1\uff0c\u8fd9\u4f1a\u6bd4\u5355\u72ec\u6267\u884c\u4ed6\u4eec\u5feb\u5f97\u591a\u5f97\u591a\u3002\u76ee\u524d\uff0c\u4e2a\u4eba\u6700\u559c\u6b22\u7684PHP\u7684ORM\u662f\uff1a<span style=\"color: #2970a6;\"><a href=\"http:\/\/www.doctrine-project.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Doctrine<\/a><\/span>\u3002<\/p>\n<p><strong>21. \u5c0f\u5fc3\u201c\u6c38\u4e45\u94fe\u63a5\u201d<\/strong><\/p>\n<p>\u201c\u6c38\u4e45\u94fe\u63a5\u201d\u7684\u76ee\u7684\u662f\u7528\u6765\u51cf\u5c11\u91cd\u65b0\u521b\u5efaMySQL\u94fe\u63a5\u7684\u6b21\u6570\u3002\u5f53\u4e00\u4e2a\u94fe\u63a5\u88ab\u521b\u5efa\u4e86\uff0c\u5b83\u4f1a\u6c38\u8fdc\u5904\u5728\u8fde\u63a5\u7684\u72b6\u6001\uff0c\u5c31\u7b97\u662f\u6570\u636e\u5e93\u64cd\u4f5c\u5df2\u7ecf\u7ed3\u675f\u4e86\u3002\u800c\u4e14\uff0c\u81ea\u4ece\u6211\u4eec\u7684Apache\u5f00\u59cb\u91cd\u7528\u5b83\u7684\u5b50\u8fdb\u7a0b\u540e\u2014\u2014\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e0b\u4e00\u6b21\u7684HTTP\u8bf7\u6c42\u4f1a\u91cd\u7528Apache\u7684\u5b50\u8fdb\u7a0b\uff0c\u5e76\u91cd\u7528\u76f8\u540c\u7684 MySQL \u94fe\u63a5\u3002<\/p>\n<ul>\n<li><span style=\"color: #2970a6;\"><a href=\"http:\/\/php.net\/manual\/en\/function.mysql-pconnect.php\" target=\"_blank\" rel=\"noopener noreferrer\">PHP\u624b\u518c\uff1amysql_pconnect()<\/a><\/span><\/li>\n<\/ul>\n<p>\u5728\u7406\u8bba\u4e0a\u6765\u8bf4\uff0c\u8fd9\u542c\u8d77\u6765\u975e\u5e38\u7684\u4e0d\u9519\u3002\u4f46\u662f\u4ece\u4e2a\u4eba\u7ecf\u9a8c\uff08\u4e5f\u662f\u5927\u591a\u6570\u4eba\u7684\uff09\u4e0a\u6765\u8bf4\uff0c\u8fd9\u4e2a\u529f\u80fd\u5236\u9020\u51fa\u6765\u7684\u9ebb\u70e6\u4e8b\u66f4\u591a\u3002\u56e0\u4e3a\uff0c\u4f60\u53ea\u6709\u6709\u9650\u7684\u94fe\u63a5\u6570\uff0c\u5185\u5b58\u95ee\u9898\uff0c\u6587\u4ef6\u53e5\u67c4\u6570\uff0c\u7b49\u7b49\u3002\u800c\u4e14\uff0cApache \u8fd0\u884c\u5728\u6781\u7aef\u5e76\u884c\u7684\u73af\u5883\u4e2d\uff0c\u4f1a\u521b\u5efa\u5f88\u591a\u5f88\u591a\u7684\u4e86\u8fdb\u7a0b\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u8fd9\u79cd\u201c\u6c38\u4e45\u94fe\u63a5\u201d\u7684\u673a\u5236\u5de5\u4f5c\u5730\u4e0d\u597d\u7684\u539f\u56e0\u3002\u5728\u4f60\u51b3\u5b9a\u8981\u4f7f\u7528\u201c\u6c38\u4e45\u94fe\u63a5\u201d\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u597d\u597d\u5730\u8003\u8651\u4e00\u4e0b\u4f60\u7684\u6574\u4e2a\u7cfb\u7edf\u7684\u67b6\u6784\u3002<\/p>\n<p>(\u5168\u6587\u5b8c)[\u8f6c\u8f7d]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1atutsplus\u00a0\u8bd1\u6587\uff1a\u9177\u58f3 \u4eca\u5929\uff0c\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u8d8a\u6765\u8d8a\u6210\u4e3a\u6574\u4e2a\u5e94\u7528\u7684\u6027\u80fd\u74f6\u9888\u4e86\uff0c\u8fd9\u70b9\u5bf9\u4e8eWeb\u5e94\u7528\u5c24\u5176\u660e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[57],"class_list":["post-870","post","type-post","status-publish","format-standard","hentry","category-sql","tag-mysql"],"_links":{"self":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=870"}],"version-history":[{"count":0,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts\/870\/revisions"}],"wp:attachment":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}