{"id":5919,"date":"2024-11-14T13:37:14","date_gmt":"2024-11-14T05:37:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5919\/"},"modified":"2024-11-14T13:37:14","modified_gmt":"2024-11-14T05:37:14","slug":"sql%e6%9f%a5%e8%af%a2%e6%85%a2%e6%80%8e%e4%b9%88%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5919\/","title":{"rendered":"sql\u67e5\u8be2\u6162\u600e\u4e48\u4f18\u5316"},"content":{"rendered":"<blockquote><p>\n  \u8fd0\u884c\u7f13\u6162\u7684 sql \u67e5\u8be2\u4f18\u5316\u7b56\u7565\uff1a\u786e\u5b9a\u67e5\u8be2\u74f6\u9888\uff1a\u4f7f\u7528 explain \u6216 explain analyze \u8bed\u53e5\u3002\u521b\u5efa\u9002\u5f53\u7684\u7d22\u5f15\uff1a\u4e3a\u7ecf\u5e38\u4f7f\u7528\u7684\u5217\u521b\u5efa\u7d22\u5f15\u3002\u4f18\u5316\u8868\u8fde\u63a5\uff1a\u4f7f\u7528 hash \u6216 merge join\uff0c\u663e\u5f0f\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\u3002\u91cd\u5199\u5b50\u67e5\u8be2\uff1a\u4f7f\u7528\u8fde\u63a5\u6216 exists\/not exists \u6761\u4ef6\u3002\u4f18\u5316\u6392\u5e8f\u548c\u5206\u7ec4\uff1a\u4f7f\u7528\u7d22\u5f15\u7684\u5217\u6392\u5e8f\u6216\u5206\u7ec4\u3002\u5229\u7528\u67e5\u8be2\u7f13\u5b58\uff1a\u5b58\u50a8\u5df2\u6267\u884c\u7684\u67e5\u8be2\u8ba1\u5212\u3002\u8c03\u6574\u6570\u636e\u5e93\u914d\u7f6e\uff1a\u4f18\u5316\u5185\u5b58\u5206\u914d\u7b49\u53c2\u6570\u3002\u786c\u4ef6\u5347\u7ea7\uff1a\u8003\u8651\u589e\u52a0\u5185\u5b58\u6216\u66f4\u6362 cpu\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060322242179563.jpg\" class=\"aligncenter\" title=\"sql\u67e5\u8be2\u6162\u600e\u4e48\u4f18\u5316\u63d2\u56fe\" alt=\"sql\u67e5\u8be2\u6162\u600e\u4e48\u4f18\u5316\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u67e5\u8be2\u6162\u7684\u4f18\u5316<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a\u5982\u4f55\u4f18\u5316\u8fd0\u884c\u7f13\u6162\u7684 SQL \u67e5\u8be2\uff1f<\/strong><\/p>\n<p><strong>\u4f18\u5316\u7b56\u7565\uff1a<\/strong><\/p>\n<p><strong>1. \u786e\u5b9a\u67e5\u8be2\u74f6\u9888\uff1a<\/strong><br \/>\u4f7f\u7528 EXPLAIN \u6216 EXPLAIN ANALYZE \u8bed\u53e5\u786e\u5b9a\u67e5\u8be2\u74f6\u9888\uff0c\u5982\u7d22\u5f15\u7f3a\u5931\u3001\u8868\u8fde\u63a5\u4e0d\u5f53\u6216\u5b50\u67e5\u8be2\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n<p><strong>2. \u521b\u5efa\u9002\u5f53\u7684\u7d22\u5f15\uff1a<\/strong><br \/>\u4e3a\u7ecf\u5e38\u4f7f\u7528\u7684\u5217\u521b\u5efa\u9002\u5f53\u7684\u7d22\u5f15\uff0c\u4ee5\u52a0\u5feb\u67e5\u8be2\u901f\u5ea6\u3002\u786e\u4fdd\u7d22\u5f15\u4e0e\u67e5\u8be2\u4e2d\u7684 WHERE \u6216 JOIN \u6761\u4ef6\u4e00\u81f4\u3002<\/p>\n<p><strong>3. \u4f18\u5316\u8868\u8fde\u63a5\uff1a<\/strong><br \/>\u907f\u514d\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8fde\u63a5\uff0c\u800c\u5e94\u4f7f\u7528\u8f83\u5feb\u7684\u8fde\u63a5\u7c7b\u578b\uff0c\u5982 HASH \u6216 MERGE JOIN\u3002\u4f7f\u7528 ON \u6216 USING \u5b50\u53e5\u663e\u5f0f\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\u3002<\/p>\n<p><strong>4. \u91cd\u5199\u5b50\u67e5\u8be2\uff1a<\/strong><br \/>\u5c06\u5d4c\u5957\u5b50\u67e5\u8be2\u66ff\u6362\u4e3a\u8fde\u63a5\u6216 EXISTS\/NOT EXISTS \u6761\u4ef6\u3002\u8fd9\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u6267\u884c\u7684\u5b50\u67e5\u8be2\u6b21\u6570\u3002<\/p>\n<p><strong>5. \u4f18\u5316\u6392\u5e8f\u548c\u5206\u7ec4\uff1a<\/strong><br \/>\u4f7f\u7528 ORDER BY \u548c GROUP BY \u4f18\u5316\u6392\u5e8f\u548c\u5206\u7ec4\u64cd\u4f5c\u3002\u786e\u4fdd\u6392\u5e8f\u6216\u5206\u7ec4\u7684\u5217\u5df2\u5efa\u7acb\u7d22\u5f15\u3002<\/p>\n<p><strong>6. \u5229\u7528\u67e5\u8be2\u7f13\u5b58\uff1a<\/strong><br \/>\u5982\u679c\u67e5\u8be2\u7ecf\u5e38\u88ab\u6267\u884c\uff0c\u53ef\u4ee5\u5229\u7528\u67e5\u8be2\u7f13\u5b58\u6765\u5b58\u50a8\u548c\u91cd\u7528\u5df2\u6267\u884c\u7684\u67e5\u8be2\u8ba1\u5212\u3002<\/p>\n<p><strong>7. \u8c03\u6574\u6570\u636e\u5e93\u914d\u7f6e\uff1a<\/strong><br \/>\u8c03\u6574\u6570\u636e\u5e93\u914d\u7f6e\u53c2\u6570\uff0c\u5982\u5185\u5b58\u5206\u914d\u3001\u8fde\u63a5\u6c60\u5927\u5c0f\u548c\u67e5\u8be2\u4f18\u5316\u5668\u8bbe\u7f6e\uff0c\u4ee5\u4f18\u5316\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n<p><strong>8. \u786c\u4ef6\u5347\u7ea7\uff1a<\/strong><br \/>\u5982\u679c\u4e0a\u8ff0\u4f18\u5316\u63aa\u65bd\u65e0\u6cd5\u663e\u7740\u63d0\u9ad8\u6027\u80fd\uff0c\u5219\u53ef\u80fd\u9700\u8981\u8003\u8651\u786c\u4ef6\u5347\u7ea7\uff0c\u5982\u589e\u52a0\u670d\u52a1\u5668\u5185\u5b58\u6216\u4f7f\u7528\u66f4\u5feb\u7684 CPU\u3002<\/p>\n<p><strong>\u5176\u4ed6\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b9a\u671f\u5206\u6790\u67e5\u8be2\u65e5\u5fd7\u4ee5\u8bc6\u522b\u957f\u671f\u8fd0\u884c\u7684\u67e5\u8be2\u3002<\/li>\n<li>\u4f7f\u7528\u67e5\u8be2\u4f18\u5316\u5de5\u5177\u6216\u987e\u95ee\u6765\u8bca\u65ad\u548c\u89e3\u51b3\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528 NoSQL \u6570\u636e\u5e93\u6216\u7f13\u5b58\u7cfb\u7edf\u6765\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u9891\u7e41\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u67e5\u8be2\u6162\u600e\u4e48\u4f18\u5316\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd0\u884c\u7f13\u6162\u7684 sql \u67e5\u8be2\u4f18\u5316\u7b56\u7565\uff1a\u786e\u5b9a\u67e5\u8be2\u74f6\u9888\uff1a\u4f7f\u7528 explain \u6216 explain analyze \u8bed\u53e5\u3002\u521b\u5efa\u9002\u5f53\u7684\u7d22\u5f15\uff1a\u4e3a\u7ecf\u5e38\u4f7f\u7528\u7684\u5217\u521b\u5efa\u7d22\u5f15\u3002\u4f18\u5316\u8868\u8fde\u63a5\uff1a\u4f7f\u7528 hash \u6216 merge join\uff0c\u663e\u5f0f\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\u3002\u91cd\u5199\u5b50\u67e5\u8be2\uff1a\u4f7f\u7528\u8fde\u63a5\u6216 exists\/not exists \u6761\u4ef6\u3002\u4f18\u5316\u6392\u5e8f\u548c\u5206\u7ec4\uff1a\u4f7f\u7528\u7d22\u5f15\u7684\u5217\u6392\u5e8f\u6216\u5206\u7ec4\u3002\u5229\u7528\u67e5\u8be2\u7f13\u5b58\uff1a\u5b58\u50a8\u5df2\u6267\u884c\u7684\u67e5\u8be2\u8ba1\u5212\u3002\u8c03\u6574\u6570\u636e\u5e93\u914d\u7f6e\uff1a\u4f18\u5316\u5185\u5b58\u5206\u914d\u7b49\u53c2\u6570\u3002\u786c\u4ef6\u5347\u7ea7\uff1a\u8003\u8651\u589e\u52a0\u5185\u5b58\u6216\u66f4\u6362 cpu\u3002 SQL \u67e5\u8be2\u6162\u7684\u4f18\u5316 \u95ee\u9898\uff1a\u5982\u4f55\u4f18\u5316\u8fd0\u884c\u7f13\u6162\u7684 SQL \u67e5\u8be2\uff1f \u4f18\u5316\u7b56\u7565\uff1a 1. \u786e\u5b9a\u67e5\u8be2\u74f6\u9888\uff1a\u4f7f\u7528 EXPLAIN \u6216 EXPLAIN ANALYZE \u8bed\u53e5\u786e\u5b9a\u67e5\u8be2\u74f6\u9888\uff0c\u5982\u7d22\u5f15\u7f3a\u5931\u3001\u8868\u8fde\u63a5\u4e0d\u5f53\u6216\u5b50\u67e5\u8be2\u6548\u7387\u4f4e\u4e0b\u3002 2. \u521b\u5efa\u9002\u5f53\u7684\u7d22\u5f15\uff1a\u4e3a\u7ecf\u5e38\u4f7f\u7528\u7684\u5217\u521b\u5efa\u9002\u5f53\u7684\u7d22\u5f15\uff0c\u4ee5\u52a0\u5feb\u67e5\u8be2\u901f\u5ea6\u3002\u786e\u4fdd\u7d22\u5f15\u4e0e\u67e5\u8be2\u4e2d\u7684 WHERE \u6216 JOIN \u6761\u4ef6\u4e00\u81f4\u3002 3. \u4f18\u5316\u8868\u8fde\u63a5\uff1a\u907f\u514d\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8fde\u63a5\uff0c\u800c\u5e94\u4f7f\u7528\u8f83\u5feb\u7684\u8fde\u63a5\u7c7b\u578b\uff0c\u5982 HASH \u6216 MERGE JOIN\u3002\u4f7f\u7528 ON \u6216 USING \u5b50\u53e5\u663e\u5f0f\u6307\u5b9a\u8fde\u63a5\u6761\u4ef6\u3002 4. \u91cd\u5199\u5b50\u67e5\u8be2\uff1a\u5c06\u5d4c\u5957\u5b50\u67e5\u8be2\u66ff\u6362\u4e3a\u8fde\u63a5\u6216 EXISTS\/NOT EXISTS \u6761\u4ef6\u3002\u8fd9\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u6267\u884c\u7684\u5b50\u67e5\u8be2\u6b21\u6570\u3002 5. \u4f18\u5316\u6392\u5e8f\u548c\u5206\u7ec4\uff1a\u4f7f\u7528 ORDER BY \u548c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5919","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5919"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5919\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}