{"id":2013,"date":"2023-04-01T12:20:48","date_gmt":"2023-04-01T04:20:48","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2013"},"modified":"2023-04-07T10:37:20","modified_gmt":"2023-04-07T02:37:20","slug":"difference-between-exists-and-in-in-mysql","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/difference-between-exists-and-in-in-mysql\/","title":{"rendered":"MySQL \u4e2d exists \u548c in \u7684\u533a\u522b"},"content":{"rendered":"<p>\u4e0b\u9762\u5c06\u4e3b\u67e5\u8be2\u7684\u8868\u79f0\u4e3a\u5916\u8868\uff1b\u5b50\u67e5\u8be2\u7684\u8868\u79f0\u4e3a\u5185\u8868\u3002exists \u4e0e in \u7684\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5b50\u67e5\u8be2\u4f7f\u7528 exists\uff0c\u4f1a\u5148\u8fdb\u884c\u4e3b\u67e5\u8be2\uff0c\u5c06\u67e5\u8be2\u5230\u7684\u6bcf\u884c\u6570\u636e\u5faa\u73af\u5e26\u5165\u5b50\u67e5\u8be2\u6821\u9a8c\u662f\u5426\u5b58\u5728\uff0c\u8fc7\u6ee4\u51fa\u6574\u4f53\u7684\u8fd4\u56de\u6570\u636e\uff1b\u5b50\u67e5\u8be2\u4f7f\u7528 in\uff0c\u4f1a\u5148\u8fdb\u884c\u5b50\u67e5\u8be2\u83b7\u53d6\u7ed3\u679c\u96c6\uff0c\u7136\u540e\u4e3b\u67e5\u8be2\u5339\u914d\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0c\u8fd4\u56de\u6570\u636e<\/li>\n<li>\u5916\u8868\u5185\u8868\u76f8\u5bf9\u5927\u5c0f\u60c5\u51b5\u4e0d\u4e00\u6837\u65f6\uff0c\u67e5\u8be2\u6548\u7387\u4e0d\u4e00\u6837\uff1a\u4e24\u8868\u5927\u5c0f\u76f8\u5f53\uff0cin \u548c exists \u5dee\u522b\u4e0d\u5927\uff1b\u5185\u8868\u5927\uff0c\u7528 exists \u6548\u7387\u8f83\u9ad8\uff1b\u5185\u8868\u5c0f\uff0c\u7528 in \u6548\u7387\u8f83\u9ad8<\/li>\n<li>\u4e0d\u7ba1\u5916\u8868\u4e0e\u5185\u8868\u7684\u5927\u5c0f\uff0c<code>not exists<\/code>\u7684\u6548\u7387\u4e00\u822c\u8981\u9ad8\u4e8e<code>not in<\/code>\uff0c\u8ddf\u5b50\u67e5\u8be2\u7684\u7d22\u5f15\u8bbf\u95ee\u7c7b\u578b\u6709\u5173<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u5efa\u8868\u3001\u9020\u6570\u636e<\/h2>\n<pre><code class=\"language-sql\"># \u5efa\u8868 student1\ndrop table if exists student1;\ncreate table student1(\nsid int primary key auto_increment,\nsname varchar(40)\n);\n\n# \u5efa\u5b58\u50a8\u8fc7\u7a0b\u7ed9\u8868 student1\uff0c\u63d2\u51651000\u6761\u6570\u636e\ndrop procedure if exists addStudent1;\ncreate procedure addStudent1()\nBEGIN\n  declare idx int;\n  set idx = 1;\n  while idx &lt;= 1000 DO\n    insert into student1 values(null, concat(&#039;student-&#039;, idx));\n    set idx = idx + 1;\n  end while;\nend;\n\ncall addStudent1();\n\nselect * from student1;\n\n# \u5efa\u8868 student2\ndrop table if exists student2;\ncreate table student2(\nsid int primary key auto_increment,\nsname varchar(40)\n);\n\n# \u5efa\u5b58\u50a8\u8fc7\u7a0b\u7ed9\u8868 student2\uff0c\u63d2\u5165100000\u6761\u6570\u636e\ndrop procedure if exists addStudent2;\ncreate procedure addStudent2()\nBEGIN\n  declare idx int;\n  set idx = 1;\n  while idx &lt;= 100000 DO\n    insert into student2 values(null, concat(&#039;student-&#039;, idx));\n    set idx = idx + 1;\n  end while;\nend;\n\ncall addStudent2();\n\nselect * from student2;<\/code><\/pre>\n<h2>in \u4e0e exists \u7684\u67e5\u8be2 SQL<\/h2>\n<pre><code class=\"language-sql\">select count(1) from student1 where sname in (select sname from student2);\nselect count(1) from student1 where exists (select sname from student2 where student2.sname = student1.sname);\nselect count(1) from student2 where sname in (select sname from student1);\nselect count(1) from student2 where exists (select sname from student1 where student2.sname = student1.sname);<\/code><\/pre>\n<p>\u6267\u884c\u65f6\u95f4\uff1a<\/p>\n<pre><code class=\"language-sql\">[SQL]\nselect count(1) from student1 where sname in (select sname from student2);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.092s\n\n[SQL] \nselect count(1) from student1 where exists (select sname from student2 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.076s\n\n[SQL] \nselect count(1) from student2 where sname in (select sname from student1);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 14.820s\n\n[SQL] \nselect count(1) from student2 where exists (select sname from student1 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 15.144s<\/code><\/pre>\n<blockquote>\n<p>\u7ed3\u8bba\uff1astudent2 \u5927\u8868\u5728\u5185\u9002\u7528 exists\uff0c\u6240\u4ee5\u7b2c 2 \u6761 SQL \u6bd4\u7b2c 1 \u6761\u5feb\uff1bstudent1 \u5c0f\u8868\u5728\u5185\u9002\u7528 in\uff0c\u6240\u4ee5\u7b2c 3 \u6761 SQL \u6bd4\u7b2c 4 \u6761\u5feb\u3002<\/p>\n<\/blockquote>\n<h2>not in \u4e0e not exists \u7684\u67e5\u8be2 SQL<\/h2>\n<pre><code class=\"language-sql\">select count(1) from student1 where sname not in (select sname from student2);\nselect count(1) from student1 where not exists (select sname from student2 where student2.sname = student1.sname);\nselect count(1) from student2 where sname not in (select sname from student1);\nselect count(1) from student2 where not exists (select sname from student1 where student2.sname = student1.sname);<\/code><\/pre>\n<p>\u6267\u884c\u65f6\u95f4\uff1a<\/p>\n<pre><code class=\"language-sql\">[SQL] \nselect count(1) from student1 where sname not in (select sname from student2);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.079s\n\n[SQL] \nselect count(1) from student1 where not exists (select sname from student2 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.075s\n\n[SQL] \nelect count(1) from student2 where sname not in (select sname from student1);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 15.797s\n\n[SQL] \nselect count(1) from student2 where not exists (select sname from student1 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 15.160s<\/code><\/pre>\n<blockquote>\n<p>\u7ed3\u8bba\uff1anot exists \u6027\u80fd\u9ad8\u4e8e not in<\/p>\n<\/blockquote>\n<h2>\u7d22\u5f15\u5f71\u54cd<\/h2>\n<p>\u7ed9 student1\u3001student2 sname \u5b57\u6bb5\uff0c\u52a0\u4e0a\u7d22\u5f15\uff0c\u4e0a\u8ff0\u7ed3\u8bba\u4ecd\u7136\u6210\u7acb\u3002<\/p>\n<pre><code class=\"language-sql\">create index idx_1 on student1(sname);\ncreate index idx_2 on student2(sname);<\/code><\/pre>\n<p>\u6267\u884c\u65f6\u95f4\uff1a<\/p>\n<pre><code class=\"language-sql\">[SQL]\nselect count(1) from student1 where sname in (select sname from student2);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.022s\n\n[SQL] \nselect count(1) from student1 where exists (select sname from student2 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.014s\n\n[SQL] \nselect count(1) from student2 where sname in (select sname from student1);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.379s\n\n[SQL] \nselect count(1) from student2 where exists (select sname from student1 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.373s\n\n[SQL] \nselect count(1) from student1 where sname not in (select sname from student2);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.006s\n\n[SQL] \nselect count(1) from student1 where not exists (select sname from student2 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.006s\n\n[SQL] \nselect count(1) from student2 where sname not in (select sname from student1);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.455s\n\n[SQL] \nselect count(1) from student2 where not exists (select sname from student1 where student2.sname = student1.sname);\n\u53d7\u5f71\u54cd\u7684\u884c: 0\n\u65f6\u95f4: 0.418s<\/code><\/pre>\n<p>\u518d\u7ec6\u770b\u4e00\u4e0b\uff0c<code>not in<\/code>\u4e0e<code>not exists<\/code>\u67e5\u8be2\u7d22\u5f15\u4f7f\u7528\u60c5\u51b5<\/p>\n<ul>\n<li><code>not in<\/code>\uff0c\u5b50\u67e5\u8be2\u4f7f\u7528\u4e86<code>index_subquery<\/code>\u8bbf\u95ee\u7c7b\u578b<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">EXPLAIN EXTENDED select count(1) from student2 where sname not in (select sname from student1);\nSHOW WARNINGS;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u9762\u8bd5\/not%20in%20\u5b50\u67e5\u8be2\u4f7f\u7528%20index_subquery%20\u8bbf\u95ee\u7c7b\u578b.png\" alt=\"not in \u5b50\u67e5\u8be2\u4f7f\u7528 index_subquery \u8bbf\u95ee\u7c7b\u578b\" \/><\/p>\n<ul>\n<li><code>not exists<\/code>\uff0c\u5b50\u67e5\u8be2\u4f7f\u7528\u4e86<code>ref<\/code>\u8bbf\u95ee\u7c7b\u578b<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">EXPLAIN EXTENDED select count(1) from student2 where not exists (select sname from student1 where student2.sname = student1.sname);\nSHOW WARNINGS;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u9762\u8bd5\/not%20exists%20\u5b50\u67e5\u8be2\u4f7f\u7528%20ref%20\u8bbf\u95ee\u7c7b\u578b.png\" alt=\"not exists \u5b50\u67e5\u8be2\u4f7f\u7528 ref \u8bbf\u95ee\u7c7b\u578b\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u5c06\u4e3b\u67e5\u8be2\u7684\u8868\u79f0\u4e3a\u5916\u8868\uff1b\u5b50\u67e5\u8be2\u7684\u8868\u79f0\u4e3a\u5185\u8868\u3002exists \u4e0e in \u7684\u533a\u522b\u5982\u4e0b\uff1a \u5b50\u67e5\u8be2\u4f7f\u7528 exists\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-2013","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2013"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2013\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}