{"id":1836,"date":"2020-07-01T06:12:04","date_gmt":"2020-06-30T22:12:04","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1836"},"modified":"2020-07-01T06:12:04","modified_gmt":"2020-06-30T22:12:04","slug":"mysql-8-0-hash-join","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1836","title":{"rendered":"MySQL 8.0 hash join"},"content":{"rendered":"<div>\n<div>\n<div>\n<div>MySQL 8.0.18 \u7248\u672c\u589e\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u7279\u6027hash join\uff0c\u5173\u4e8ehash join\uff0c\u901a\u5e38\u5176\u6267\u884c\u8fc7\u7a0b\u5982\u4e0b\uff0c\u9996\u5148\u57fa\u4e8ejoin\u64cd\u4f5c\u7684\u4e00\u4e2a\u8868\uff0c\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684hash\u8868\uff0c\u7136\u540e\u518d\u4e00\u884c\u4e00\u884c\u7684\u8bfb\u53e6\u5916\u4e00\u5f20\u8868\uff0c\u901a\u8fc7\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u67e5\u627e\u5185\u5b58\u4e2d\u7684\u54c8\u5e0c\u8868\u3002<\/div>\n<\/div>\n<\/div>\n<\/div>\n<article>\u8fd9\u79cd\u65b9\u5f0f\u771f\u7684\u80fd\u5e26\u6765\u5f88\u5927\u7684\u6027\u80fd\u63d0\u5347\u5417\uff1f<br \/>\nhash join\u4ec5\u4ec5\u5728join\u7684\u5b57\u6bb5\u4e0a\u6ca1\u6709\u7d22\u5f15\u65f6\u624d\u8d77\u4f5c\u7528\uff0c\u5728\u6b64\u4e4b\u524d\uff0c\u6211\u4eec\u4e0d\u5efa\u8bae\u5728\u6ca1\u6709\u7d22\u5f15\u7684\u5b57\u6bb5\u4e0a\u505ajoin\u64cd\u4f5c\uff0c\u56e0\u4e3a\u901a\u5e38\u4e2d\u8fd9\u79cd\u64cd\u4f5c\u4f1a\u6267\u884c\u7684\u5f88\u6162\uff0c\u4f46\u662f\u6709\u4e86hash join\uff0c\u5b83\u80fd\u591f\u521b\u5efa\u4e00\u4e2a\u5185\u5b58\u4e2d\u7684hash \u8868\uff0c\u4ee3\u66ff\u4e4b\u524d\u7684nested loop\uff0c\u4f7f\u5f97\u6ca1\u6709\u7d22\u5f15\u7684\u7b49\u503cjoin\u6027\u80fd\u63d0\u5347\u5f88\u591a\u3002<br \/>\n\u901a\u8fc7\u793a\u4f8b\u6765\u770b\u4e0b\u6027\u80fd\u6709\u591a\u5927\u63d0\u5347\u3002<\/p>\n<div>\n<pre>CREATE TABLE `t1` (\n`id` int(11) NOT NULL AUTO_INCREMENT ,\n`c1` int(11) NOT NULL DEFAULT \\'0\\',\n`c2` int(11) NOT NULL DEFAULT \\'0\\',\nPRIMARY KEY (`id`),\nKEY `idx_c1` (`c1`)\n) ENGINE=InnoDB;\nCREATE TABLE `t2` (\n`id` int(11) NOT NULL AUTO_INCREMENT ,\n`c1` int(11) NOT NULL DEFAULT \\'0\\',\n`c2` int(11) NOT NULL DEFAULT \\'0\\',\nPRIMARY KEY (`id`),\nKEY `idx_c1` (`c1`)\n) ENGINE=InnoDB;\n<\/pre>\n<\/div>\n<p>\u968f\u673a\u63d2\u5165 131072 \u6761\u8bb0\u5f55\u5230\u4e24\u5f20\u8868\u4e2d\u3002<\/p>\n<div>\n<pre>mysql&gt; select count(*) from t1;\n+----------+\n| count(*) |\n+----------+\n| 131072   |\n+----------+\n<\/pre>\n<\/div>\n<p>c2\u5b57\u6bb5\u6ca1\u6709\u7d22\u5f15\uff0c\u770b\u4e0bhash join\u7684\u6267\u884c\u8ba1\u5212\uff1a<\/p>\n<div>\n<pre>mysql&gt; explain format=tree select count(*) from t1 join t2 on t1.c2 = t2.c2\\\\G\n*************************** 1. row ***************************\nEXPLAIN: -&gt; Aggregate: count(0)\n-&gt; Inner hash join (t2.c2 = t1.c2) (cost=1728502115.04 rows=1728488704)\n-&gt; Table scan on t2 (cost=0.01 rows=131472)\n-&gt; Hash\n-&gt; Table scan on t1 (cost=13219.45 rows=131472)\n1 row in set (0.00 sec)\n<\/pre>\n<\/div>\n<p>\u901a\u8fc7 explain format=tree \u80fd\u591f\u770b\u5230hash join\u662f\u5426\u88ab\u4f7f\u7528\uff0c\u8fd9\u662f\u65b0\u52a0\u7684\u529f\u80fd\uff0c\u800c\u4f20\u7edf\u7684explain\u4ecd\u7136\u4f1a\u663e\u793anested loop\uff0c\u8fd9\u79cd\u60c5\u51b5\u5bb9\u6613\u4ea7\u751f\u8bef\u89e3\u3002<br \/>\n\u6d4b\u8bd5\u4e00\uff0chash join<\/p>\n<div>\n<pre>mysql&gt; select count(*) from t1 join t2 on t1.c2 = t2.c2;\n+----------+\n| count(*) |\n+----------+\n| 17172231 |\n+----------+\n1 row in set (0.73 sec)\n<\/pre>\n<\/div>\n<p>\u5b9e\u9645\u6267\u884c\u65f6\uff0chash join\u4e00\u5171\u8017\u65f60.73\u79d2\u3002<br \/>\n\u6d4b\u8bd5\u4e8c\uff0c\u975ehash join<br \/>\n\u901a\u8fc7\u7981\u7528hash join\uff0c\u770b\u4e0b\u6267\u884c\u8017\u65f6\uff0c\u5982\u4e0b\uff1a<\/p>\n<div>\n<pre>mysql&gt; select \/*+ NO_HASH_JOIN (t1,t2) *\/ count(*) from t1 join t2 on t1.c2 = t2.c2;\n+----------+\n| count(*) |\n+----------+\n| 17172231 |\n+----------+\n1 row in set (13 min 36.36 sec)\n<\/pre>\n<\/div>\n<p>\u4e00\u5171\u8017\u65f613\u520636\u79d2\u3002<br \/>\n\u6d4b\u8bd5\u4e09\uff0c\u57fa\u4e8e\u7d22\u5f15\u7684join<\/p>\n<div>\n<pre>create index idx_c2 on t1(c2);\ncreate index idx_c2 on t2(c2);\nmysql&gt; select count(*) from t1 join t2 on t1.c2 = t2.c2;\n+----------+\n| count(*) |\n+----------+\n| 17172231 |\n+----------+\n1 row in set (2.63 sec)\n<\/pre>\n<\/div>\n<p>\u4e00\u5171\u8017\u65f6 2.63\u79d2\u3002<br \/>\n\u7ed3\u679c\u5bf9\u6bd4\uff1a<\/p>\n<ul>\n<li>hash join\uff1a0.73\u79d2<\/li>\n<li>\u975ehash join\uff1a 13\u520636.36\u79d2<\/li>\n<li>\u57fa\u4e8e\u7d22\u5f15\u7684join\uff1a2.63\u79d2<\/li>\n<\/ul>\n<p>\u4ece\u7ed3\u679c\u5bf9\u6bd4\u53ef\u4ee5\u770b\u51fa\uff0chash join\u6267\u884c\u6027\u80fd\u786e\u5b9e\u63d0\u5347\u5f88\u591a\u3002<br \/>\n\u914d\u7f6ehash join\u529f\u80fd\u662f\u5426\u5f00\u542f\uff1a<\/p>\n<ul>\n<li>optimizer_switch \u4e2d\u7684 hash_join=on\/off\uff0c\u9ed8\u8ba4\u4e3aon<\/li>\n<li>sql\u8bed\u53e5\u4e2d\u6307\u5b9aHASH_JOIN\u6216\u8005NO_HASH_JOIN<\/li>\n<\/ul>\n<p>\u9650\u5236\uff1a<\/p>\n<ul>\n<li>hash join\u53ea\u80fd\u5728\u6ca1\u6709\u7d22\u5f15\u7684\u5b57\u6bb5\u4e0a\u6709\u6548<\/li>\n<li>hash join\u53ea\u5728\u7b49\u503cjoin\u6761\u4ef6\u4e2d\u6709\u6548<\/li>\n<li>hash join\u4e0d\u80fd\u7528\u4e8eleft join\u548cright join<\/li>\n<\/ul>\n<p>\u6700\u540e\uff1a<br \/>\nhash join \u662f\u4e00\u4e2a\u6bd4\u8f83\u5f3a\u5927\u7684join\u9009\u9879\uff0c\u4e5f\u8bb8\u8fd9\u4ec5\u4ec5\u662f\u4e00\u4e2a\u5f00\u5934\uff0c\u5728\u4e0d\u4e45\u7684\u5c06\u6765\uff0c\u57fa\u4e8e\u5b83\u53ef\u80fd\u4f1a\u5ef6\u4f38\u51fa\u66f4\u591a\u66f4\u5f3a\u5927\u7684\u7279\u6027\uff0c\u6bd4\u5982\u652f\u6301left join\u548cright join\u7b49\u7b49\u3002<br \/>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>MySQL 8.0.18 \u7248\u672c\u589e\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u7279\u6027hash join\uff0c\u5173\u4e8ehash join\uff0c\u901a\u5e38\u5176\u6267\u884c\u8fc7\u7a0b\u5982\u4e0b &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1836\"> <span class=\"screen-reader-text\">MySQL 8.0 hash join<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"MySQL 8.0.18 \u7248\u672c\u589e\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u7279\u6027hash join\uff0c\u5173\u4e8ehash join\uff0c\u901a\u5e38\u5176\u6267\u884c\u8fc7\u7a0b\u5982\u4e0b&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1836"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1836"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1836\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1836"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}