{"id":3935,"date":"2012-12-11T16:00:00","date_gmt":"2012-12-11T08:00:00","guid":{"rendered":"http:\/\/www.orczhou.com\/?p=3935"},"modified":"2016-07-20T17:52:57","modified_gmt":"2016-07-20T09:52:57","slug":"mysql-join-order-complex-or-not","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2012\/12\/mysql-join-order-complex-or-not\/","title":{"rendered":"MySQL\u6e90\u7801:JOIN\u987a\u5e8f\u9009\u62e9\u7684\u590d\u6742\u5ea6"},"content":{"rendered":"<p>\u5728\u770bMySQL\u4f18\u5316\u5668\u4ee3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u5e94\u8be5\u662f\u76f8\u5bf9\u8f83\u7b80\u5355\/\u4ee3\u7801\u8f83\u6e05\u6670\u7684\u90e8\u5206\u4e86\u3002MySQL\u4f18\u5316\u5668\u6709\u4e24\u4e2a\u81ea\u7531\u5ea6\uff1a\u5355\u8868\u8bbf\u95ee\u65b9\u5f0f\uff0c\u591a\u8868\u987a\u5e8f\u9009\u62e9\u3002\u524d\u6587\u5df2\u7ecf\u4ecb\u7ecd\u8fc7MySQL\u5355\u8868\u8bbf\u95ee\u7684\u4e00\u4e9b\u8003\u91cf(ref\/range\u7b49)\uff0c\u672c\u6587\u5c06\u4ecb\u7ecdJOIN\u5728\u987a\u5e8f\u9009\u62e9\u4e0a\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002<\/p>\n<p>\u5f53\u6709\u591a\u4e2a\u8868\u9700\u8981JOIN\u7684\u65f6\u5019\uff0cMySQL\u9996\u5148\u4f1a\u5904\u7406\u4e24\u7c7b\u7279\u6b8a\u60c5\u51b5\uff0c\u4e00\u4e2a\u662f\u5e38\u6570\u8868\uff0c\u4e00\u4e2a\u662f\u7531\u4e8e\u5916\u8fde\u63a5\u5bfc\u81f4\u987a\u5e8f\u4f9d\u8d56\u5173\u7cfb\u3002\u524d\u8005\u603b\u662f\u653e\u5728\u5173\u8054\u7684\u6700\u524d\u9762\uff0c\u540e\u8005\u4f1a\u5728\u904d\u5386\u7684\u65f6\u5019\u8003\u8651\u3002\u672c\u6587\u5c06\u5ffd\u7565\u4e0a\u9762\u4e24\u70b9\uff0c\u4ece\u8f83\u5b8f\u89c2\u89d2\u5ea6\u770bJOIN\u987a\u5e8f\u9009\u62e9\u65f6\u5019\u7684\u590d\u6742\u5ea6\u3002<!--more--><\/p>\n<p>\u5728\u8bbe\u7f6e\u4e86\u53c2\u6570prune_level(\u9ed8\u8ba4\u8bbe\u7f6e)\u540e\uff0cMySQL\u4f1a\u4f7f\u7528&#8221;\u8f83\u4e3a\u542f\u53d1\u5f0f&#8221;\u7684\u65b9\u5f0f\u5ffd\u7565\u4e00\u4e9b\u6267\u884c\u8ba1\u5212\u3002\u5982\u679c\u672a\u8bbe\u7f6e\uff0c\u5219\u4f7f\u7528\u4e86\u7a77\u4e3e\u83b7\u53d6&#8221;\u6700\u4f18&#8221;\u7684\u6267\u884c\u8ba1\u5212\u3002<\/p>\n\n<h3>1. \u6709\u9650\u7a77\u4e3e<\/h3>\n<p>\u5728MySQL\u6253\u5f00\u53c2\u6570prune_level(\u9ed8\u8ba4\u6253\u5f00)\u65f6\uff0c\u4f1a\u901a\u8fc7\u4e00\u4e2a&#8221;\u5077\u61d2&#8221;\u6280\u5de7\u6765\u8df3\u8fc7\u67d0\u4e9b\u770b\u4f3c\u6d88\u8017\u8f83\u5927\u6267\u884c\u8ba1\u5212\uff0c\u53ef\u4ee5\u53c2\u8003<a href=\"#2_MySQL\">\u5077\u61d2\u7684MySQL<\/a>\u3002<\/p>\n<p>\u867d\u7136\u4f1a&#8221;\u5077\u61d2&#8221;\u7684\u8df3\u8fc7\u67d0\u4e9b\u6267\u884c\u8ba1\u5212\uff0c\u4f46\u662fMySQL\u4ecd\u7136\u4f1a\u6309\u7167\u7a77\u4e3e\u7684\u65b9\u5f0f\u63a2\u7d22\uff0c\u8bf4&#8221;\u6709\u9650&#8221;\u662f\u6307\uff0c\u5f53\u5173\u8054\u8868\u7684\u6570\u91cf\u8d85\u8fc763\u65f6(search_depth\u7684\u9ed8\u8ba4\u503c)\uff0c\u8fbe\u5230\u6700\u5927\u6df1\u5ea6\uff0c MySQL\u5c06\u5206\u591a\u4e2a\u9636\u6bb5\u7a77\u4e3e\u3002\u5f53\u5173\u8054\u8868\u7684\u6570\u91cf\u8f83\u5c11\u7684\u65f6\u5019(\u5c0f\u4e8esearch_depth)\uff0cMySQL\u4f1a\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u4e2aJOIN\u987a\u5e8f\u7684\u6210\u672c\uff0c\u9009\u62e9\u6210\u672c\u6700\u4f4e\u7684\u4f5c\u4e3a\u5176\u6267\u884c\u8ba1\u5212\u3002\u5173\u4e8e\u8fd9\u90e8\u5206\u7684\u7b97\u6cd5\u590d\u6742\u5ea6\uff0c\u5728\u4ee3\u7801\u6ce8\u91ca\u4e2d\u6709\u8f83\u4e3a\u8be6\u7ec6\u7684\u63cf\u8ff0\uff0c\u5efa\u8bae\u9605\u8bfb\u51fd\u6570greedy_search\u7684\u6ce8\u91ca\u5148\u3002\u4e0b\u9762\u662f\u6ce8\u91ca\u90e8\u5206\u7684\u4e24\u6bb5\u4f2a\u4ee3\u7801\uff0c\u5f88\u597d\u7684\u63cf\u8ff0\u4e86\u6574\u4e2a\u8fc7\u7a0b\uff1a<\/p>\n<h4>1.1 greedy_search<\/h4>\n<pre><blockquote> 4997     procedure greedy_search                                                                         \r\n 4998     input: remaining_tables                                                                         \r\n 4999     output: pplan;                                                                                  \r\n 5000     {                                                                                               \r\n 5001       pplan = <>;                                                                                   \r\n 5002       do {                                                                                          \r\n 5003         (t, a) = best_extension(pplan, remaining_tables);                                           \r\n 5004         pplan = concat(pplan, (t, a));                                                              \r\n 5005         remaining_tables = remaining_tables - t;                                                    \r\n 5006       } while (remaining_tables != {})                                                              \r\n 5007       return pplan;                                                                                 \r\n 5008     }<\/blockquote><\/pre>\n<p>\u8fd9\u91cc\u7684(t , a)\u8868\u793a\uff0c\u6bcf\u6b21best_extension\u8fd4\u56de\u4e0b\u4e00\u4e2a\u9700\u8981JOIN\u7684\u8868t\uff0c\u5e76\u4e14\u786e\u5b9a\u7684\u8bbf\u95ee\u65b9\u5f0f\u662fa\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6267\u884c\u8ba1\u5212\u7684\u6269\u5c55\u7531\u51fd\u6570best_extension\uff0c\u521d\u59cbpplan\u4e3a\u7a7a\uff0cdo\u5faa\u73af\u7ed3\u675f\u8f93\u51fa\u6700\u7ec8\u7684\u6267\u884c\u8ba1\u5212\u3002<\/p>\n<h4>1.2 best_extension<\/h4>\n<p>best_extension\u4e2d\u8c03\u7528\u51fd\u6570best_extension_by_limited_search\u5b8c\u6210\u9012\u5f52\u904d\u5386\uff0c\u5176\u8f93\u5165\u662f\u90e8\u5206\u6267\u884c\u8ba1\u5212(pplan)\u548c\u5b83\u7684\u6210\u672c\uff0c\u51fd\u6570\u76ee\u7684\u662f\u627e\u5230\u4e0b\u4e00\u4e2a\u5173\u8054\u7684\u8868\u3002\u601d\u8def\u5f88\u7b80\u5355\uff0c\u904d\u5386\u6240\u6709\u5269\u4f59\u8868\uff0c\u5bf9\u6bcf\u4e00\u4e2a\u8868\uff0c\u8ba1\u7b97\u5bf9\u5e94\u7684&#8221;\u5c40\u90e8&#8221;\u6700\u4f18\u6267\u884c\u8ba1\u5212\uff0c\u5f53\u7136\u8ba1\u7b97\u8fd9\u4e2a\u201c\u5c40\u90e8\u201d\u6700\u4f18\u4ecd\u7136\u662f\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6240\u4ee5\u8fd9\u662f\u4e00\u4e2a\u6df1\u5ea6\u4f18\u5148\u7684\u904d\u5386\u3002<\/p>\n<p>\u4f2a\u4ee3\u7801(\u662f\u4e0d\u662f\u53c8\u6709\u4eba\u8bf4\u6211\u603b\u8d34\u4ee3\u7801\u4e86)\uff1a<\/p>\n<pre><blockquote> 5171     @code\r\n 5172     procedure best_extension_by_limited_search(\r\n 5173       pplan in,             \/\/ in, partial plan of tables-joined-so-far\r\n 5174       pplan_cost,           \/\/ in, cost of pplan\r\n 5175       remaining_tables,     \/\/ in, set of tables not referenced in pplan\r\n 5176       best_plan_so_far,     \/\/ in\/out, best plan found so far\r\n 5177       best_plan_so_far_cost,\/\/ in\/out, cost of best_plan_so_far\r\n 5178       search_depth)         \/\/ in, maximum size of the plans being considered\r\n 5179     {\r\n 5180       for each table T from remaining_tables\r\n 5181       {\r\n 5182         \/\/ Calculate the cost of using table T as above\r\n 5183         cost = complex-series-of-calculations;\r\n 5184 \r\n 5185         \/\/ Add the cost to the cost so far.\r\n 5186         pplan_cost+= cost;\r\n 5187 \r\n 5188         if (pplan_cost >= best_plan_so_far_cost)\r\n 5189           \/\/ pplan_cost already too great, stop search\r\n 5190           continue;\r\n 5191 \r\n 5192         pplan= expand pplan by best_access_method;\r\n 5193         remaining_tables= remaining_tables - table T;\r\n 5194         if (remaining_tables is not an empty set\r\n 5195             and\r\n 5196             search_depth > 1)\r\n 5197         {\r\n 5198           best_extension_by_limited_search(pplan, pplan_cost,\r\n 5199                                            remaining_tables,\r\n 5200                                            best_plan_so_far,\r\n 5201                                            best_plan_so_far_cost,\r\n 5202                                            search_depth - 1);\r\n 5203         }\r\n 5204         else\r\n 5205         {\r\n 5206           best_plan_so_far_cost= pplan_cost;\r\n 5207           best_plan_so_far= pplan;\r\n 5208         }\r\n 5209       }\r\n 5210     }\r\n 5211     @endcode\r\n<\/blockquote><\/pre>\n<p>\u4e00\u4e2a\u8bf4\u660e\uff1a\u5728\u6bcf\u6b21\u904d\u5386\u7684\u65f6\u5019\uff0c\u4e00\u65e6\u53d1\u73b0\u6210\u672c\u5927\u4e8e\u5f53\u524d\u7684\u6700\u4f18\u6210\u672c\uff0c\u5219\u653e\u5f03\uff0c\u4e0d\u518d\u7ee7\u7eed\u6df1\u5165\u3002<\/p>\n<h4>1.3 \u7b80\u5355\u7684\u5c0f\u7ed3<\/h4>\n<pre><blockquote>\u51fd\u6570\u7684\u8f93\u5165\uff1a\r\n\t\u90e8\u5206\u6267\u884c\u8ba1\u5212 partial plan\r\n\tN\u4e2a\u5269\u4f59\u8868\r\n\u51fd\u6570\u8f93\u51fa\uff1a\r\n\t\u5f53 N <= search_depth\uff0c\u8fd4\u56de\u5269\u4f59\u8868\u7684\u6700\u4f18\u6267\u884c\u8ba1\u5212\uff0c\u5e76\u548c\u524d\u9762\u7684\u90e8\u5206\u6267\u884c\u8ba1\u5212\u5408\u5e76\r\n\t\u5f53 N >  search_depth\uff0c\u8fd4\u56desearch_depth\u4e2a\u8868\u7684\u6700\u4f18\u6267\u884c\u8ba1\u5212\uff0c\u5e76\u5408\u5e76\u5230\u90e8\u5206\u6267\u884c\u8ba1\u5212\r\n\t\t\u9012\u5f52\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u8f93\u5165\u4e3a\uff1a\u5f53\u524d\u90e8\u5206\u6267\u884c\u8ba1\u5212   \u5269\u4f59\u8868N-depth<\/blockquote><\/pre>\n<h4>1.4 \u590d\u6742\u5ea6\u5206\u6790<\/h4>\n\n<p>\u5047\u8bbe\u9700\u8981\u5173\u8054\u7684\u8868\u4e00\u5171\u6709N\u4e2a\uff0c\u641c\u7d22\u6df1\u5ea6\u6700\u5927\u4e3adepth\uff0c\u90a3\u4e48\u7a77\u4e3e\u7684\u590d\u6742\u5ea6\u4e3a(\u8fd9\u662f\u4e00\u4e2a\u7c97\u7565\u7684\u8ba1\u7b97)\uff1a<\/p>\n<p>\u5f53 N &lt; depth \u65f6\uff0c\u4e3a\uff1a<\/p>\n<p>    \\[O(N!)\\]<\/p>\n<p>\u5f53 N &gt;= depth\u65f6\uff0c\u4e3a(\u8fd1\u4f3c)\uff1a\\[O(\\frac{N*N^{depth}}{depth})\\]<\/p>\n<p>\u7b2c\u4e00\u79cd\u60c5\u51b5\uff0c\u662f\u7b80\u5355\uff0c\u5c31\u662f\u4e00\u4e2a\u5b8c\u5168\u7684\u7a77\u4e3e\u3002 <\/p>\n<p>\u7b2c\u4e8c\u79cd\u60c5\u51b5\u8bf4\u660e\uff1a \u6bcf\u6b21\u4f18\u5316\u6df1\u5ea6\u662f\\(depth\\)\uff0c\u6240\u4ee5\uff0c\u9700\u8981\u8fdb\u884c\\(\\frac{N}{depth}\u6b21\u4f18\u5316\\)\uff1b\u6bcf\u6b21\u7684\u590d\u6742\u5ea6\u4e3a\uff1a\\((N-1)*(N-2)&#8230;(N-depth)\\)\uff1b\u603b\u7684\u590d\u6742\u5ea6\u4e3a\uff1a\\[O(\\frac{N}{depth}*N^{depth}) = O(\\frac{N*N^{depth}}{depth})\\]<\/p>\n<p><!-- <a href=\"http:\/\/www.flickr.com\/photos\/26825745@N06\/8262706893\/\" title=\"join-complex by orczhou, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm9.staticflickr.com\/8201\/8262706893_5a7fdf658e.jpg\" width=\"500\" height=\"204\" alt=\"join-complex\"><\/a> --><\/p>\n<p>\u6240\u4ee5\uff0c\u590d\u6742\u5ea6\u53ef\u80fd\u662f\\(O(\\frac{N}{depth}*N^{depth}) = O(\\frac{N*N^{depth}}{depth})\\)\u3002\u5982\u679csearch_depth > N \u90a3\u4e48\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5c31\u662fO(N!)\u3002\u901a\u5e38MySQL\u4f18\u5316\u5668\u5206\u6790\u7684\u590d\u6742\u5ea6\u90fd\u662fO(N!)\u3002<\/p>\n<h4>1.5 \u8fb9\u754c\u60c5\u5f62<\/h4>\n<p>\u6709\u4e24\u4e2a\u6bd4\u8f83\u6781\u7aef\u7684\u60c5\u51b5\uff1a<\/p>\n<p>&#8212; \u5f53\u9700\u8981JOIN\u7684\u8868\u7684\u6570\u91cf\u5c0f\u4e8esearch_depth\u65f6\uff0c\u8fd9\u91cc\u5c31\u9000\u5316\u4e3a\u4e00\u4e2a\u6df1\u5ea6\u4f18\u5148\u7684\u7a77\u4e3e\u786e\u5b9a\u6700\u4f18\u6267\u884c\u8ba1\u5212<\/p>\n<p>&#8212; \u5f53search_depth = 1\u7684\u65f6\u5019\uff0c\u51fd\u6570\u9000\u5316\u4e3a&#8221;\u6781\u5176&#8221;\u8d2a\u5a6a\u7684\u7b97\u6cd5\uff0c\u6bcf\u6b21\u4ece\u5f53\u524d\u7684\u5269\u4f59\u7684\u8868\u4e2d\u53d6\u4e00\u4e2a\u6210\u672c\u6700\u5c0f\u7684\uff0c\u6765\u6269\u5c55\u5f53\u524d\u7684\u6267\u884c\u8ba1\u5212<\/p>\n<p>\u5269\u4f59\u7684\u60c5\u51b5\u5c31\u662f\u4ecb\u4e8e\u4e0a\u9762\u4e24\u8005\u4e4b\u95f4\u3002<\/p>\n<h3>2. \u5077\u61d2\u7684MySQL<\/h3>\n<p>\u5728\u6253\u5f00\u4e86\u53c2\u6570prune_level(\u9ed8\u8ba4\u5f00\u542f)\u540e\uff0cMySQL\u4e0d\u518d\u4f7f\u7528\u7a77\u4e3e\u7684\u65b9\u5f0f\u6269\u5c55\u6267\u884c\u8ba1\u5212\uff0c\u800c\u662f\u901a\u8fc7\u4e00\u4e9b\u89c4\u5219\u8df3\u8fc7\u4e00\u4e9b\u770b\u4f3c\u6d88\u8017\u66f4\u5927\u7684\u6267\u884c\u8ba1\u5212\uff0c<del datetime=\"2013-04-03T07:27:13+00:00\">\u800c\u662f\u5728\u5269\u4f59\u8868\u4e2d\u76f4\u63a5\u9009\u53d6\u8bbf\u95ee\u6700\u5c11\u7eaa\u5f55\u6570\u7684\u8868<\/del>\u901a\u8fc7\u8fd9\u79cd&#8221;\u542f\u53d1\u5f0f&#8221;\u7684\u65b9\u5f0f\u5ffd\u7565\u4e00\u4e9b\u6267\u884c\u8ba1\u5212\uff0c\u501f\u6b64\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u9700\u8981\u7a77\u4e3e\u7684\u6267\u884c\u8ba1\u5212\u3002\u6309\u7167MySQL\u624b\u518c\u4e0a\u7684\u63cf\u8ff0\u662f\uff1a\u6839\u636e\u7ecf\u9a8c\u6765\u770b\uff0c\u8fd9\u79cd\u201deducated guess\u201d\u57fa\u672c\u4e0d\u4f1a\u6f0f\u6389\u6700\u4f18\u7684\u6267\u884c\u8ba1\u5212\uff0c\u4f46\u662f\u5374\u53ef\u4ee5\u5927\u5927(dramatically )\u7f29\u5c0f\u641c\u7d22\u7a7a\u95f4\u3002\u8981\u662f\u4f60\u6000\u7591\u6f0f\u6389\u4e86\u67d0\u4e2a\u6700\u4f18\u7684\u6267\u884c\u8ba1\u5212\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u5173\u95ed\u53c2\u6570\u8bd5\u8bd5\uff0c\u5f53\u7136\u8fd9\u4f1a\u5bfc\u81f4\u641c\u7d22\u7a7a\u95f4\u589e\u5927\uff0c\u4f18\u5316\u5668\u6267\u884c\u65f6\u95f4\u504f\u957f\u3002<\/p>\n<p>\u8fd9\u4e2a\u53c2\u6570\u5728\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e2d\u8d77\u4f5c\u7528\uff0c\u5728\u8fdb\u884c\u6df1\u5ea6\u63a2\u7d22\u65f6\uff0c\u6839\u636ecurrent_record_count\u548ccurrent_read_time\uff0c\u6765\u786e\u5b9a\uff0c\u6765\u5224\u65ad\u662f\u5426\u9700\u8981\u5ffd\u7565\u5f53\u524d\u7684\u6267\u884c\u8ba1\u5212\u7a77\u4e3e\u3002(\u539f\u672c\u662f\u9700\u8981\u9012\u5f52\u8c03\u7528\u8ba1\u7b97\u6210\u672c\u786e\u5b9a)\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f2a\u4ee3\u7801\u63cf\u8ff0\uff1a<\/p>\n<pre><blockquote>\u573a\u666f\uff1a\r\npplan \t\t\t\u5f53\u524d\u90e8\u5206\u6267\u884c\u8ba1\u5212(\u521d\u59cb\u4e3a\u7a7a) short for partial plan \r\nN remaining table \t\u5f53\u524d\u5269\u4f59\u8868(\u521d\u59cb\u5316\u65f6\uff0c\u4e3a\u9664\u4e86\u5e38\u6570\u8868\u4e4b\u5916\u7684\u6240\u6709\u8868)\r\n\t\u8fd9N\u8868\u8bb0\u4e3aT[0] T[1] ... T[N-1]\r\n\r\n\u8ba1\u7b97\u4ee3\u7801\uff1a\r\nFunction best_extension(pplan,N)\r\nForeach T in T[0...N-1]\r\n    let P(pplan,T) := add T to pplan\r\n    let current_record_count := #row of P(pplan,T)\r\n    let current_read_time := #read time of P(pplan,T)\r\n    if [\r\n         T is Not The First Table in T[0...N-1] AND\r\n         current_record_count >= best_record_count AND\r\n         current_read_time >= best_read_time \r\n       ]\r\n        \"P(pplan,T) is a bad plan! SKIP it!!!!!!!\"\r\n    END\r\n    let best_record_count := min(best_record_count, current_record_count )\r\n    let best_read_time := min(best_read_time,current_read_time)\r\n    best_extension(P(pplan,T),N-1);\r\nEND<\/blockquote><\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<p>(1) \u4f2a\u4ee3\u7801\u4e2d\u672a\u8003\u8651\u4f9d\u8d56\u5173\u7cfb\u3002\u7b2c\u4e00\u4e2a\u8868\u7684COST\u603b\u662f\u4f1a\u8ba1\u7b97\u51fa\u6765\u3002<\/p>\n<p>(2) \u9762\u5bf9pplan\u548cT[0&#8230;N-1]\u65f6\uff0c\u53ea\u8ba1\u7b97pplan\u4e0eT[0],T[1]&#8230;T[N-1]\u7684\u5173\u8054\u540e\u5404\u81ea\u7684current_record_count\/current_read_time\uff0c\u4f1a\u4f9d\u6b64\u503c\u5ffd\u7565\u4e00\u4e9b\u6267\u884c\u8ba1\u5212\uff0c\u53ea\u6709\u4f18\u4e8e&#8221;\u5f53\u524d\u6700\u4f18&#8221;best_record_count\/best_read_time\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u9012\u63a8\u4e0b\u53bb\uff0c\u5426\u5219\u76f4\u63a5\u5ffd\u7565\u5f53\u524d\u7684\u6267\u884c\u8ba1\u5212\u3002\u6848\u4f8b\u8bf4\u660e\uff1a<\/p>\n<p>(**) \u5f53\u524d\u5269\u4f59\u4e09\u4e2a\u8868A\u3001B\u3001C\uff0cMySQL\u5148\u5c06\u4e09\u4e2a\u8868\u6309\u7167found_records\u8fdb\u884c\u6392\u5e8f\uff0c\u5047\u8bbe\u6392\u5e8f\u540e\u4e3aB\u3001A\u3001C\uff1b<\/p>\n<p>(**) <span style=\"color:red;\">MySQL\u5148\u5c1d\u8bd5\u628aB\u52a0\u5165\u5f53\u524d\u6267\u884c\u8ba1\u5212(pplan+B)<\/span>\uff0c\u5148\u8ba1\u7b97\u8bbf\u95eeB\u7684\u6700\u4f18\u65b9\u5f0f\uff0c\u540c\u65f6\u8ba1\u7b97\u51facurrent_record_count\/current_read_time\uff0c\u5e76\u5c06\u5176\u8bb0\u5f55\u4e3abest_record_count\/best_read_time(\u4ee5\u540e\u518d\u5c1d\u8bd5\u628aA\u3001C\u52a0\u5165\u65f6\uff0c\u5982\u679c\u66f4\u4f18\uff0c\u5219\u66f4\u65b0\u8be5\u503c\uff1b\u5373\u8be5\u503c\u603b\u662f\u4e3a&#8221;\u5f53\u524d\u6700\u4f18&#8221;)\uff1b<\/p>\n<p>(**) pplan+B\u7ee7\u7eed\u5411\u540e\u6269\u5c55\uff1b<\/p>\n<p>(**) <span style=\"color:red;\">MySQL\u518d\u5c1d\u8bd5\u628aA\u52a0\u5165\u5f53\u524d\u6267\u884c\u8ba1\u5212(pplan+A)<\/span>\uff0c\u5148\u8ba1\u7b97\u8bbf\u95eeA\u7684\u6700\u4f18\u65b9\u5f0f\uff0c\u5982\u679c\u5176current_record_count\/current_read_time\u5c0f\u4e8e\u5f53\u524d\u6700\u4f18\uff0c<span style=\"color:green;\">\u5219\u5ffd\u7565\u5f53\u524d\u6267\u884c\u8ba1\u5212(\u5982\u679cprune_level=1)<\/span>\uff0c\u5426\u5219\uff0cpplan+A\u7ee7\u7eed\u5411\u540e\u6269\u5c55<\/p>\n<p>(**) <span style=\"color:red;\">MySQL\u518d\u5c1d\u8bd5\u628aC\u52a0\u5165\u5f53\u524d\u6267\u884c\u8ba1\u5212(pplan+C)<\/span>&#8230; <\/p>\n<p>(**) \u7a77\u4e3e\u5b8c\u6210<\/p>\n<p>(3) \u8fd9\u770b\u8d77\u6765\u8fd9\u662f\u4e00\u4e2a&#8221;\u8f83\u4e3a\u6fc0\u8fdb&#8221;\u7684\u4f18\u5316\u65b9\u5f0f\u3002<\/p>\n<h3>3. \u5f00\u59cb\u524d\u7684\u6392\u5e8f<\/h3>\n<pre><blockquote> 4753   my_qsort(join->best_ref + join->const_tables,\r\n 4754            join->tables - join->const_tables, sizeof(JOIN_TAB*),\r\n 4755            straight_join ? join_tab_cmp_straight : join_tab_cmp);<\/blockquote><\/pre>\n<p>MySQL\u5728\u5f00\u59cb\u786e\u5b9aJOIN\u987a\u5e8f\u4e4b\u524d\u4f1a\u6839\u636e\u6bcf\u4e2a\u8868\u53ef\u80fd\u8bbf\u95ee\u7684\u7eaa\u5f55\u6570\uff0c\u8fdb\u884c\u4e00\u6b21\u6392\u5e8f\u3002\u8fd9\u4e00\u6b65\u770b\u4f3c\u591a\u4f59\uff0c\u4f46\u662f\u5f53\u7a77\u4e3e\u641c\u7d22\u65f6\uff0c\u53ef\u4ee5\u5927\u5927\u7684\u51cf\u5c11\u6267\u884c\u8ba1\u5212\u9700\u8981\u63a2\u6d4b\u7684\u6df1\u5ea6\u3002<\/p>\n<p>\u5f53\u8bc4\u4f30\u67d0\u4e2a\u6267\u884c\u8ba1\u5212\u7684\u65f6\u5019\uff0c\u5982\u679c\u67d0\u4e00\u6b65\u53d1\u73b0\u5f53\u524d\u7684cost\u5df2\u7ecf\u5927\u4e8e\u6700\u4f18\u7684\u6267\u884c\u8ba1\u5212\u65f6\uff0c\u5219\u7acb\u5373\u9000\u51fa\u8bc4\u4f30\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u6700\u5148\u627e\u5230\u6700\u4f18\u7684\u6267\u884c\u8ba1\u5212\uff0c\u90a3\u4e48\u9700\u8981\u505a\u7684\u8bc4\u4f30\u5c06\u4f1a\u5c11\u5f88\u591a\u3002\u5982\u679c\u67d0\u4e2a\u8868\u9700\u8981\u626b\u63cf\u7684\u884c\u6570\u8d8a\u5c11\uff0c\u90a3\u4e48\u53ef\u4ee5\u521d\u6b65\u8ba4\u4e3a\u8d8a\u5148\u4f7f\u7528\u8d8a\u597d\u3002\u5f53\u7136\uff0c\u56e0\u4e3a\u8fd9\u91cc\u7684\u6392\u5e8f\u8bc4\u4f30\u662f\u6ca1\u6709\u4f7f\u7528JOIN\u6761\u4ef6\u7684\uff0c\u6240\u4ee5\uff0c\u770b\u8d77\u6765\u9700\u8981\u626b\u63cf\u5f88\u591a\u7684\uff0c\u4e5f\u53ef\u80fd\u52a0\u4e0aJOIN\u4ee5\u540e\u53ea\u9700\u8981\u626b\u63cf\u5f88\u5c11\u7684\u8bb0\u5f55\u3002<\/p>\n<h3>4. \u51fd\u6570\u8c03\u7528\u6808<\/h3>\n<pre><blockquote>#0            best_access_path \r\n#1          best_extension_by_limited_search \r\n#2        greedy_search\r\n#3      choose_plan \r\n#4    make_join_statistics\r\n#5  JOIN::optimize<\/blockquote><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u770bMySQL\u4f18\u5316\u5668\u4ee3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u5e94\u8be5\u662f\u76f8\u5bf9\u8f83\u7b80\u5355\/\u4ee3\u7801\u8f83\u6e05\u6670\u7684\u90e8\u5206\u4e86\u3002MySQL\u4f18\u5316\u5668\u6709\u4e24\u4e2a\u81ea\u7531\u5ea6\uff1a\u5355\u8868\u8bbf\u95ee\u65b9\u5f0f\uff0c\u591a\u8868\u987a\u5e8f\u9009\u62e9\u3002\u524d\u6587\u5df2\u7ecf\u4ecb\u7ecd\u8fc7MySQL\u5355\u8868\u8bbf\u95ee\u7684\u4e00\u4e9b\u8003\u91cf(ref\/range\u7b49)\uff0c\u672c\u6587\u5c06\u4ecb\u7ecdJOIN\u5728\u987a\u5e8f\u9009\u62e9\u4e0a\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002 \u5f53\u6709\u591a\u4e2a\u8868\u9700\u8981JOIN\u7684\u65f6\u5019\uff0cMySQL\u9996\u5148\u4f1a\u5904\u7406\u4e24\u7c7b\u7279\u6b8a\u60c5\u51b5\uff0c\u4e00\u4e2a\u662f\u5e38\u6570\u8868\uff0c\u4e00\u4e2a\u662f\u7531\u4e8e\u5916\u8fde\u63a5\u5bfc\u81f4\u987a\u5e8f\u4f9d\u8d56\u5173\u7cfb\u3002\u524d\u8005\u603b\u662f\u653e\u5728\u5173\u8054\u7684\u6700\u524d\u9762\uff0c\u540e\u8005\u4f1a\u5728\u904d\u5386\u7684\u65f6\u5019\u8003\u8651\u3002\u672c\u6587\u5c06\u5ffd\u7565\u4e0a\u9762\u4e24\u70b9\uff0c\u4ece\u8f83\u5b8f\u89c2\u89d2\u5ea6\u770bJOIN\u987a\u5e8f\u9009\u62e9\u65f6\u5019\u7684\u590d\u6742\u5ea6\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6],"tags":[101,118,102,58],"class_list":["post-3935","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-index","tag-mysql","tag-optimizer","tag-source-code"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/3935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=3935"}],"version-history":[{"count":22,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/3935\/revisions"}],"predecessor-version":[{"id":5133,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/3935\/revisions\/5133"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=3935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=3935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=3935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}