{"id":6478,"date":"2024-11-14T08:26:59","date_gmt":"2024-11-14T00:26:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6478\/"},"modified":"2024-11-14T08:26:59","modified_gmt":"2024-11-14T00:26:59","slug":"mysql%e5%88%86%e8%a1%a8%e6%80%8e%e4%b9%88%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6478\/","title":{"rendered":"mysql\u5206\u8868\u600e\u4e48\u67e5\u8be2"},"content":{"rendered":"<blockquote><p>\n  \u9488\u5bf9 mysql \u5206\u8868\u67e5\u8be2\u6570\u636e\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a1. union all \u8054\u5408\u67e5\u8be2\uff0c\u5c06\u4e0d\u540c\u5206\u8868\u4e2d\u7684\u6570\u636e\u5408\u5e76\u4e3a\u4e00\u5f20\u8868\uff1b2. hash \u5206\u7247\u67e5\u8be2\uff0c\u901a\u8fc7\u5305\u542b\u5206\u7247\u952e\u76f4\u63a5\u8bbf\u95ee\u6240\u9700\u7684\u5206\u533a\uff0c\u907f\u514d\u626b\u63cf\u6240\u6709\u5206\u533a\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/05\/2024080511140217912.jpg\" class=\"aligncenter\" title=\"mysql\u5206\u8868\u600e\u4e48\u67e5\u8be2\u63d2\u56fe\" alt=\"mysql\u5206\u8868\u600e\u4e48\u67e5\u8be2\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u5206\u8868\u67e5\u8be2\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u5982\u4f55\u67e5\u8be2\u5206\u8868\uff1f<\/strong><\/p>\n<p>\u5bf9\u4e8e MySQL \u5206\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u67e5\u8be2\u6570\u636e\uff1a<\/p>\n<p><strong>1. UNION ALL \u8054\u5408\u67e5\u8be2<\/strong><\/p>\n<p>UNION ALL \u64cd\u4f5c\u7b26\u53ef\u5c06\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u8868\uff0c\u5373\u4f7f\u5206\u8868\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e2d\uff1a<\/p>\n<pre>SELECT * FROM table1 UNION ALL SELECT * FROM table2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. HASH \u5206\u7247\u67e5\u8be2<\/strong><\/p>\n<p>\u6b64\u65b9\u6cd5\u9700\u8981\u4fee\u6539\u67e5\u8be2\u8bed\u53e5\u4ee5\u5305\u542b\u5206\u7247\u952e\uff0c\u4ece\u800c\u4ec5\u8bbf\u95ee\u5305\u542b\u6240\u9700\u6570\u636e\u7684\u7279\u5b9a\u5206\u533a\uff1a<\/p>\n<pre>SELECT * FROM table WHERE MOD(id, num_of_partitions) = partition_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<p><strong>UNION ALL \u8054\u5408\u67e5\u8be2<\/strong><\/p>\n<ul>\n<li>UNION ALL \u64cd\u4f5c\u7b26\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5177\u6709\u76f8\u540c\u7ed3\u6784\uff08\u5217\u540d\u548c\u6570\u636e\u7c7b\u578b\u76f8\u540c\uff09\u7684\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u8868\u3002<\/li>\n<li>\u7531\u4e8e\u5206\u8868\u901a\u5e38\u5177\u6709\u76f8\u540c\u7684\u6a21\u5f0f\uff0c\u56e0\u6b64 UNION ALL \u53ef\u7528\u4e8e\u67e5\u8be2\u8de8\u5206\u8868\u7684\u6570\u636e\u3002<\/li>\n<li>UNION ALL \u4e0d\u4f1a\u8fc7\u6ee4\u91cd\u590d\u884c\uff0c\u56e0\u6b64\u5982\u679c\u5206\u8868\u6709\u91cd\u53e0\u6570\u636e\uff0c\u67e5\u8be2\u7ed3\u679c\u4e2d\u4f1a\u51fa\u73b0\u91cd\u590d\u884c\u3002<\/li>\n<\/ul>\n<p><strong>HASH \u5206\u7247\u67e5\u8be2<\/strong><\/p>\n<ul>\n<li>HASH \u5206\u7247\u5c06\u6570\u636e\u6839\u636e\u5206\u7247\u952e\uff08\u901a\u5e38\u662f\u8868\u4e2d\u7684\u67d0\u4e2a\u5217\uff09\u5747\u5300\u5206\u5e03\u5230\u5206\u533a\u4e2d\u3002<\/li>\n<li>\u5206\u533a\u952e\u7528\u4e8e\u8ba1\u7b97\u6570\u636e\u7684\u76ee\u6807\u5206\u533a\u3002<\/li>\n<li>\u901a\u8fc7\u5c06\u5206\u7247\u952e\u5305\u542b\u5728\u67e5\u8be2\u6761\u4ef6\u4e2d\uff0cMySQL \u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u6240\u9700\u7684\u5206\u533a\uff0c\u4ece\u800c\u907f\u514d\u626b\u63cf\u6240\u6709\u5206\u533a\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u5c06\u8868 users \u5206\u4e3a\u4e24\u4e2a\u5206\u533a\uff0c\u5206\u533a\u952e\u4e3a user_id\uff1a<\/p>\n<ul>\n<li><strong>UNION ALL \u8054\u5408\u67e5\u8be2\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT * FROM users_partition1 UNION ALL SELECT * FROM users_partition2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li><strong>HASH \u5206\u7247\u67e5\u8be2\uff1a<\/strong><\/li>\n<\/ul>\n<pre>SELECT * FROM users WHERE MOD(user_id, 2) = 0;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u4ec5\u4ece users_partition1 \u5206\u533a\u68c0\u7d22\u6570\u636e\uff0c\u56e0\u4e3a user_id \u7684\u5947\u5076\u6821\u9a8c\u4e3a 0\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5206\u8868\u600e\u4e48\u67e5\u8be2\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>\u9488\u5bf9 mysql \u5206\u8868\u67e5\u8be2\u6570\u636e\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a1. union all \u8054\u5408\u67e5\u8be2\uff0c\u5c06\u4e0d\u540c\u5206\u8868\u4e2d\u7684\u6570\u636e\u5408\u5e76\u4e3a\u4e00\u5f20\u8868\uff1b2. hash \u5206\u7247\u67e5\u8be2\uff0c\u901a\u8fc7\u5305\u542b\u5206\u7247\u952e\u76f4\u63a5\u8bbf\u95ee\u6240\u9700\u7684\u5206\u533a\uff0c\u907f\u514d\u626b\u63cf\u6240\u6709\u5206\u533a\u3002 MySQL \u5206\u8868\u67e5\u8be2\u65b9\u6cd5 \u5982\u4f55\u67e5\u8be2\u5206\u8868\uff1f \u5bf9\u4e8e MySQL \u5206\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u67e5\u8be2\u6570\u636e\uff1a 1. UNION ALL \u8054\u5408\u67e5\u8be2 UNION ALL \u64cd\u4f5c\u7b26\u53ef\u5c06\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u8868\uff0c\u5373\u4f7f\u5206\u8868\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e2d\uff1a SELECT * FROM table1 UNION ALL SELECT * FROM table2; \u767b\u5f55\u540e\u590d\u5236 2. HASH \u5206\u7247\u67e5\u8be2 \u6b64\u65b9\u6cd5\u9700\u8981\u4fee\u6539\u67e5\u8be2\u8bed\u53e5\u4ee5\u5305\u542b\u5206\u7247\u952e\uff0c\u4ece\u800c\u4ec5\u8bbf\u95ee\u5305\u542b\u6240\u9700\u6570\u636e\u7684\u7279\u5b9a\u5206\u533a\uff1a SELECT * FROM table WHERE MOD(id, num_of_partitions) = partition_id; \u767b\u5f55\u540e\u590d\u5236 \u8be6\u7ec6\u8bf4\u660e\uff1a UNION ALL \u8054\u5408\u67e5\u8be2 UNION ALL \u64cd\u4f5c\u7b26\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5177\u6709\u76f8\u540c\u7ed3\u6784\uff08\u5217\u540d\u548c\u6570\u636e\u7c7b\u578b\u76f8\u540c\uff09\u7684\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u4e3a\u4e00\u4e2a\u8868\u3002 \u7531\u4e8e\u5206\u8868\u901a\u5e38\u5177\u6709\u76f8\u540c\u7684\u6a21\u5f0f\uff0c\u56e0\u6b64 UNION [&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-6478","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6478","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=6478"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6478\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}