{"id":12902,"date":"2023-10-11T15:20:37","date_gmt":"2023-10-11T07:20:37","guid":{"rendered":"https:\/\/92it.top\/?p=12902"},"modified":"2023-10-11T15:56:33","modified_gmt":"2023-10-11T07:56:33","slug":"sql-in%e6%9f%a5%e8%af%a2%e6%95%88%e7%8e%87%e6%85%a2%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=12902","title":{"rendered":"SQL IN\u67e5\u8be2\u6548\u7387\u6162\u7684\u95ee\u9898"},"content":{"rendered":"\n<p>\u6700\u8fd1\u9879\u76ee\u7684\u8001\u4ee3\u7801\u53c8\u51fa\u95ee\u9898\u4e86\uff0cmysql\u7684in\u67e5\u8be2\u4e00\u6b21\u67e5\u8be2\u4e865\u4e07\u4e2a\u6570\u636e\uff0ccpu\u4e00\u4e0b\u98d9\u5347\u5230100%\uff0c\u8fd9\u6761\u67e5\u8be2sql\u4e5f\u8fd0\u884c\u7684\u8fd1600s\uff0c\u90a3\u4e48\u662f\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4mysql\u7684in\u67e5\u8be2\u6548\u7387\u5982\u6b64\u4e4b\u4f4e\uff0c\u5e76\u4e14\u6ca1\u6709\u8d70\u7d22\u5f15\u5462\uff1f<\/p>\n\n\n\n<p><strong>IN\u7684\u53d6\u503c\u8303\u56f4\u8f83\u5927<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u56e0\u4e3a\u672c\u6b21\u7684\u67e5\u8be2sql\u7684in\u67e5\u8be2\u7684\u8303\u56f4\u5927\u7ea6\u67095\u4e07\u6761\uff0c\u5f53IN\u7684\u53d6\u503c\u8303\u56f4\u8f83\u5927\u65f6\u4f1a\u5bfc\u81f4\u7d22\u5f15\u5931\u6548\uff0c\u8d70\u5168\u8868\u626b\u63cf\u3002<br>\u90a3\u4e48IN\u7684\u53d6\u503c\u8303\u56f4\u6709\u591a\u5c11\u6761\u7684\u65f6\u5019\u4f1a\u8d70\u7d22\u5f15\u5462\uff1f<br>\u65b0\u5efa\u4e86\u4e00\u5f20\u8868\uff0ctest_id\u6709\u7d22\u5f15\uff0c\u63d2\u5165\u4e8630\u4e07\u7684\u6570\u636e\u3002<br>explain\u4e0a\u8ff0sql\uff0c\u53d1\u73b0in\u7684\u53d6\u503c\u8303\u56f4\u572810000\u6761\u4ee5\u5185\u7684\u65f6\u5019\uff0c\u6b64\u6761\u67e5\u8be2\u8d70\u7d22\u5f15\u3002<br>\u4f46\u662f\u4e00\u65e6in\u7684\u53d6\u503c\u8303\u56f4\u8d85\u8fc710000\u6761\u540e\uff0c\u5c06\u4e0d\u4f1a\u8d70\u7d22\u5f15\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\u5728in\u7684\u5b57\u6bb5\u6dfb\u52a0\u4e86\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\uff0c\u6700\u7ec8in\u8d70\u4e0d\u8d70\u7d22\u5f15\u7531mysql \u4f18\u5316\u6267\u884c\u5668\u53bb\u5224\u65ad\uff0c\u6b64\u5e95\u5c42\u903b\u8f91\u8f83\u4e3a\u590d\u6742\uff0c\u4f46\u662f<strong>\u5728\u7b14\u8005\u7684\u6761\u4ef6\u4e0b\uff0cin\u7684\u53d6\u503c\u8303\u56f4\u9650\u5236\u572810000\u6761\u4ee5\u5185\u65f6\uff0c\u5c06\u8d70\u7d22\u5f15<\/strong>\u3002<\/p>\n\n\n\n<p><strong>PostgreSQL in\u4e5f\u6709\u8fd9\u4e2a\u95ee\u9898<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5e94\u8be5\u6709\u4e00\u90e8\u5206\u539f\u56e0 \u8ddf\u7d22\u5f15\u6709\u5173\u7cfb\uff0c\u53d1\u73b0\u5f53in \u9879\u76ee\u8fc7\u591a\u65f6\uff0c\u7d22\u5f15\u4f1a\u5931\u6548\u3002\u76ee\u524d\u672c\u5730\u6d4b\u8bd5\u662f&nbsp;<strong>in\u9879\u76ee\u6570&gt;8&nbsp;<\/strong>\u65f6\uff0cSQL\u7684\u7d22\u5f15\u5931\u6548\u3002PS\uff1a\u53ef\u4ee5\u7528&nbsp;<strong>explain<\/strong>\u5173\u952e\u5b57\u5206\u6790\u6267\u884c\u7684SQL\u7684\u8fc7\u7a0b\u3002\u7d22\u5f15\u6709\u6548\uff1ain<strong>\u9879\u76ee\u6570 =8<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">explain SELECT * FROM tableA where no in ('1','2','3','4','5','6','7','8')\n\n\u8fd4\u56de\u7ed3\u679c\uff1a\nBitmap Index Scan on tableA_pk  (cost=0.00..5.24 rows=8 width=0)\nIndex Cond: ((no)::text = ANY ('{1,2,3,4,5,6,7,8}'::text[]))\n\n==\u300b\u53ef\u4ee5\u770b\u5230\u662f Index Scan<\/pre>\n\n\n\n<p>\u7d22\u5f15\u5931\u6548\uff1ain<strong>\u9879\u76ee\u6570 &gt;8<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">explain SELECT * FROM tableA where no in ('1','2','3','4','5','6','7','8','9')\n\n\u8fd4\u56de\u7ed3\u679c\uff1a\nSeq Scan on tableA  (cost=0.02..15.72 rows=9 width=183)\nFilter: ((no)::text = ANY ('{1,2,3,4,5,6,7,8,9}'::text[]))\n\n==\u300b\u53ef\u4ee5\u770b\u5230\u53d8\u6210\u4e86Seq Scan\uff0c\u5c31\u662f\u987a\u5e8f\u68c0\u7d22\uff0c\u5168\u8868\u626b\u63cf\u4e86\u3002<\/pre>\n\n\n\n<p><br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-1024x874.png\" alt=\"\" class=\"wp-image-12903\" width=\"477\" height=\"406\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-1024x874.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-300x256.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-768x656.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-830x709.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-230x196.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-350x299.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21-480x410.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-21.png 1380w\" sizes=\"(max-width: 477px) 100vw, 477px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4ee3\u66ffin\u7684\u5199\u6cd5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">explain WITH cte AS (\n  SELECT unnest(ARRAY['1','2','3','4','5','6','7','8']) AS number\n)\n\n SELECT * FROM tableA inner join cte on no = cte.number;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Hash Join  (cost=0.24..15.54 rows=8 width=215)\"\nHash Cond: ((tableA.no)::text = (unnest('{1,2,3,4,5,6,7,8}'::text[])))\"\nSeq Scan on tableA  (cost=0.00..13.80 rows=380 width=183)\"\nHash  (cost=0.14..0.14 rows=8 width=32)\"\nProjectSet  (cost=0.00..0.06 rows=8 width=32)\"\nResult  (cost=0.00..0.01 rows=1 width=0)\"<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-1024x924.png\" alt=\"\" class=\"wp-image-12906\" width=\"483\" height=\"435\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-1024x924.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-300x271.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-768x693.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-830x749.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-230x208.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-350x316.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22-480x433.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/10\/\u56fe\u7247-22.png 1354w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u9879\u76ee\u7684\u8001\u4ee3\u7801\u53c8\u51fa\u95ee\u9898\u4e86\uff0cmysql\u7684in\u67e5\u8be2\u4e00\u6b21\u67e5\u8be2\u4e865\u4e07\u4e2a\u6570\u636e\uff0ccpu\u4e00\u4e0b\u98d9\u5347\u5230100%\uff0c\u8fd9\u6761\u67e5\u8be2sql [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12902"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12902"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12902\/revisions"}],"predecessor-version":[{"id":12907,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12902\/revisions\/12907"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}