{"id":4322,"date":"2013-03-13T14:05:02","date_gmt":"2013-03-13T06:05:02","guid":{"rendered":"http:\/\/www.orczhou.com\/?p=4322"},"modified":"2013-03-13T14:08:36","modified_gmt":"2013-03-13T06:08:36","slug":"index-merge-intersection-ror","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2013\/03\/index-merge-intersection-ror\/","title":{"rendered":"index merge\u7684\u8865\u5145\u8bf4\u660e"},"content":{"rendered":"<p>\u5728\u9664\u4e86\u524d\u9762\u4ecb\u7ecd\u7684\u5e38\u89c1index merge\u7684\u6848\u4f8b(Index Merge Union Access Algorithm)\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u7c7b\u5f88\u5c11\u89c1\u4e5f\u6bd4\u8f83\u7279\u6b8a\u7684index merge\uff0c\u591a\u4e2a\u7d22\u5f15\u626b\u63cf\u540e\u8fdb\u884c\u4ea4\u96c6\uff0c\u5373 <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/index-merge-intersection.html\" target=\"_blank\">Index Merge Intersection<\/a>\u3002\u8fd9\u7c7b\u6267\u884c\u8ba1\u5212\u6bd4\u8f83\u5c11\u89c1(\u56e0\u4e3aMySQL\u9700\u8981ROR\u7684\u539f\u56e0)\uff0c\u4f46\u662f\uff0c\u5728\u5408\u9002\u7684\u573a\u666f\u4f7f\u7528\uff0c\u6548\u7387\u4ecd\u7136\u4f1a\u6709\u5f88\u5927\u7684\u63d0\u793a\uff0c\u672c\u6587\u5c06\u770b\u770bMySQL\u4f18\u5316\u5668\u5982\u4f55\u8bc4\u4f30\u548c\u9009\u62e9\u6b64\u7c7b\u6267\u884c\u8ba1\u5212\u3002<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/index-merge-intersection.html\" target=\"_blank\">MySQL\u624b\u518c\u5bf9\u6b64<\/a>\u53ea\u662f\u4e09\u8a00\u4e24\u8bed\u7b80\u5355\u4ecb\u7ecd\u4e86\u4e00\u4e0b\uff0c\u8fd9\u91cc\u505a\u4e2a\u8f83\u4e3a\u8be6\u7ec6\u7684\u8bf4\u660e\u3002<\/p>\n<p>\u8fd9\u7c7b\u6267\u884c\u8ba1\u5212\u5b8c\u6574\u540d\u79f0\u5e94\u8be5\u662f\uff1a<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/index-merge-intersection.html\" target=\"_blank\">The Index Merge Intersection Access Algorithm<\/a>\uff0c\u4e0b\u6587\u7b80\u79f0<strong>Intersection<\/strong>\u3002<\/p>\n\n<h3>1. \u4e3a\u4ec0\u4e48\u9700\u8981\u8003\u8651Intersection<\/h3>\n<p>\u8003\u8651\u5982\u4e0b\u67e5\u8be2:<\/p>\n<pre><blockquote>SELECT COUNT(*) FROM t1 WHERE key1=1 AND key2=1;<\/blockquote><\/pre>\n<p>\u4f18\u5316\u5668\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7d22\u5f15key1<strong>\u6216\u8005<\/strong>key2\u8fdb\u884cREF\/Range\u8bbf\u95ee\uff0c\u5982\u679c\u4f7f\u7528key1\uff0c\u90a3\u4e48key2=1\u5219\u4f5c\u4e3a\u8fc7\u6ee4\u6761\u4ef6\u3002\u53e6\u5916\uff0c\u4f18\u5316\u5668\u8fd8\u4f1a\u8003\u8651\u4f7f\u7528<strong>Intersection<\/strong>\uff0c\u5373\u540c\u65f6\u4f7f\u7528\u7d22\u5f15key1\u548ckey2\u3002\u8fd9\u6837\u505a<strong>\u53ef\u80fd\u7684\u597d\u5904<\/strong>\u662f\uff1a<\/p>\n<p>(a) \u5982\u679c\u4e24\u6b21\u7d22\u5f15\u626b\u63cf\u540e\u505a\u4ea4\u96c6\uff0c\u5982\u679c\u6700\u540eROWID\u5f88\u5c11\uff0c\u5219\u56de\u8868\u6b21\u6570\u5927\u5927\u51cf\u5c11<\/p>\n<p>(b) \u5982\u679c\u626b\u63cf\u8fd9\u4e24\u4e2a\u7d22\u5f15\u80fd\u662f\u8986\u76d6\u626b\u63cf\u7684\u8bdd\uff0c\u5219\u65e0\u9700\u56de\u8868<!--more--><\/p>\n<p>\u5bf9\u6bd4ref\/range\u8bbf\u95ee\u65b9\u5f0f\uff0cindex merge\u9700\u8981\u989d\u5916\u591a\u8bbf\u95ee\u4e00\u4e2a\u7d22\u5f15\uff0cROWID\u9700\u8981\u505a\u4ea4\u96c6\uff0c\u6240\u4ee5\u9700\u8981\u989d\u5916\u7684\u6bd4\u8f83\u64cd\u4f5c\u3002\u4f18\u5316\u5668\u5c06\u5404\u81ea\u8ba1\u7b97ref\/range\u548cindex merge\u7684\u6210\u672c\uff0c\u7136\u540e\u9009\u62e9\u6210\u672c\u8f83\u4f4e\u4f5c\u4e3a\u6700\u7ec8\u7684\u6267\u884c\u8ba1\u5212\u3002<\/p>\n<h3>2. MySQL\u4f18\u5316\u5668\u7684Intersection<\/h3>\n<p>\u524d\u9762\u63cf\u8ff0\u4e86<strong>Intersection<\/strong>\u7684\u4e24\u4e2a\u597d\u5904\uff0cMySQL\u4f18\u5316\u5668\u5148\u4f7f\u7528\u4e86\u4e00\u4e2a\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\u6765\u9884\u4f30\u5408\u5e76\u540eROWID\u6570\u91cf\uff1b\u53e6\u5916\uff0c\u5982\u679c\u53d1\u73b0\u6709\u8986\u76d6\u626b\u63cf\uff0c\u5219\u65e0\u9700\u56de\u8868\uff0c\u5219\u6210\u672c\u4f1a\u5927\u5927\u51cf\u5c11\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u56e0\u4e3aindex merge\u901a\u5e38\u9700\u8981\u8bbf\u95ee\u4e24\u4e2a\u4ee5\u4e0a\u7d22\u5f15\uff0c\u6210\u672c\u901a\u5e38\u4e0d\u62b5\uff0cMySQL\u9009\u62e9<strong>Intersection<\/strong>\u7684\u65f6\u5019\uff0c\u52a0\u4e0a\u4e86\u4e00\u4e2a\u989d\u5916\u7684\u8981\u6c42\uff1a<\/p>\n<p>(a) \u53ea\u6709ROR\u7c7b\u578b\u7684\u7d22\u5f15\u4f7f\u7528\u624d\u80fd\u4f5c\u4e3a<strong>Intersection<\/strong>\u6267\u884c\u8ba1\u5212\u7684\u4e00\u90e8\u5206(<a href=\"http:\/\/www.orczhou.com\/index.php\/2013\/03\/mysql-source-code-query-optimization-index-merge-structure-cost\/#41_Rowid-Ordered_Retrieval\" target=\"_blank\">\u4ec0\u4e48\u662fROR<\/a>)<\/p>\n<h3>3. \u4f18\u5316\u5668\u5982\u4f55\u7b5b\u9009<strong>Intersection<\/strong>\u4f7f\u7528\u7684\u7d22\u5f15<\/h3>\n<h4>3.1 \u7b97\u6cd5\u8bf4\u660e<\/h4>\n<p>\u8fd9\u91cc\u5206\u4e86\u4e24\u4e2a\u90e8\u5206\uff0c\u5148\u4f7f\u7528\u8d2a\u5a6a\u7b97\u6cd5\u5728\u6240\u6709\u7684ROR\u7d22\u5f15\u4e2d\uff0c\u7ec4\u5408\u51fa\u4e00\u7ec4\u6210\u672c\u6700\u5c0f\u7684\u505a<strong>Intersection<\/strong>\u3002\u5982\u679c\u8fd9\u4e2a\u201c\u6700\u5c0f\u7ec4\u5408\u201d\u4e0d\u662f\u8986\u76d6\u7d22\u5f15\uff0c\u800c\u4e14\u53c8\u5b58\u5728\u8986\u76d6\u7d22\u5f15\uff0c\u90a3\u4e48\u518d\u505a\u4e00\u6b21\u8d2a\u5a6a\u7b97\u6cd5\u627e\u5230\u4e00\u4e2a\u6210\u672c\u6700\u5c0f\u7684\u8986\u76d6\u67e5\u8be2\uff0c\u5982\u679c\u6210\u672c\u66f4\u5c0f\u5219\u9009\u62e9\u4e4b\u3002<\/p>\n<h5>3.1.1 \u627e\u5230\u6210\u672c\u6700\u5c0f\u7684ROR\u7ec4\u5408<\/h5>\n<p>\u8fd9\u662f\u4e00\u4e2a\u8d2a\u5a6a\u7b97\u6cd5\uff0c\u627e\u5230\u672a\u5fc5\u662f\u5168\u5c40\u6700\u4f18\u7684\u7ed3\u679c\u3002\u8fd9\u91cc\u7b80\u5355\u63cf\u8ff0\u4e00\u4e0b\u7b97\u6cd5(\u53ef\u4ee5\u53c2\u8003get_best_ror_intersect\u7684\u6ce8\u91ca\u548c\u5b9e\u73b0)\uff1a<\/p>\n<pre><blockquote>\u521d\u59cb\uff1aR\u662f\u6240\u6709\u53ef\u7528\u7684ROR\u7d22\u5f15\u67e5\u8be2\uff1bS\u662f\u7a7a\u96c6\uff1b\r\nR\u4e2d\u7684\u8bb0\u5f55\u662f\u6309\u7167\u9700\u8981\u626b\u63cf\u7d22\u5f15\u7684\u5927\u5c0f\u6392\u5e8f(E(#records_matched) * key_record_length)\r\n  S= first(R); R= R-first(S);\r\n  min_cost= cost(S); min_scan= make_scan(S);\r\n  while (R is not empty)\r\n  {\r\n    firstR= R - first(R);\r\n    if (!selectivity(S + firstR < selectivity(S)))\r\n      continue;\r\n    S= S + first(R);\r\n    if (cost(S) < min_cost)\r\n    {min_cost= cost(S);min_scan= make_scan(S);}\r\n  }\r\n  return min_scan;\r\n<\/blockquote><\/pre>\n<p>\u7b97\u6cd5\u8bf4\u660e\uff1a\u6bcf\u6b21\u4ece\u6240\u6709ROR\u4e2d\u53d6\u51fa\u626b\u63cf\u6210\u672c\u6700\u4f4e\u7684\u7d22\u5f15\uff0c\u5224\u65ad\u52a0\u5165\u8be5\u7d22\u5f15\u540e\u6210\u672c\u662f\u5426\u4f1a\u4e0b\u964d\u3002\u5982\u679c\u6210\u672c\u4e0b\u964d\uff0c\u5219\u5c06\u672cROR\u52a0\u5165\u7ed3\u679c\u96c6\uff1b\u5982\u679c\u6210\u672c\u4e0d\u4f1a\u4e0b\u964d\uff0c\u90a3\u4e48\u5ffd\u7565\uff1b<\/p>\n<p>\u9664\u6b64\uff0cMySQL\u8fd8\u505a\u4e86\u4e00\u4e2a\u5224\u65ad\uff0c\u5982\u679c\u65b0\u589eROR\u7d22\u5f15\u4e4b\u540e\uff0c\u4f1a\u8ba1\u7b97\u5176\u9009\u62e9\u5ea6(selectivity)\uff0c\u53ea\u6709\u5f53\u65b0\u589eROR\u7d22\u5f15\u4f1a\u964d\u4f4e\u6574\u4f53\u533a\u5206\u5ea6\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u7d22\u5f15\u624d\u4f1a\u88ab\u52a0\u5165\u5176\u4e2d\u3002\u8fd9\u90e8\u5206\u8ba1\u7b97\u7684\u76ee\u7684\uff0c\u4e00\u65b9\u9762\u662f\u4fdd\u8bc1\u65b0\u589e\u7d22\u5f15\u540e\u4e00\u5b9a\u4f1a\u964d\u4f4e\u9009\u62e9\u5ea6\uff0c\u8fd9\u901a\u5e38\u90fd\u662f\u6ee1\u8db3\u7684\uff0c\u53ea\u8981\u65b0\u589e\u7684\u7d22\u5f15\u6761\u4ef6\u4e0d\u662fS\u96c6\u5408\u7684\u5b50\u96c6\uff0c\u4e00\u822c\u90fd\u662f\u6ee1\u8db3\u7684\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u4f1a\u987a\u4fbf\u8ba1\u7b97\u51fa\u65b0\u589e\u7d22\u5f15\u540e\u7684\u9009\u62e9\u5ea6\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8ba1\u7b97\uff0c\u591a\u4e2a\u7d22\u5f15\u5408\u5e76\u540e\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u5927\u7ea6\u662f\u591a\u5c11\u3002\u4e0b\u9762\u4f1a\u5355\u72ec\u4ecb\u7ecdMySQL\u5982\u4f55\u9884\u4f30\uff0c\u4e24\u4e2a\u6761\u4ef6\u4ea4\u96c6\u547d\u4e2d\u7684\u8bb0\u5f55\u6570\u3002<\/p>\n<h5>3.1.2 \u8ba1\u7b97\u4e24\u4e2a\u7d22\u5f15\u4ea4\u96c6\u547d\u4e2d\u7684\u8bb0\u5f55\u6570<\/h5>\n<p>\u8fd9\u4e2a\u95ee\u9898\u7684\u62bd\u8c61\u5982\u4e0b\uff1a\u6709\u5982\u4e0b\u6761\u4ef6key1_p1=c1 and key1_p1=c2 and key2_p1=c3 and key2_p2=c4\uff0c\u73b0\u5728\u5df2\u77e5key1_p1=c1 and key1_p1=c2\u7684\u9009\u62e9\u5ea6\u662fX\uff0ckey2_p1=c3 and key2_p2=c4\u7684\u9009\u62e9\u5ea6\u662fY\uff0c\u95ee\uff0c\u603b\u4f53\u9009\u62e9\u5ea6\u662f\u591a\u5c11\uff1f<\/p>\n<p>\u5982\u679ckey1\u548ckey2\u662f\u5b8c\u5168\u72ec\u7acb\u7684\uff0c\u6ca1\u6709\u4efb\u4f55\u5b57\u6bb5\u91cd\u590d\uff0c\u90a3\u4e48\u6309\u7167\u5747\u5300\u8ba1\u7b97\uff0c\u4ea4\u96c6\u540e\uff0c\u603b\u4f53\u9009\u62e9\u5ea6\u4e3aX*Y\uff0c\u8fd9\u90e8\u5206\u662f\u8f83\u4e3a\u5bb9\u6613\u7406\u89e3\u7684\u3002<\/p>\n<p>\u5982\u679ckey1\u548ckey2\u4e0d\u662f\u72ec\u7acb\u7684\uff0c\u95ee\u9898\u5c31\u8f83\u4e3a\u590d\u6742\u4e86\uff0c\u4f8b\u5982\uff0ckey1_p1 = c1 \u548c key2_p1=c3 \u662f\u4e24\u4e2a\u4e00\u6837\u7684\u91cd\u590d\u7684\u6761\u4ef6\uff0c\u5373\u7d22\u5f15key1\u548ckey2\u7684\u67d0\u4e2a\u5b57\u6bb5\u76f8\u540c\u3002\u90a3\u4e48\uff0c\u5982\u679c\u6309\u7167\u4e0a\u9762\u7684\u516c\u5f0f\u8ba1\u7b97\u5c31\u975e\u5e38\u4e0d\u51c6\u786e\u4e86\u3002MySQL\u8ba1\u7b97\u7684\u529e\u6cd5\uff0c\u662f\u9010\u4e2a\u6dfb\u52a0\uff1a<\/p>\n\n<p>\u5047\u8bbe\u6709\u96c6\u5408A={key1_p1 = c1, key1_p1=c2}\uff0c\u5bf9\u5e94\u7684\u9009\u62e9\u5ea6\u8bb0\u4e3aP(A)\uff0c\u5982\u679c\u6709\u7d22\u5f15\u6761\u4ef6\uff1akey2_p1=c3 and key2_p2=c4\uff0cMySQL\u5148\u5c06key2_p1=c3\u52a0\u5165\u96c6\u5408A\uff0c\u5e76\u8ba1\u7b97\u9009\u62e9\u5ea6\uff1b\u7136\u540e\u628akey2_p2=c4\u52a0\u5165\u96c6\u5408A\uff0c\u5e76\u8ba1\u7b97\u9009\u62e9\u5ea6\u3002\u8fdb\u4e00\u6b65\u62bd\u8c61\uff0c\u6709\u96c6\u5408A\uff0c\u5df2\u77e5\u9009\u62e9\u5ea6\u4e3aP(A)\uff0c\u73b0\u6709\u7d22\u5f15\u6761\u4ef6key2\u5bf9\u5e94\u7684\u4e24\u4e2aAND\u6761\u4ef6\u4e3a\\(b_1\\)\u548c\\(b_2\\)\uff0c\u73b0\u5728\u6f14\u793a\u5982\u4f55\u9010\u4e2a\u5c06\\(b_1\\)\u548c\\(b_2\\)\u52a0\u5165\u96c6\u5408A\u5e76\u8ba1\u7b97\u5176\u9009\u62e9\u5ea6\u3002<\/p>\n<p>\u5df2\u77e5\u96c6\u5408A\uff0c\u5176\u9009\u62e9\u5ea6\u4e3aP(A)\uff1b\u7d22\u5f15\u6761\u4ef6\\(b_1\\) and \\(b_2\\)\uff1b\u5e76\u8bb0 \\(B_1 = \\{b_1\\}\uff0cB_2 = \\{b_2\\}\uff1b\\)\uff1b<\/p>\n<p>\u8bb0R\u4e3a\u8be5\u8868\u603b\u8bb0\u5f55\u6570\uff0c\\(R(b_1)\\)\u8868\u793a\u6761\u4ef6\\(b_1\\)\u5bf9\u5e94\u7684\u8bb0\u5f55\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570records_in_range\u8ba1\u7b97\uff1b<\/p>\n<p>\\(P(X|Y)\\)\u8868\u793aY\u6761\u4ef6\u53d1\u751f\u65f6\u7684\u6761\u4ef6\u6982\u7387\uff0c\u8fd9\u91cc\u5047\u8bbe\u90fd\u662f\u5747\u5300\u5206\u5e03\uff0c\u9009\u62e9\u5ea6\u5c31\u662f\u6982\u7387\u3002\u4e14\u6709P(X|Y) = P(X)*P(Y|X)\uff1b<\/p>\n<p>\u90a3\u4e48\uff0c\u5c06\u96c6\u5408\\(B_1\\)\u5408\u5e76\u5230\u96c6\u5408A\u4e4b\u540e\uff0c\u9009\u62e9\u5ea6\u8ba1\u7b97\u4e3a\uff1a<\/p>\n<p>\\[P(A\\cap B_1) = P(A)*P(B_1|A) \\] <\/p>\n<p>(1) \u5982\u679cA\uff0c\\(B_1\\)\u4e0d\u72ec\u7acb\uff0c\u5373\u5bf9\u5e94\u6761\u4ef6\\(b_1\\)\u5c5e\u4e8e\u96c6\u5408A\uff0c\u90a3\u4e48\uff0c\\(P(B_1|A) = 1\\)\u3002\u90a3\u4e48\u9009\u62e9\u5ea6\u4e0d\u53d8\uff0c\u4ecd\u7136\u662f\\(P(A)\\);<\/p>\n<p>(2) \u5982\u679cA\uff0c\\(B_1\\)\u72ec\u7acb\uff0c\u5bf9\u5e94\u6761\u4ef6\\(b_1\\)<strong>\u4e0d<\/strong>\u5c5e\u4e8e\u96c6\u5408A\uff0c\u90a3\u4e48\u6709<\/p>\n<p>\\[P(A\\cap B_1) = P(A)*P(B_1) \\] <\/p>\n<p>\\[P(B_1) = \\frac{R(b_1)}{R}\\]<\/p>\n<p>\\[P(A\\cap B_1) = P(A)*\\frac{R(b_1)}{R} \\] <\/p>\n<p>\u8fd9\u65f6\u5c31\u53ef\u4ee5\u628a\u6761\u4ef6\\(b_1\\)\u5e76\u5165\u96c6\u5408A\uff0c\u5bf9\u5e94\u7684\u9009\u62e9\u5ea6\u5982\u4e0a\u5f0f\u3002\u7ee7\u7eed\uff0c\u8003\u8651\u628a\u6761\u4ef6\\(b_2\\)\u52a0\u5165\u5408\u8ba1A\u3002<\/p>\n<p>\\[P((A \\cap B_1) \\cap B_2) =  P(A)*\\frac{R(b_1)}{R}*P(B_2|A \\cap B_1)   \\]<\/p>\n<p>\u540c\u6837\u7684\uff0c\u5982\u679c\\(B_2\\)\u548c\\(A \\cap B_1\\)\u4e0d\u72ec\u7acb\uff0c\u5373\\(B_2\\)\u662f\\(\\{x|x \\in A \u6216\u8005 x \\in B_1 \\}\\)\u7684\u5b50\u96c6\uff0c\u90a3\u4e48<\/p>\n<p>\\[P(B_2|A \\cap B_1) = 1\\]<\/p>\n<p>\\[P((A \\cap B_1) \\cap B_2) =  P(A)*\\frac{R(b_1)}{R} \\]<\/p>\n<p>\u5982\u679c\u4e24\u8005\u72ec\u7acb\uff0c\u7ee7\u7eed\u8ba1\u7b97\uff1a<\/p>\n<p>\\[P(B_2|A \\cap B_1) = P(B_2) = \\frac{R(b_1 and b_2)}{R(b_1)} \\]<\/p>\n<p>\\[P((A \\cap B_1) \\cap B_2) = P(A)*\\frac{R(b_1)}{R} * \\frac{R(b_1 and b_2)}{R(b_1)} = P(A)*\\frac{R(b_1 and b_2)}{R} \\]<\/p>\n<p>MySQL\u5c06\u4f7f\u7528\u4e0a\u9762\u7684\u65b9\u6cd5\u8ba1\u7b97\u591a\u4e2a\u6761\u4ef6\u5408\u5e76\u7684\u65f6\u5019\u7684\u9009\u62e9\u5ea6\u3002 MySQL\u901a\u8fc7records_in_range\u6765\u8ba1\u7b97\\(R(b_1 and b_2)\\)\u3002<\/p>\n<p>MySQL\u5728\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u7565\u6709\u4e0d\u540c\u7684\u5730\u65b9\u662f\uff0c\u4e3a\u4e86\u5c3d\u53ef\u80fd\u5c11\u7684\u907f\u514drecords_in_range\u7684\u8c03\u7528\u6b21\u6570\uff0c\u5982\u679c\u8fde\u7eed\u7684\u591a\u4e2a\u6761\u4ef6\u90fd\u662f\u540c\u65f6\u72ec\u7acb\u6216\u8005\u540c\u65f6\u90fd\u4e0d\u72ec\u7acb\uff0c\u90a3\u4e48\u5219\u4f1a\u5c06\u8fd9\u591a\u4e2a\u6761\u4ef6\u4f5c\u4e3a\u4e00\u4e2a\u6574\u7406\u6765\u8ba1\u7b97\u3002<\/p>\n<h5>3.1.3 \u627e\u5230\u6210\u672c\u6700\u5c0f\u8986\u76d6\u7d22\u5f15\u7ec4\u5408<\/h5>\n<p>\u5982\u679c\u524d\u9762\u627e\u5230ROR\u7ec4\u5408\u4e0d\u662f\u8986\u76d6\u67e5\u8be2\uff0c\u800c\u4e14\u53c8\u5b58\u591a\u4e2a\u7d22\u5f15\u7ec4\u5408\u7684\u8986\u76d6\u7d22\u5f15\u7684\u8bdd\uff0cMySQL\u8fd8\u4f1a\u518d\u505a\u4e00\u6b21\u8d2a\u5a6a\u67e5\u627e\uff0c\u5c1d\u8bd5\u627e\u5230\u6700\u4f18\u7684\u8986\u76d6\u7d22\u5f15\u7ec4\u5408\uff0c\u5982\u679c\u6210\u672c\u6bd4\u4e4b\u524d\u7684&#8221;\u6700\u5c0f\u6210\u672c&#8221;\u66f4\u5c0f\uff0c\u5219\u9009\u62e9\u8fd9\u7ec4\u7d22\u5f15\u3002<\/p>\n<p>\u8fd9\u90e8\u5206\u5b9e\u73b0\u53c2\u8003\u51fd\u6570get_best_covering_ror_intersect\uff0c\u6ca1\u6709\u7279\u522b\u9700\u8981\u8bf4\u660e\u7684\u3002<\/p>\n<h3>4. \u6210\u672c\u7684\u8ba1\u7b97<\/h3>\n<p>\u5982\u679c\u4e0a\u9762\u8ba1\u7b97\u597d\u4e86\u9009\u62e9\u5ea6\uff0c<strong>Intersection<\/strong>\u7684\u6210\u672c\u8ba1\u7b97\u5c31\u5f88\u7b80\u5355\u4e86\u3002\u6bcf\u6b21\u65b0\u589e\u4e00\u4e2a\u7d22\u5f15\u5230index merge\u4e2d\u7684\u65f6\u5019\uff0c\u5148\u8ba1\u7b97\u5404\u4e2a\u7d22\u5f15\u8bfb\u53d6\u7684\u6210\u672c(<a href=\"http:\/\/www.orczhou.com\/index.php\/2013\/03\/mysql-source-code-query-optimization-index-merge-structure-cost\/#451\" target=\"_blank\">\u53c2\u8003<\/a>)\uff0c\u5982\u679c\u4e0d\u662f\u8986\u76d6\u626b\u63cf\u5219\u9700\u8981\u989d\u5916\u52a0\u4e0a\uff0c\u6839\u636eROWID\u53d6\u51fa\u8bb0\u5f55\u7684\u6210\u672c(<a href=\"http:\/\/www.orczhou.com\/index.php\/2013\/03\/mysql-source-code-query-optimization-index-merge-structure-cost\/#447_ROWID\" target=\"_blank\">\u53c2\u8003<\/a>)\u3002<\/p>\n<h3>5. Intersection\u7684\u6848\u4f8b<\/h3>\n<pre><blockquote>CREATE TABLE `tmp_index_merge` (\r\n  `id` int(11) NOT NULL,\r\n  `key1_part1` int(11) NOT NULL,\r\n  `key1_part2` int(11) NOT NULL,\r\n  `key2_part1` int(11) NOT NULL,\r\n  `key2_part2` int(11) NOT NULL,\r\n  `key2_part3` int(11) NOT NULL,\r\n  `key3_part1` int(11) NOT NULL DEFAULT '4',\r\n  PRIMARY KEY (`id`),\r\n  KEY `ind2` (`key2_part1`,`key2_part2`,`key2_part3`),\r\n  KEY `ind1` (`key1_part1`,`key1_part2`,`id`),\r\n  KEY `ind3` (`key3_part1`,`id`)\r\n) ENGINE=InnoDB\r\n\r\nfor i in `seq 1 5000` ; do mysql -vvv -uroot test \\\r\n-e 'insert into tmp_index_merge values (60000*rand(),5000*rand(),\\\r\n5000*rand(),5000*rand(),5000*rand(),5000*rand(),2877)'; done\r\n\r\nfor i in `seq 1 5000` ; do mysql -vvv -uroot test \\\r\n-e 'insert into tmp_index_merge values (600000*rand(),4333,1657,\\\r\n5000*rand(),5000*rand(),5000*rand(),5000*rand())'; done\r\n\r\nexplain select count(*) from tmp_index_merge where \r\n(key1_part1 = 4333 and key1_part2 = 1657) and (key3_part1 = 2877)\\G\r\n*************************** 1. row ***************************\r\n           id: 1\r\n  select_type: SIMPLE\r\n        table: tmp_index_merge\r\n         type: index_merge\r\npossible_keys: ind1,ind3\r\n          key: ind3,ind1\r\n      key_len: 4,8\r\n          ref: NULL\r\n         rows: 3622\r\n        Extra: Using intersect(ind3,ind1); Using where; Using index<\/blockquote><\/pre>\n<p>\u5982\u679c\u4e0d\u6ee1\u8db3ROR\u7684\u6761\u4ef6\uff0c\u4f8b\u5982\u5c06\u4e0a\u9762\u6848\u4f8b\u7684ind3\u7d22\u5f15\u7684ID\u5b57\u6bb5\u53bb\u6389\uff0c\u5219\u4e0d\u4f1a\u518d\u8003\u8651\u4f7f\u7528<strong>Intersection<\/strong>\u3002<\/p>\n<pre><blockquote>alter table tmp_index_merge drop index ind3,add KEY `ind3` (`key3_part1`);\r\nQuery OK, 14137 rows affected (1.15 sec)\r\nRecords: 14137  Duplicates: 0  Warnings: 0\r\n\r\nroot@test 04:32:58>explain select * from tmp_index_merge where \r\n(key1_part1 = 4333 and key1_part2 = 1657) and (key3_part1 = 2877)\\G\r\n*************************** 1. row ***************************\r\n           id: 1\r\n  select_type: SIMPLE\r\n        table: tmp_index_merge\r\n         type: ref\r\npossible_keys: ind1,ind3\r\n          key: ind1\r\n      key_len: 8\r\n          ref: const,const\r\n         rows: 3408\r\n        Extra: Using where<\/blockquote><\/pre>\n<h3>6. \u6700\u540e<\/h3>\n<p><strong>Intersection<\/strong>\u8fd9\u7c7b\u6267\u884c\u8ba1\u5212\uff0c\u56e0\u4e3a\u9700\u8981\u6ee1\u8db3ROR\u6761\u4ef6\uff0c\u6240\u4ee5\u8f83\u4e3a\u5c11\u89c1\u3002\u7406\u60f3\u60c5\u51b5\u662f\uff0c\u8986\u76d6\u4f46\u975eROR\u6210\u672c\u4e5f\u53ef\u80fd\u4f1a\u5f88\u4f4e\uff0c\u4f46\u662fMySQL\u4e0d\u8003\u8651\u8fd9\u70b9\u3002\u53e6\u5916\uff0c\u8f83\u65b0\u7248\u672c\u5f00\u59cb\u652f\u6301<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/index-condition-pushdown-optimization.html\" target=\"_blank\">Index Condition Pushdown<\/a>\uff0c\u8fd9\u4f1a\u5927\u5927\u964d\u4f4e\u9009\u62e9ref\/range\u7684\u6267\u884c\u6210\u672c\uff0c<strong>Intersection<\/strong>\u7684\u4f18\u52bf\u4f1a\u5927\u5927\u4e0b\u964d\u3002<\/p>\n<p>\u5230\u6b64\uff0cMySQL index merge\u8c03\u7814\u5c31\u544a\u4e00\u6bb5\u843d\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9664\u4e86\u524d\u9762\u4ecb\u7ecd\u7684\u5e38\u89c1index merge\u7684\u6848\u4f8b(Index Merge Union Access Algorithm)\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u7c7b\u5f88\u5c11\u89c1\u4e5f\u6bd4\u8f83\u7279\u6b8a\u7684index merge\uff0c\u591a\u4e2a\u7d22\u5f15\u626b\u63cf\u540e\u8fdb\u884c\u4ea4\u96c6\uff0c\u5373 Index Merge Intersection\u3002\u8fd9\u7c7b\u6267\u884c\u8ba1\u5212\u6bd4\u8f83\u5c11\u89c1(\u56e0\u4e3aMySQL\u9700\u8981ROR\u7684\u539f\u56e0)\uff0c\u4f46\u662f\uff0c\u5728\u5408\u9002\u7684\u573a\u666f\u4f7f\u7528\uff0c\u6548\u7387\u4ecd\u7136\u4f1a\u6709\u5f88\u5927\u7684\u63d0\u793a\uff0c\u672c\u6587\u5c06\u770b\u770bMySQL\u4f18\u5316\u5668\u5982\u4f55\u8bc4\u4f30\u548c\u9009\u62e9\u6b64\u7c7b\u6267\u884c\u8ba1\u5212\u3002MySQL\u624b\u518c\u5bf9\u6b64\u53ea\u662f\u4e09\u8a00\u4e24\u8bed\u7b80\u5355\u4ecb\u7ecd\u4e86\u4e00\u4e0b\uff0c\u8fd9\u91cc\u505a\u4e2a\u8f83\u4e3a\u8be6\u7ec6\u7684\u8bf4\u660e\u3002 \u8fd9\u7c7b\u6267\u884c\u8ba1\u5212\u5b8c\u6574\u540d\u79f0\u5e94\u8be5\u662f\uff1aThe Index Merge Intersection Access Algorithm\uff0c\u4e0b\u6587\u7b80\u79f0Intersection\u3002 1. \u4e3a\u4ec0\u4e48\u9700\u8981\u8003\u8651Intersection \u8003\u8651\u5982\u4e0b\u67e5\u8be2: SELECT COUNT(*) FROM t1 WHERE key1=1 AND key2=1; \u4f18\u5316\u5668\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7d22\u5f15key1\u6216\u8005key2\u8fdb\u884cREF\/Range\u8bbf\u95ee\uff0c\u5982\u679c\u4f7f\u7528key1\uff0c\u90a3\u4e48key2=1\u5219\u4f5c\u4e3a\u8fc7\u6ee4\u6761\u4ef6\u3002\u53e6\u5916\uff0c\u4f18\u5316\u5668\u8fd8\u4f1a\u8003\u8651\u4f7f\u7528Intersection\uff0c\u5373\u540c\u65f6\u4f7f\u7528\u7d22\u5f15key1\u548ckey2\u3002\u8fd9\u6837\u505a\u53ef\u80fd\u7684\u597d\u5904\u662f\uff1a (a) \u5982\u679c\u4e24\u6b21\u7d22\u5f15\u626b\u63cf\u540e\u505a\u4ea4\u96c6\uff0c\u5982\u679c\u6700\u540eROWID\u5f88\u5c11\uff0c\u5219\u56de\u8868\u6b21\u6570\u5927\u5927\u51cf\u5c11 (b) \u5982\u679c\u626b\u63cf\u8fd9\u4e24\u4e2a\u7d22\u5f15\u80fd\u662f\u8986\u76d6\u626b\u63cf\u7684\u8bdd\uff0c\u5219\u65e0\u9700\u56de\u8868<\/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,5],"tags":[101,118,102,58],"class_list":["post-4322","post","type-post","status-publish","format-standard","hentry","category-mysql","category-code-staff","tag-index","tag-mysql","tag-optimizer","tag-source-code"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4322","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=4322"}],"version-history":[{"count":49,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4322\/revisions"}],"predecessor-version":[{"id":4567,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4322\/revisions\/4567"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=4322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=4322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=4322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}