{"id":74,"date":"2019-01-21T16:36:34","date_gmt":"2019-01-21T08:36:34","guid":{"rendered":"http:\/\/www.anthonylee.cn\/myblog\/?p=74"},"modified":"2019-01-21T16:36:34","modified_gmt":"2019-01-21T08:36:34","slug":"mysql%e4%bc%98%e5%8c%9619%e6%9d%a1","status":"publish","type":"post","link":"http:\/\/www.anthonylee.cn\/myblog\/?p=74","title":{"rendered":"MySQL\u4f18\u531619\u6761"},"content":{"rendered":"<p data-spm-anchor-id=\"a2c4e.11153940.blogcont684044.i2.144fBYqYBYqY4a\">1\u3001EXPLAIN<\/p>\n<p>\u505aMySQL\u4f18\u5316\uff0c\u6211\u4eec\u8981\u5584\u7528EXPLAIN\u67e5\u770bSQL\u6267\u884c\u8ba1\u5212\u3002<\/p>\n<p>\u4e0b\u9762\u6765\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6807\u6ce8\uff081\u30012\u30013\u30014\u30015\uff09\u6211\u4eec\u8981\u91cd\u70b9\u5173\u6ce8\u7684\u6570\u636e\uff1a<\/p>\n<p><img title=\"image\" src=\"https:\/\/yqfile.alicdn.com\/551f766ed3e0d4d6889dd01258980da57cd9fd26.png\" alt=\"image\" \/><\/p>\n<p>type\u5217\uff0c\u8fde\u63a5\u7c7b\u578b\u3002\u4e00\u4e2a\u597d\u7684SQL\u8bed\u53e5\u81f3\u5c11\u8981\u8fbe\u5230range\u7ea7\u522b\u3002\u675c\u7edd\u51fa\u73b0all\u7ea7\u522b\u3002<\/p>\n<p>key\u5217\uff0c\u4f7f\u7528\u5230\u7684\u7d22\u5f15\u540d\u3002\u5982\u679c\u6ca1\u6709\u9009\u62e9\u7d22\u5f15\uff0c\u503c\u662fNULL\u3002\u53ef\u4ee5\u91c7\u53d6\u5f3a\u5236\u7d22\u5f15\u65b9\u5f0f\u3002<\/p>\n<p>key_len\u5217\uff0c\u7d22\u5f15\u957f\u5ea6\u3002<\/p>\n<p>rows\u5217\uff0c\u626b\u63cf\u884c\u6570\u3002\u8be5\u503c\u662f\u4e2a\u9884\u4f30\u503c\u3002<\/p>\n<p>extra\u5217\uff0c\u8be6\u7ec6\u8bf4\u660e\u3002\u6ce8\u610f\uff0c\u5e38\u89c1\u7684\u4e0d\u592a\u53cb\u597d\u7684\u503c\uff0c\u5982\u4e0b\uff1aUsing filesort\uff0cUsing temporary\u3002<\/p>\n<p>2\u3001SQL\u8bed\u53e5\u4e2dIN\u5305\u542b\u7684\u503c\u4e0d\u5e94\u8fc7\u591a<\/p>\n<p>MySQL\u5bf9\u4e8eIN\u505a\u4e86\u76f8\u5e94\u7684\u4f18\u5316\uff0c\u5373\u5c06IN\u4e2d\u7684\u5e38\u91cf\u5168\u90e8\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u91cc\u9762\uff0c\u800c\u4e14\u8fd9\u4e2a\u6570\u7ec4\u662f\u6392\u597d\u5e8f\u7684\u3002\u4f46\u662f\u5982\u679c\u6570\u503c\u8f83\u591a\uff0c\u4ea7\u751f\u7684\u6d88\u8017\u4e5f\u662f\u6bd4\u8f83\u5927\u7684\u3002\u518d\u4f8b\u5982\uff1aselect id from t where num in(1,2,3) \u5bf9\u4e8e\u8fde\u7eed\u7684\u6570\u503c\uff0c\u80fd\u7528between\u5c31\u4e0d\u8981\u7528in\u4e86\uff1b\u518d\u6216\u8005\u4f7f\u7528\u8fde\u63a5\u6765\u66ff\u6362\u3002<\/p>\n<p>3\u3001SELECT\u8bed\u53e5\u52a1\u5fc5\u6307\u660e\u5b57\u6bb5\u540d\u79f0<\/p>\n<p>SELECT*\u589e\u52a0\u5f88\u591a\u4e0d\u5fc5\u8981\u7684\u6d88\u8017\uff08CPU\u3001IO\u3001\u5185\u5b58\u3001\u7f51\u7edc\u5e26\u5bbd\uff09\uff1b\u589e\u52a0\u4e86\u4f7f\u7528\u8986\u76d6\u7d22\u5f15\u7684\u53ef\u80fd\u6027\uff1b\u5f53\u8868\u7ed3\u6784\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u524d\u65ad\u4e5f\u9700\u8981\u66f4\u65b0\u3002\u6240\u4ee5\u8981\u6c42\u76f4\u63a5\u5728select\u540e\u9762\u63a5\u4e0a\u5b57\u6bb5\u540d\u3002<\/p>\n<p>4\u3001\u5f53\u53ea\u9700\u8981\u4e00\u6761\u6570\u636e\u7684\u65f6\u5019\uff0c\u4f7f\u7528limit 1<\/p>\n<p>\u8fd9\u662f\u4e3a\u4e86\u4f7fEXPLAIN\u4e2dtype\u5217\u8fbe\u5230const\u7c7b\u578b<\/p>\n<p>5\u3001\u5982\u679c\u6392\u5e8f\u5b57\u6bb5\u6ca1\u6709\u7528\u5230\u7d22\u5f15\uff0c\u5c31\u5c3d\u91cf\u5c11\u6392\u5e8f<\/p>\n<p>6\u3001\u5982\u679c\u9650\u5236\u6761\u4ef6\u4e2d\u5176\u4ed6\u5b57\u6bb5\u6ca1\u6709\u7d22\u5f15\uff0c\u5c3d\u91cf\u5c11\u7528or<\/p>\n<p>or\u4e24\u8fb9\u7684\u5b57\u6bb5\u4e2d\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u4e0d\u662f\u7d22\u5f15\u5b57\u6bb5\uff0c\u800c\u5176\u4ed6\u6761\u4ef6\u4e5f\u4e0d\u662f\u7d22\u5f15\u5b57\u6bb5\uff0c\u4f1a\u9020\u6210\u8be5\u67e5\u8be2\u4e0d\u8d70\u7d22\u5f15\u7684\u60c5\u51b5\u3002\u5f88\u591a\u65f6\u5019\u4f7f\u7528union all\u6216\u8005\u662funion\uff08\u5fc5\u8981\u7684\u65f6\u5019\uff09\u7684\u65b9\u5f0f\u6765\u4ee3\u66ff\u201cor\u201d\u4f1a\u5f97\u5230\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<p>7\u3001\u5c3d\u91cf\u7528union all\u4ee3\u66ffunion<\/p>\n<p>union\u548cunion all\u7684\u5dee\u5f02\u4e3b\u8981\u662f\u524d\u8005\u9700\u8981\u5c06\u7ed3\u679c\u96c6\u5408\u5e76\u540e\u518d\u8fdb\u884c\u552f\u4e00\u6027\u8fc7\u6ee4\u64cd\u4f5c\uff0c\u8fd9\u5c31\u4f1a\u6d89\u53ca\u5230\u6392\u5e8f\uff0c\u589e\u52a0\u5927\u91cf\u7684CPU\u8fd0\u7b97\uff0c\u52a0\u5927\u8d44\u6e90\u6d88\u8017\u53ca\u5ef6\u8fdf\u3002\u5f53\u7136\uff0cunion all\u7684\u524d\u63d0\u6761\u4ef6\u662f\u4e24\u4e2a\u7ed3\u679c\u96c6\u6ca1\u6709\u91cd\u590d\u6570\u636e\u3002<\/p>\n<p>8\u3001\u4e0d\u4f7f\u7528ORDER BY RAND()<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">`dynamic`<\/span> <span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> <span class=\"hljs-keyword\">rand<\/span>() <span class=\"hljs-keyword\">limit<\/span> <span class=\"hljs-number\">1000<\/span>;<\/code><\/pre>\n<p>\u4e0a\u9762\u7684SQL\u8bed\u53e5\uff0c\u53ef\u4f18\u5316\u4e3a\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">`dynamic`<\/span> t1 <span class=\"hljs-keyword\">join<\/span> (<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">rand<\/span>() * (<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">max<\/span>(<span class=\"hljs-keyword\">id<\/span>) <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">`dynamic`<\/span>) <span class=\"hljs-keyword\">as<\/span> nid) t2 <span class=\"hljs-keyword\">on<\/span> t1.id &gt; t2.nidlimit <span class=\"hljs-number\">1000<\/span>;<\/code><\/pre>\n<p>9\u3001\u533a\u5206in\u548cexists\u3001not in\u548cnot exists<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> \u8868A <span class=\"hljs-keyword\">where<\/span> <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-keyword\">from<\/span> \u8868B)<\/code><\/pre>\n<p>\u4e0a\u9762SQL\u8bed\u53e5\u76f8\u5f53\u4e8e<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> \u8868A <span class=\"hljs-keyword\">where<\/span> <span class=\"hljs-keyword\">exists<\/span>(<span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> \u8868B <span class=\"hljs-keyword\">where<\/span> \u8868B.id=\u8868A.id)<\/code><\/pre>\n<p>\u533a\u5206in\u548cexists\u4e3b\u8981\u662f\u9020\u6210\u4e86\u9a71\u52a8\u987a\u5e8f\u7684\u6539\u53d8\uff08\u8fd9\u662f\u6027\u80fd\u53d8\u5316\u7684\u5173\u952e\uff09\uff0c\u5982\u679c\u662fexists\uff0c\u90a3\u4e48\u4ee5\u5916\u5c42\u8868\u4e3a\u9a71\u52a8\u8868\uff0c\u5148\u88ab\u8bbf\u95ee\uff0c\u5982\u679c\u662fIN\uff0c\u90a3\u4e48\u5148\u6267\u884c\u5b50\u67e5\u8be2\u3002\u6240\u4ee5IN\u9002\u5408\u4e8e\u5916\u8868\u5927\u800c\u5185\u8868\u5c0f\u7684\u60c5\u51b5\uff1bEXISTS\u9002\u5408\u4e8e\u5916\u8868\u5c0f\u800c\u5185\u8868\u5927\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5173\u4e8enot in\u548cnot exists\uff0c\u63a8\u8350\u4f7f\u7528not exists\uff0c\u4e0d\u4ec5\u4ec5\u662f\u6548\u7387\u95ee\u9898\uff0cnot in\u53ef\u80fd\u5b58\u5728\u903b\u8f91\u95ee\u9898\u3002\u5982\u4f55\u9ad8\u6548\u7684\u5199\u51fa\u4e00\u4e2a\u66ff\u4ee3not exists\u7684SQL\u8bed\u53e5\uff1f<\/p>\n<p>\u539fSQL\u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> colname \u2026 <span class=\"hljs-keyword\">from<\/span> A\u8868 <span class=\"hljs-keyword\">where<\/span> a.id <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-keyword\">in<\/span> (<span class=\"hljs-keyword\">select<\/span> b.id <span class=\"hljs-keyword\">from<\/span> B\u8868)<\/code><\/pre>\n<p>\u9ad8\u6548\u7684SQL\u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> colname \u2026 <span class=\"hljs-keyword\">from<\/span> A\u8868 <span class=\"hljs-keyword\">Left<\/span> <span class=\"hljs-keyword\">join<\/span> B\u8868 <span class=\"hljs-keyword\">on<\/span> <span class=\"hljs-keyword\">where<\/span> a.id = b.id <span class=\"hljs-keyword\">where<\/span> b.id <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-literal\">null<\/span><\/code><\/pre>\n<p>\u53d6\u51fa\u7684\u7ed3\u679c\u96c6\u5982\u4e0b\u56fe\u8868\u793a\uff0cA\u8868\u4e0d\u5728B\u8868\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n<p><img title=\"image\" src=\"https:\/\/yqfile.alicdn.com\/11398525fb5a941adc33eaba0f5c3bb7cef2a05b.png\" alt=\"image\" \/><\/p>\n<p>10\u3001\u4f7f\u7528\u5408\u7406\u7684\u5206\u9875\u65b9\u5f0f\u4ee5\u63d0\u9ad8\u5206\u9875\u7684\u6548\u7387<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">id<\/span>,<span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-keyword\">from<\/span> product <span class=\"hljs-keyword\">limit<\/span> <span class=\"hljs-number\">866613<\/span>, <span class=\"hljs-number\">20<\/span><\/code><\/pre>\n<p>\u4f7f\u7528\u4e0a\u8ff0SQL\u8bed\u53e5\u505a\u5206\u9875\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u6709\u4eba\u4f1a\u53d1\u73b0\uff0c\u968f\u7740\u8868\u6570\u636e\u91cf\u7684\u589e\u52a0\uff0c\u76f4\u63a5\u4f7f\u7528limit\u5206\u9875\u67e5\u8be2\u4f1a\u8d8a\u6765\u8d8a\u6162\u3002<\/p>\n<p>\u4f18\u5316\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a\u53ef\u4ee5\u53d6\u524d\u4e00\u9875\u7684\u6700\u5927\u884c\u6570\u7684id\uff0c\u7136\u540e\u6839\u636e\u8fd9\u4e2a\u6700\u5927\u7684id\u6765\u9650\u5236\u4e0b\u4e00\u9875\u7684\u8d77\u70b9\u3002\u6bd4\u5982\u6b64\u5217\u4e2d\uff0c\u4e0a\u4e00\u9875\u6700\u5927\u7684id\u662f866612\u3002SQL\u53ef\u4ee5\u91c7\u7528\u5982\u4e0b\u7684\u5199\u6cd5\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">id<\/span>,<span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-keyword\">from<\/span> product <span class=\"hljs-keyword\">where<\/span> <span class=\"hljs-keyword\">id<\/span>&gt; <span class=\"hljs-number\">866612<\/span> <span class=\"hljs-keyword\">limit<\/span> <span class=\"hljs-number\">20<\/span><\/code><\/pre>\n<p>11\u3001\u5206\u6bb5\u67e5\u8be2<\/p>\n<p>\u5728\u4e00\u4e9b\u7528\u6237\u9009\u62e9\u9875\u9762\u4e2d\uff0c\u53ef\u80fd\u4e00\u4e9b\u7528\u6237\u9009\u62e9\u7684\u65f6\u95f4\u8303\u56f4\u8fc7\u5927\uff0c\u9020\u6210\u67e5\u8be2\u7f13\u6162\u3002\u4e3b\u8981\u7684\u539f\u56e0\u662f\u626b\u63cf\u884c\u6570\u8fc7\u591a\u3002\u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u901a\u8fc7\u7a0b\u5e8f\uff0c\u5206\u6bb5\u8fdb\u884c\u67e5\u8be2\uff0c\u5faa\u73af\u904d\u5386\uff0c\u5c06\u7ed3\u679c\u5408\u5e76\u5904\u7406\u8fdb\u884c\u5c55\u793a\u3002<\/p>\n<p>\u5982\u4e0b\u56fe\u8fd9\u4e2aSQL\u8bed\u53e5\uff0c\u626b\u63cf\u7684\u884c\u6570\u6210\u767e\u4e07\u7ea7\u4ee5\u4e0a\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u4f7f\u7528\u5206\u6bb5\u67e5\u8be2\uff1a<\/p>\n<p><img title=\"image\" src=\"https:\/\/yqfile.alicdn.com\/4d8b576d7a87a40c201910cf4109fc4c4bac5c49.png\" alt=\"image\" \/><\/p>\n<p>12\u3001\u907f\u514d\u5728where\u5b50\u53e5\u4e2d\u5bf9\u5b57\u6bb5\u8fdb\u884cnull\u503c\u5224\u65ad<\/p>\n<p>\u5bf9\u4e8enull\u7684\u5224\u65ad\u4f1a\u5bfc\u81f4\u5f15\u64ce\u653e\u5f03\u4f7f\u7528\u7d22\u5f15\u800c\u8fdb\u884c\u5168\u8868\u626b\u63cf\u3002<\/p>\n<p>13\u3001\u4e0d\u5efa\u8bae\u4f7f\u7528%\u524d\u7f00\u6a21\u7cca\u67e5\u8be2<\/p>\n<p>\u4f8b\u5982LIKE\u201c%name\u201d\u6216\u8005LIKE\u201c%name%\u201d\uff0c\u8fd9\u79cd\u67e5\u8be2\u4f1a\u5bfc\u81f4\u7d22\u5f15\u5931\u6548\u800c\u8fdb\u884c\u5168\u8868\u626b\u63cf\u3002\u4f46\u662f\u53ef\u4ee5\u4f7f\u7528LIKE \u201cname%\u201d\u3002<\/p>\n<p>\u90a3\u5982\u4f55\u67e5\u8be2%name%\uff1f<\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u867d\u7136\u7ed9secret\u5b57\u6bb5\u6dfb\u52a0\u4e86\u7d22\u5f15\uff0c\u4f46\u5728explain\u7ed3\u679c\u5e76\u6ca1\u6709\u4f7f\u7528\uff1a<\/p>\n<p><img title=\"image\" src=\"https:\/\/yqfile.alicdn.com\/c8eca13a61e27d4c839465daacee996413588b9c.png\" alt=\"image\" \/><\/p>\n<p>\u90a3\u4e48\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff0c\u7b54\u6848\uff1a\u4f7f\u7528\u5168\u6587\u7d22\u5f15\u3002<\/p>\n<p>\u5728\u6211\u4eec\u67e5\u8be2\u4e2d\u7ecf\u5e38\u4f1a\u7528\u5230select id,fnum,fdst from dynamic_201606 where user_name like &#8216;%zhangsan%&#8217;; \u3002\u8fd9\u6837\u7684\u8bed\u53e5\uff0c\u666e\u901a\u7d22\u5f15\u662f\u65e0\u6cd5\u6ee1\u8db3\u67e5\u8be2\u9700\u6c42\u7684\u3002\u5e86\u5e78\u7684\u662f\u5728MySQL\u4e2d\uff0c\u6709\u5168\u6587\u7d22\u5f15\u6765\u5e2e\u52a9\u6211\u4eec\u3002<\/p>\n<p>\u521b\u5efa\u5168\u6587\u7d22\u5f15\u7684SQL\u8bed\u6cd5\u662f\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-string\">`dynamic_201606`<\/span> <span class=\"hljs-keyword\">ADD<\/span> FULLTEXT <span class=\"hljs-keyword\">INDEX<\/span> <span class=\"hljs-string\">`idx_user_name`<\/span> (<span class=\"hljs-string\">`user_name`<\/span>);\r\n<\/code><\/pre>\n<p>\u4f7f\u7528\u5168\u6587\u7d22\u5f15\u7684SQL\u8bed\u53e5\u662f\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> <span class=\"hljs-keyword\">id<\/span>,fnum,fdst <span class=\"hljs-keyword\">from<\/span> dynamic_201606 <span class=\"hljs-keyword\">where<\/span> <span class=\"hljs-keyword\">match<\/span>(user_name) against(<span class=\"hljs-string\">'zhangsan'<\/span> <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">boolean<\/span> <span class=\"hljs-keyword\">mode<\/span>);<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5728\u9700\u8981\u521b\u5efa\u5168\u6587\u7d22\u5f15\u4e4b\u524d\uff0c\u8bf7\u8054\u7cfbDBA\u786e\u5b9a\u80fd\u5426\u521b\u5efa\u3002\u540c\u65f6\u9700\u8981\u6ce8\u610f\u7684\u662f\u67e5\u8be2\u8bed\u53e5\u7684\u5199\u6cd5\u4e0e\u666e\u901a\u7d22\u5f15\u7684\u533a\u522b\u3002<\/p>\n<p>14\u3001\u907f\u514d\u5728where\u5b50\u53e5\u4e2d\u5bf9\u5b57\u6bb5\u8fdb\u884c\u8868\u8fbe\u5f0f\u64cd\u4f5c<\/p>\n<p>\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> user_id,user_project <span class=\"hljs-keyword\">from<\/span> user_base <span class=\"hljs-keyword\">where<\/span> age*<span class=\"hljs-number\">2<\/span>=<span class=\"hljs-number\">36<\/span>;<\/code><\/pre>\n<p>\u4e2d\u5bf9\u5b57\u6bb5\u5c31\u884c\u4e86\u7b97\u672f\u8fd0\u7b97\uff0c\u8fd9\u4f1a\u9020\u6210\u5f15\u64ce\u653e\u5f03\u4f7f\u7528\u7d22\u5f15\uff0c\u5efa\u8bae\u6539\u6210\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> user_id,user_project <span class=\"hljs-keyword\">from<\/span> user_base <span class=\"hljs-keyword\">where<\/span> age=<span class=\"hljs-number\">36<\/span>\/<span class=\"hljs-number\">2<\/span>;<\/code><\/pre>\n<p>15\u3001\u907f\u514d\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/p>\n<p>where\u5b50\u53e5\u4e2d\u51fa\u73b0column\u5b57\u6bb5\u7684\u7c7b\u578b\u548c\u4f20\u5165\u7684\u53c2\u6570\u7c7b\u578b\u4e0d\u4e00\u81f4\u7684\u65f6\u5019\u53d1\u751f\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u5efa\u8bae\u5148\u786e\u5b9awhere\u4e2d\u7684\u53c2\u6570\u7c7b\u578b\u3002<\/p>\n<p>16\u3001\u5bf9\u4e8e\u8054\u5408\u7d22\u5f15\u6765\u8bf4\uff0c\u8981\u9075\u5b88\u6700\u5de6\u524d\u7f00\u6cd5\u5219<\/p>\n<p>\u4e3e\u5217\u6765\u8bf4\u7d22\u5f15\u542b\u6709\u5b57\u6bb5id\u3001name\u3001school\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528id\u5b57\u6bb5\uff0c\u4e5f\u53ef\u4ee5id\u3001name\u8fd9\u6837\u7684\u987a\u5e8f\uff0c\u4f46\u662fname;school\u90fd\u65e0\u6cd5\u4f7f\u7528\u8fd9\u4e2a\u7d22\u5f15\u3002\u6240\u4ee5\u5728\u521b\u5efa\u8054\u5408\u7d22\u5f15\u7684\u65f6\u5019\u4e00\u5b9a\u8981\u6ce8\u610f\u7d22\u5f15\u5b57\u6bb5\u987a\u5e8f\uff0c\u5e38\u7528\u7684\u67e5\u8be2\u5b57\u6bb5\u653e\u5728\u6700\u524d\u9762\u3002<\/p>\n<p>17\u3001\u5fc5\u8981\u65f6\u53ef\u4ee5\u4f7f\u7528force index\u6765\u5f3a\u5236\u67e5\u8be2\u8d70\u67d0\u4e2a\u7d22\u5f15<\/p>\n<p>\u6709\u7684\u65f6\u5019MySQL\u4f18\u5316\u5668\u91c7\u53d6\u5b83\u8ba4\u4e3a\u5408\u9002\u7684\u7d22\u5f15\u6765\u68c0\u7d22SQL\u8bed\u53e5\uff0c\u4f46\u662f\u53ef\u80fd\u5b83\u6240\u91c7\u7528\u7684\u7d22\u5f15\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u8fd9\u65f6\u5c31\u53ef\u4ee5\u91c7\u7528forceindex\u6765\u5f3a\u5236\u4f18\u5316\u5668\u4f7f\u7528\u6211\u4eec\u5236\u5b9a\u7684\u7d22\u5f15\u3002<\/p>\n<p>18\u3001\u6ce8\u610f\u8303\u56f4\u67e5\u8be2\u8bed\u53e5<\/p>\n<p>\u5bf9\u4e8e\u8054\u5408\u7d22\u5f15\u6765\u8bf4\uff0c\u5982\u679c\u5b58\u5728\u8303\u56f4\u67e5\u8be2\uff0c\u6bd4\u5982between\u3001&gt;\u3001&lt;\u7b49\u6761\u4ef6\u65f6\uff0c\u4f1a\u9020\u6210\u540e\u9762\u7684\u7d22\u5f15\u5b57\u6bb5\u5931\u6548\u3002<\/p>\n<p>19\u3001\u5173\u4e8eJOIN\u4f18\u5316<\/p>\n<p><img title=\"image\" src=\"https:\/\/yqfile.alicdn.com\/0e458b85b2c76e82eb9f0fb66de479c5b687cdd5.png\" alt=\"image\" \/><\/p>\n<p>LEFT JOIN A\u8868\u4e3a\u9a71\u52a8\u8868\uff0cINNER JOIN MySQL\u4f1a\u81ea\u52a8\u627e\u51fa\u90a3\u4e2a\u6570\u636e\u5c11\u7684\u8868\u4f5c\u7528\u9a71\u52a8\u8868\uff0cRIGHT JOIN B\u8868\u4e3a\u9a71\u52a8\u8868\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>1\uff09MySQL\u4e2d\u6ca1\u6709full join\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u6765\u89e3\u51b3\uff1a<\/p>\n<pre><code class=\"hljs sql\"><span class=\"hljs-keyword\">select<\/span> * <span class=\"hljs-keyword\">from<\/span> A <span class=\"hljs-keyword\">left<\/span> <span class=\"hljs-keyword\">join<\/span> B <span class=\"hljs-keyword\">on<\/span> B.name = A.namewhere B.name <span class=\"hljs-keyword\">is<\/span> nullunion allselect * <span class=\"hljs-keyword\">from<\/span> B;<\/code><\/pre>\n<p>2\uff09\u5c3d\u91cf\u4f7f\u7528inner join\uff0c\u907f\u514dleft join\uff1a<\/p>\n<p>\u53c2\u4e0e\u8054\u5408\u67e5\u8be2\u7684\u8868\u81f3\u5c11\u4e3a2\u5f20\u8868\uff0c\u4e00\u822c\u90fd\u5b58\u5728\u5927\u5c0f\u4e4b\u5206\u3002\u5982\u679c\u8fde\u63a5\u65b9\u5f0f\u662finner join\uff0c\u5728\u6ca1\u6709\u5176\u4ed6\u8fc7\u6ee4\u6761\u4ef6\u7684\u60c5\u51b5\u4e0bMySQL\u4f1a\u81ea\u52a8\u9009\u62e9\u5c0f\u8868\u4f5c\u4e3a\u9a71\u52a8\u8868\uff0c\u4f46\u662fleft join\u5728\u9a71\u52a8\u8868\u7684\u9009\u62e9\u4e0a\u9075\u5faa\u7684\u662f\u5de6\u8fb9\u9a71\u52a8\u53f3\u8fb9\u7684\u539f\u5219\uff0c\u5373left join\u5de6\u8fb9\u7684\u8868\u540d\u4e3a\u9a71\u52a8\u8868\u3002<\/p>\n<p>3\uff09\u5408\u7406\u5229\u7528\u7d22\u5f15\uff1a<\/p>\n<p>\u88ab\u9a71\u52a8\u8868\u7684\u7d22\u5f15\u5b57\u6bb5\u4f5c\u4e3aon\u7684\u9650\u5236\u5b57\u6bb5\u3002<\/p>\n<p>4\uff09\u5229\u7528\u5c0f\u8868\u53bb\u9a71\u52a8\u5927\u8868\uff1a<\/p>\n<p><img title=\"image\" src=\"https:\/\/yqfile.alicdn.com\/9fa98a8644c5eb054626df6691e9af68c1dea816.png\" alt=\"image\" \/><\/p>\n<p>\u4ece\u539f\u7406\u56fe\u80fd\u591f\u76f4\u89c2\u7684\u770b\u51fa\u5982\u679c\u80fd\u591f\u51cf\u5c11\u9a71\u52a8\u8868\u7684\u8bdd\uff0c\u51cf\u5c11\u5d4c\u5957\u5faa\u73af\u4e2d\u7684\u5faa\u73af\u6b21\u6570\uff0c\u4ee5\u51cf\u5c11 IO\u603b\u91cf\u53caCPU\u8fd0\u7b97\u7684\u6b21\u6570\u3002<\/p>\n<p>5\uff09\u5de7\u7528STRAIGHT_JOIN\uff1a<\/p>\n<p>inner join\u662f\u7531MySQL\u9009\u62e9\u9a71\u52a8\u8868\uff0c\u4f46\u662f\u6709\u4e9b\u7279\u6b8a\u60c5\u51b5\u9700\u8981\u9009\u62e9\u53e6\u4e2a\u8868\u4f5c\u4e3a\u9a71\u52a8\u8868\uff0c\u6bd4\u5982\u6709group by\u3001order by\u7b49\u300cUsing filesort\u300d\u3001\u300cUsing temporary\u300d\u65f6\u3002STRAIGHT_JOIN\u6765\u5f3a\u5236\u8fde\u63a5\u987a\u5e8f\uff0c\u5728STRAIGHT_JOIN\u5de6\u8fb9\u7684\u8868\u540d\u5c31\u662f\u9a71\u52a8\u8868\uff0c\u53f3\u8fb9\u5219\u662f\u88ab\u9a71\u52a8\u8868\u3002\u5728\u4f7f\u7528STRAIGHT_JOIN\u6709\u4e2a\u524d\u63d0\u6761\u4ef6\u662f\u8be5\u67e5\u8be2\u662f\u5185\u8fde\u63a5\uff0c\u4e5f\u5c31\u662finner join\u3002\u5176\u4ed6\u94fe\u63a5\u4e0d\u63a8\u8350\u4f7f\u7528STRAIGHT_JOIN\uff0c\u5426\u5219\u53ef\u80fd\u9020\u6210\u67e5\u8be2\u7ed3\u679c\u4e0d\u51c6\u786e\u3002<\/p>\n<p><img title=\"image\" src=\"https:\/\/yqfile.alicdn.com\/3171f01e411c127170379cbc7e853ce0ff19edae.png\" alt=\"image\" \/><\/p>\n<p>\u8fd9\u4e2a\u65b9\u5f0f\u6709\u65f6\u80fd\u51cf\u5c113\u500d\u7684\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001EXPLAIN \u505aMySQL\u4f18\u5316\uff0c\u6211\u4eec\u8981\u5584\u7528EXPLAIN\u67e5\u770bSQL\u6267\u884c\u8ba1\u5212\u3002 \u4e0b\u9762\u6765\u4e2a\u7b80\u5355\u7684\u793a&hellip; <a href=\"http:\/\/www.anthonylee.cn\/myblog\/?p=74\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">MySQL\u4f18\u531619\u6761<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts\/74"}],"collection":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":1,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":75,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions\/75"}],"wp:attachment":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}