{"id":5374,"date":"2020-07-29T23:56:04","date_gmt":"2020-07-29T15:56:04","guid":{"rendered":"http:\/\/39.97.231.10\/?p=5374"},"modified":"2020-07-29T23:56:04","modified_gmt":"2020-07-29T15:56:04","slug":"mysql-8-0-%e6%96%b0%e7%89%b9%e6%80%a7-antijoin","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=5374","title":{"rendered":"MySQL 8.0 \u65b0\u7279\u6027-antijoin"},"content":{"rendered":"<div>\nMySQL 8.0.17\u7248\u672c\u5f15\u5165\u4e86\u4e00\u4e2aantijoin\u7684\u4f18\u5316\uff0c\u8fd9\u4e2a\u4f18\u5316\u80fd\u591f\u5c06where\u6761\u4ef6\u4e2d\u7684not in(subquery)\uff0c not exists(subquery)\uff0cin(subquery) is not true\uff0cexists(subquery) is not true\uff0c\u5728\u5185\u90e8\u8f6c\u5316\u6210\u4e00\u4e2aantijoin(\u53cd\u8fde\u63a5\uff09\uff0c\u4ee5\u4fbf\u79fb\u9664\u91cc\u9762\u7684\u5b50\u67e5\u8be2subquery\uff0c\u8fd9\u4e2a\u4f18\u5316\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u80fd\u591f\u5c06\u6027\u80fd\u63d0\u534720%\u5de6\u53f3\u3002\n<\/div>\n<div>\n<div>\n<h4>1. antijoin\u9002\u7528\u573a\u666f<\/h4>\n<p>antijoin\u9002\u7528\u7684\u573a\u666f\u6848\u4f8b\u901a\u5e38\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u627e\u51fa\u5728\u96c6\u5408A\u4e14\u4e0d\u5728\u96c6\u5408B\u4e2d\u7684\u6570\u636e<\/li>\n<li>\u627e\u51fa\u5728\u5f53\u524d\u5b63\u5ea6\u91cc\u6ca1\u6709\u8d2d\u4e70\u5546\u54c1\u7684\u5ba2\u6237<\/li>\n<li>\u627e\u51fa\u4eca\u5e74\u6ca1\u6709\u901a\u8fc7\u8003\u8bd5\u7684\u5b66\u751f<\/li>\n<li>\u627e\u51fa\u8fc7\u53bb3\u5e74\uff0c\u67d0\u4e2a\u533b\u751f\u7684\u75c5\u4eba\u4e2d\u6ca1\u6709\u8fdb\u884c\u533b\u5b66\u68c0\u67e5\u7684\u90e8\u5206<\/li>\n<\/ol>\n<p>\u4e0a\u9762\u8fd9\u4e9b\u573a\u666f\uff0c\u4ee5\u7b2c4\u4e2a\u4e3a\u4f8b\uff0c\u8f6c\u6362\u6210\u4e00\u4e2aSQL\uff0c\u901a\u5e38\u5982\u4e0b\uff1a<\/p>\n<pre>select * from patients where not exists(\n    select * from exams where\n    exams.type=\\'check-up\\' and\n    exams.date&gt;=date_sub(now(), interval 3 year) and\n    exams.patient_id=patients.patient_id\n);\n<\/pre>\n<p>\u5982\u679cSQL\u6309\u7167\u8fd9\u79cd\u5f62\u5f0f\u53bb\u5199\uff0c\u901a\u5e38\u6ca1\u6709\u592a\u591a\u7684\u4f18\u5316\u7a7a\u95f4\u3002\u6211\u4eec\u9700\u8981\u4ecepatients\u8868\u8bfb\u53d6\u6bcf\u6761\u8bb0\u5f55\uff0c\u5bf9\u4e8e\u6bcf\u6761\u8bb0\u5f55\uff0c\u5e26\u5230\u5b50\u67e5\u8be2\u4e2d\uff0c\u68c0\u67e5\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\u3002\u56e0\u4e3a\u5b50\u67e5\u8be2\u7684where\u5b50\u53e5\u4f9d\u8d56patients.patient_id\uff0cpatients \u8868\u7684\u6bcf\u4e00\u6761\u8bb0\u5f55\u904d\u5386\uff0c\u90fd\u4f1a\u5bfc\u81f4\u5b50\u67e5\u8be2\u88ab\u91cd\u590d\u6267\u884c\uff0c\u4e25\u91cd\u5f71\u54cd\u6027\u80fd\u3002<br \/>\n\u4f18\u5316\u8fd9\u79cdSQL\u7684\u7b2c\u4e00\u6b65\u5c31\u662f\u6253\u7834\u4e0a\u5c42\u67e5\u8be2\u4e0e\u5b50\u67e5\u8be2\u4e4b\u95f4\u7684\u8fb9\u754c\uff0c\u5c06\u540e\u8005\u4e5f\u5c31\u662f\u5b50\u67e5\u8be2\u5408\u5e76\u5230\u4e0a\u5c42\u67e5\u8be2\u91cc\u9762\u3002<br \/>\n\u770b\u4e00\u4e0b\u4f18\u5316\u4e4b\u540e\u7684SQL\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>select * from patients antijoin exams on\n    exams.type=\\'check-up\\' and\n    exams.date&gt;=date_sub(now(), interval 3 year) and\n    patients.patient_id=exams.patient_id;\n<\/pre>\n<p>\u4e0a\u9762\u7684SQL\u4f7f\u7528\u4e86\u5173\u952e\u5b57antijoin\uff0c\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u5173\u952e\u5b57\u662f\u4e0d\u5b58\u5728\u7684\uff0c\u6216\u8005\u8bf4\u5b83\u53ea\u5728MySQL\u5185\u90e8\u4f7f\u7528\uff0c\u7528\u6237\u5e76\u4e0d\u77e5\u9053\u5b83\uff0c\u5b83\u548cjoin\u64cd\u4f5c\u7c7b\u4f3c\uff0cjoin\u901a\u5e38\u5bfb\u627e\u6ee1\u8db3\u5339\u914d\u6761\u4ef6\u7684\u8bb0\u5f55\uff0c\u800cantijoin\u5bfb\u627e\u4e0d\u5339\u914d\u7684\u8bb0\u5f55\u3002\u66f4\u51c6\u786e\u5730\u8bf4\uff0c\u5b83\u4ece\u5de6\u8fb9\u8868\u9009\u62e9\u8bb0\u5f55\uff0c\u7136\u540e\u68c0\u67e5\u53f3\u8fb9\u8868\uff0c\u6839\u636eon\u6761\u4ef6\uff0c\u68c0\u67e5\u662f\u5426\u6ca1\u6709\u8bb0\u5f55\u80fd\u591f\u5339\u914d\u4e0a\uff0c\u5982\u679c\u6ca1\u6709\u8bb0\u5f55\u5339\u914d\uff0c\u90a3\u4e48\u5de6\u8fb9\u7684\u8fd9\u6761\u8bb0\u5f55\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u7ed3\u679c\u8fd4\u56de\u3002<br \/>\n\u67e5\u770bSQL\u7684\u6267\u884c\u8ba1\u5212\uff0c\u52a0\u4e0a format=tree\uff0c\u5c31\u80fd\u770b\u5230\u6267\u884c\u8ba1\u5212\u4e2d\u4f7f\u7528\u4e86antijoin\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre> -&gt; Nested loop anti-join\n    -&gt; Table scan on tb1  (cost=850482.22 rows=8372128)\n    -&gt; Single-row index lookup on &lt;subquery2&gt; using &lt;auto_distinct_key&gt; (id=tb1.id)\n        -&gt; Materialize with deduplication\n            -&gt; Index scan on tb2 using PRIMARY  (cost=1.85 rows=16)\n<\/pre>\n<h4>2. antijoin\u5185\u90e8\u4f18\u5316\u7b56\u7565<\/h4>\n<p>MySQL\u6709\u4e24\u79cd\u7b56\u7565\u7528\u4e8e\u6267\u884cantijoin\u3002<\/p>\n<ul>\n<li>First Match<\/li>\n<li>Materialization<\/li>\n<\/ul>\n<h5>2.1 First Match\u7b56\u7565<\/h5>\n<p>First Match \u7b56\u7565\uff0c\u4ecepatients\u8868\u4e2d\u8bfb\u53d6\u4e00\u6761\u8bb0\u5f55\uff0c\u7136\u540e\u5728exams\u8868\u4e2d\u5bfb\u627e\u5339\u914d\uff0c\u5982\u679c\u6ca1\u6709\u5339\u914d\u4e0a\uff0c\u5219\u5c06\u8fd9\u6761\u8bb0\u5f55\u4f5c\u4e3a\u7ed3\u679c\u8fd4\u56de\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0e\u4f7f\u7528\u5b50\u67e5\u8be2\u5e76\u6ca1\u6709\u592a\u5927\u7684\u533a\u522b\u3002<\/p>\n<h5>2.2 Materialization\u7b56\u7565<\/h5>\n<p>Materialization\u7b56\u7565\uff0c\u5bf9\u4e8e\u4e0a\u8ff0SQL\u4f8b\u5b50\uff0cON\u5b50\u53e5\u67093\u4e2a\u5b50\u6761\u4ef6\uff0c\u5206\u522b\u662f<\/p>\n<ul>\n<li>exams.type=\\&#8217;check-up\\&#8217;<\/li>\n<li>exams.date&gt;=date_sub(now(), interval 3 year)<\/li>\n<li>patients.patient_id=exams.patient_id<\/li>\n<\/ul>\n<p>3\u4e2a\u6761\u4ef6\u4e2d\u53ea\u6709\u4e00\u4e2a\u4f9d\u8d56patients\u8868\uff0c\u6240\u4ee5MySQL\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u8868tmp\uff0c\u8fd9\u4e2a\u4e34\u65f6\u8868\u7531exams\u8868\u6309\u7167\u524d\u4e24\u4e2a\u6761\u4ef6\u8fc7\u6ee4\u540e\u7684\u6570\u636e\u7ec4\u6210\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>create table tmp\n    select patient_id from exams where\n    exams.type=\\'check-up\\' and\n    exams.date&gt;=date_sub(now(), interval 3 year);\n<\/pre>\n<p>MySQL\u4f18\u5316\u5668\u4f1a\u81ea\u52a8\u5728tmp\u7684patient_id\u5b57\u6bb5\u4e0a\u6dfb\u52a0\u7d22\u5f15\uff0c\u7136\u540e\u4ecepatients\u8868\u8bfb\u53d6\u8bb0\u5f55\uff0c\u4f7f\u7528\u7d22\u5f15\u5339\u914dtmp\u8868\u4e2d\u7684\u8bb0\u5f55\uff0c\u5982\u679c\u6ca1\u6709\u5339\u914d\u4e0a\uff0c\u5219\u8fd4\u56de\u8fd9\u6761\u8bb0\u5f55\u3002<br \/>\n\u76f8\u5bf9\u4e8eFirst Match\u7b56\u7565\uff0cMaterialization\u7b56\u7565\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u52bf\uff1a<\/p>\n<ol>\n<li>exams\u8868\u53ea\u8bfb\u53d6\u4e00\u6b21\uff0c\u7528\u4e8e\u521b\u5efatmp\u8868\u3002<\/li>\n<li>tmp\u8868\u76f8\u5bf9\u4e8eexams\u8868\uff0c\u6709\u66f4\u5c11\u7684\u8bb0\u5f55\uff0c\u8bbf\u95eetmp\u8868\u8981\u6bd4exams\u8868\u66f4\u5feb\u3002<\/li>\n<li>tmp\u8868\u4e0a\u7531\u4e8e\u521b\u5efa\u4e86\u7d22\u5f15\uff0c\u8bbf\u95ee\u8d77\u6765\u66f4\u5feb\uff0c\u800c\u539f\u59cb\u8868exams\u5f88\u53ef\u80fd\u6ca1\u6709\u7d22\u5f15\u3002<\/li>\n<\/ol>\n<p>\u5f53\u7136\uff0c\u8fd9\u79cd\u7b56\u7565\u521b\u5efa\u4e34\u65f6\u8868\uff0c\u4e5f\u6709\u4e00\u4e9b\u524d\u671f\u7684\u6210\u672c\u6d88\u8017\uff0c\u6bd4\u5982\u9700\u8981\u7533\u8bf7\u5185\u5b58\u6765\u5b58\u50a8\u4e34\u65f6\u8868\u6570\u636e\uff0c\u6bd4\u5982\u4e34\u65f6\u8868\u975e\u5e38\u5927\uff0c\u9700\u8981\u5c06\u4e34\u65f6\u8868\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a\u3002\u56e0\u6b64\u4e24\u79cd\u7b56\u7565\u54ea\u4e00\u79cd\u66f4\u597d\uff0c\u4f9d\u8d56\u4e8e\u5177\u4f53\u7684\u573a\u666f\u3002\u5e78\u8fd0\u7684\u662f\uff0cMySQL\u6709\u4e00\u4e2a\u57fa\u4e8e\u6210\u672c\u7684\u4f18\u5316\u5668\uff0c\u901a\u8fc7\u8ba1\u7b97\u4e24\u79cd\u7b56\u7565\u57fa\u4e8e\u6570\u636e\u884c\u6570\u7684\u6210\u672c\uff0c\u6761\u4ef6\u7684\u9009\u62e9\u6027\uff0c\u7d22\u5f15\u7684\u4f7f\u7528\uff0c\u6700\u7ec8\u9009\u62e9\u6210\u672c\u6700\u4f4e\u7684\u90a3\u4e2a\u7b56\u7565\u3002<\/p>\n<h4>3. \u603b\u7ed3<\/h4>\n<ul>\n<li>antijoin\u4f18\u5316\u9002\u7528\u4e8enot exists(subquery), not in(subquery)\u8fd9\u7c7b\u67e5\u8be2\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u80fd\u591f\u5bf9\u8fd9\u7c7bSQL\u8fdb\u884c\u5f88\u597d\u7684\u4f18\u5316\u548c\u6027\u80fd\u63d0\u9ad8\u3002<\/li>\n<li>antijoin\u6709\u4e24\u79cd\u6267\u884c\u7b56\u7565\uff0cFirst Match\u548cMaterialization\uff0c\u4f18\u5316\u5668\u6839\u636e\u6210\u672c\u6a21\u578b\u8fdb\u884c\u9009\u62e9\u3002<\/li>\n<\/ul>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL 8.0.17\u7248\u672c\u5f15\u5165\u4e86\u4e00\u4e2aantijoin\u7684\u4f18\u5316\uff0c\u8fd9\u4e2a\u4f18\u5316\u80fd\u591f\u5c06where\u6761\u4ef6\u4e2d\u7684not in(s &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=5374\"> <span class=\"screen-reader-text\">MySQL 8.0 \u65b0\u7279\u6027-antijoin<\/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.17\u7248\u672c\u5f15\u5165\u4e86\u4e00\u4e2aantijoin\u7684\u4f18\u5316\uff0c\u8fd9\u4e2a\u4f18\u5316\u80fd\u591f\u5c06where\u6761\u4ef6\u4e2d\u7684not in(s&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5374"}],"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=5374"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5374\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5374"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}