{"id":241,"date":"2024-12-24T21:32:12","date_gmt":"2024-12-24T21:32:12","guid":{"rendered":"https:\/\/www.isvmspace.com\/?p=241"},"modified":"2024-12-24T21:32:12","modified_gmt":"2024-12-24T21:32:12","slug":"mysql-%e8%a1%a8%e5%88%86%e5%8c%ba","status":"publish","type":"post","link":"https:\/\/www.isvmspace.com\/?p=241","title":{"rendered":"Mysql-\u8868\u5206\u533a"},"content":{"rendered":"<h1>\u4ec0\u4e48\u662fMysql\u8868\u5206\u533a<\/h1>\n<p>\u5c06\u4e00\u4e2a\u8868\u6216\u8005\u7d22\u5f15\u5206\u89e3\u4e3a\u591a\u4e2a\u66f4\u5c0f\u3001\u66f4\u53ef\u7ba1\u7406\u7684\u90e8\u5206\u3002<br \/>\n\u4ece\u903b\u8f91\u4e0a\u5c06\uff0c\u53ea\u6709\u4e00\u4e2a\u8868\u6216\u8005\u7d22\u5f15\uff0c\u4f46\u662f\u5728\u7269\u7406\u4e0a\u8fd9\u4e2a\u8868\u6216\u7d22\u5f15\u53ef\u80fd\u7531\u6570\u5341\u4e2a\u7269\u7406\u5206\u533a\u7ec4\u6210\u3002<br \/>\n\u6bcf\u4e2a\u5206\u533a\u90fd\u662f\u72ec\u7acb\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u72ec\u81ea\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u66f4\u5927\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h1>\u8868\u5206\u533a\u7684\u4f18\u70b9<\/h1>\n<p>1) \u5206\u533a\u8868\u5bf9\u4e1a\u52a1\u900f\u660e\uff0c\u53ea\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a\u8868\u7684\u6570\u636e\u7ed3\u6784<\/p>\n<p>2) \u51b7\u70ed\u5206\u79bb\uff1a\u8868\u975e\u5e38\u5927\u4e14\u53ea\u5728\u8868\u7684\u6700\u540e\u90e8\u5206\u6709\u70ed\u70b9\u6570\u636e\uff0c\u51b7\u6570\u636e\u6839\u636e\u5206\u533a\u89c4\u5219\u81ea\u52a8\u5f52\u6863\u3002<\/p>\n<p>3) \u5b9a\u671f\u6dd8\u6c70\u5386\u53f2\u6570\u636e\uff1a\u6309\u65f6\u95f4\u5199\u5165\uff0c\u5386\u53f2\u6570\u636e\u53ef\u6dd8\u6c70\uff0c\u53ef\u5feb\u901f\u5220\u9664\uff0c\u7a7a\u95f4\u53ef\u5feb\u901f\u56de\u6536\u3002<\/p>\n<ul>\n<li>\u53ef\u901a\u8fc7TRUNCATE\u64cd\u4f5c\u5feb\u901f\u6e05\u7406\u7279\u5b9a\u5206\u533a\u6570\u636e<\/li>\n<\/ul>\n<p>4) \u4f18\u5316\u67e5\u8be2\uff1a\u5728where\u5b57\u53e5\u4e2d\u5305\u542b\u5206\u533a\u5217\u65f6\uff0c\u5206\u533a\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\uff0c\u51cf\u5c11\u7f13\u5b58\u5f00\u9500\u3001\u51cf\u5c11IO\u5f00\u9500\u3002<\/p>\n<p>5) \u7edf\u8ba1\u6027\u80fd\u63d0\u5347\uff1a\u5728\u6d89\u53casum()\u548ccount()\u8fd9\u7c7b\u805a\u5408\u51fd\u6570\u7684\u67e5\u8be2\u65f6\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5206\u533a\u4e0a\u9762\u5e76\u884c\u5904\u7406\uff0c\u6700\u7ec8\u53ea\u9700\u8981\u6c47\u603b\u6240\u6709\u5206\u533a\u5f97\u5230\u7684\u7ed3\u679c<\/p>\n<h1>\u8868\u5206\u533a\u7684\u7f3a\u70b9<\/h1>\n<p>1) \u8868\u5206\u533a\u7684\u6570\u91cf\u6709\u9650\uff08mysql5.6\u4e4b\u540e\u652f\u63018192\u4e2a\u5206\u533a\uff0c\u4e4b\u524d\u6700\u591a1024\u4e2a\u5206\u533a\uff09<\/p>\n<p>2) \u5982\u679c\u5206\u533a\u5b57\u6bb5\u4e2d\u6709\u4e3b\u952e\u6216\u8005\u552f\u4e00\u7d22\u5f15\u5217\uff0c\u90a3\u4e48\u6240\u6709\u4e3b\u952e\u5217\u548c\u552f\u4e00\u7d22\u5f15\u5217\u90fd\u5fc5\u987b\u5305\u542b\u8fdb\u6765\uff0c\u5982\u679c\u8868\u4e2d\u6709\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\uff0c\u90a3\u4e48\u5206\u533a\u952e\u5fc5\u987b\u662f\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15<\/p>\n<p>3) \u5206\u533a\u8868\u4e2d\u65e0\u6cd5\u4f7f\u7528\u5916\u952e\u7ea6\u675f<\/p>\n<p>4) mysql\u6570\u636e\u5e93\u652f\u6301\u7684\u5206\u533a\u7c7b\u578b\u4e3a\u6c34\u5e73\u5206\u533a\uff0c\u5e76\u4e0d\u652f\u6301\u5782\u76f4\u5206\u533a<\/p>\n<p>5) DDL\u64cd\u4f5c\u9700\u8981\u9501\u5b9a\u6240\u6709\u5206\u533a\uff0c\u5bfc\u81f4\u6240\u6709\u5206\u533a\u4e0a\u64cd\u4f5c\u90fd\u88ab\u963b\u585e<\/p>\n<h1>\u8868\u5206\u533a\u7684\u89c4\u5219<\/h1>\n<h2>RANGE\u5206\u533a<\/h2>\n<p><strong>RANGE\u5206\u533a\u7279\u70b9<\/strong><br \/>\n1\uff09\u6839\u636e\u5206\u533a\u952e\u503c\u7684\u8303\u56f4\u628a\u6570\u636e\u884c\u5b58\u50a8\u5230\u8868\u7684\u4e0d\u540c\u5206\u533a\u4e2d<br \/>\n2\uff09\u591a\u4e2a\u5206\u533a\u7684\u8303\u56f4\u8981\u8fde\u7eed\uff0c\u4f46\u662f\u4e0d\u80fd\u91cd\u53e0<br \/>\n3\uff09\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f7f\u7528VALUES LESS THAN\u5c5e\u6027\uff0c\u5373\u6bcf\u4e2a\u5206\u533a\u4e0d\u5305\u62ec\u6307\u5b9a\u7684\u90a3\u4e2a\u503c<br \/>\n<strong>RANGE\u5206\u533a\u7684\u9002\u7528\u573a\u666f<\/strong><br \/>\n1\uff09\u5206\u533a\u952e\u4e3a\u65e5\u671f\u6216\u662f\u65f6\u95f4\u7c7b\u578b<br \/>\n2\uff09\u6240\u6709\u67e5\u8be2\u4e2d\u90fd\u5305\u62ec\u5206\u533a\u952e\uff08\u907f\u514d\u8de8\u5206\u533a\u67e5\u8be2\uff09<br \/>\n3\uff09\u5b9a\u671f\u6309\u5206\u533a\u8303\u56f4\u6e05\u7406\u5386\u53f2\u6570\u636e<br \/>\n  alter table t_range drop partition p2018;<\/p>\n<pre><code class=\"language-sql line-numbers\">CREATE TABLE `t_range` (\n  id int(11),\n  created_at datetime\n) ENGINE=InnoDB DEFAULT CHARSET=utf8\npartition by range(year(created_at))(\n partition p2017 values less than (2018),\n partition p2018 values less than (2019),\n partition p2019 values less than (2020),\n partition p2020 values less than (2021)\n);\n\nALTER TABLE t_range reorganize partition p2019 into (\nPARTITION p2017 VALUES LESS THAN (2018),\nPARTITION p2018 VALUES LESS THAN (2019),\nPARTITION p2019 VALUES LESS THAN (2020)\n);\n\nALTER TABLE t_range reorganize partition p2020 into (\n\nPARTITION p2020 VALUES LESS THAN (2021),\nPARTITION p2021 VALUES LESS THAN (2022)\n)   \n\n<\/code><\/pre>\n<h2>LIST\u5206\u533a<\/h2>\n<p><strong>LIST\u5206\u533a\u7684\u7279\u70b9<\/strong><br \/>\n1\uff09\u6309\u5206\u533a\u952e\u53d6\u503c\u7684\u5217\u8868\u8fdb\u884c\u5206\u533a<br \/>\n2\uff09\u540c\u8303\u56f4\u5206\u533a\u4e00\u6837\uff0c\u5404\u5206\u533a\u7684\u5217\u8868\u503c\u4e0d\u80fd\u91cd\u590d<br \/>\n3\uff09\u6bcf\u4e00\u884c\u6570\u636e\u5fc5\u987b\u80fd\u627e\u5230\u5bf9\u5e94\u7684\u5206\u533a\u5217\u8868\uff0c\u5426\u5219\u6570\u636e\u63d2\u5165\u5931\u8d25<\/p>\n<pre><code class=\"language-sql line-numbers\">CREATE TABLE `t_list` (\n id int(11),\n age int(11)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8\npartition by list (b) (\n partition p0 values in (1,3,5,7,9),\n partition p1 values in (2,4,6,8,0)\n);\n\nalter table t_list add partition(partition p1 values in(0));\n\n<\/code><\/pre>\n<h2>HASH\u5206\u533a<\/h2>\n<p><strong>HASH\u5206\u533a\u7684\u7279\u70b9\uff1a<\/strong><br \/>\n1\uff09\u6839\u636eMOD(\u5206\u533a\u952e\uff0c\u5206\u533a\u6570)\u7684\u503c\u628a\u6570\u636e\u884c\u5b58\u50a8\u5230\u8868\u7684\u4e0d\u540c\u5206\u533a\u4e2d<br \/>\n2\uff09\u6570\u636e\u53ef\u4ee5\u5e73\u5747\u7684\u5206\u5e03\u5728\u5404\u4e2a\u5206\u533a\u4e2d<br \/>\n3\uff09HASH\u5206\u533a\u7684\u952e\u503c\u5fc5\u987b\u662f\u4e00\u4e2aINT\u7c7b\u578b\u7684\u503c\uff0c\u6216\u662f\u901a\u8fc7\u51fd\u6570\u53ef\u4ee5\u8f6c\u4e3aINT\u7c7b\u578b<\/p>\n<pre><code class=\"language-sql line-numbers\">CREATE TABLE `t_hash` (\n id bigint(20),\n uuid VARCHAR(64)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8\npartition by hash (id) partitions 4;\n<\/code><\/pre>\n<h2>KEY\u5206\u533a<\/h2>\n<p>key\u5206\u533a\u548chash\u5206\u533a\u76f8\u4f3c\uff0c\u4e0d\u540c\u5728\u4e8ehash\u5206\u533a\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u5206\u533a\uff0ckey\u5206\u533a\u4f7f\u7528mysql\u6570\u636e\u5e93\u63d0\u4f9b\u7684\u51fd\u6570\u8fdb\u884c\u5206\u533a\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><br \/>\n\u5982\u679c\u8bbe\u7f6e40\uff0c64\uff0c128\u7b49\u5076\u6570\u4e2a\u5206\u533a\u6570\uff08PARTITIONS 64\uff09\uff0c\u4f1a\u5bfc\u81f4\u7f16\u53f7\u4e3a\u5947\u6570\u7684\u5206\u533a\uff08p1, p3, p5, p7, ... p2n-1\uff09\u5b8c\u5168\u63d2\u4e0d\u8fdb\u6570\u636e\uff1b<br \/>\n\u5982\u679c\u8bbe\u7f6e63\uff0c121\uff08PARTITIONS 63\uff09\u8fd9\u79cd\u5947\u6570\u4f46\u975e\u8d28\u6570\u4e2a\u5206\u533a\u6570\uff0c\u6240\u6709\u5206\u533a\u90fd\u4f1a\u6709\u6570\u636e\uff0c\u4f46\u662f\u4e0d\u5747\u5300\uff1b<br \/>\n\u5982\u679c\u8bbe\u7f6e137\uff0c31\u8fd9\u79cd\u8d28\u6570\u4e2a\u5206\u533a\u6570\uff08PARTITIONS 137\uff09\uff0c\u6240\u6709\u5206\u533a\u90fd\u4f1a\u6709\u6570\u636e\uff0c\u5e76\u4e14\u975e\u5e38\u5747\u5300\uff1b<\/p>\n<pre><code class=\"language-sql line-numbers\">CREATE TABLE `t_key` (\nuuid VARCHAR(64)\n)  ENGINE=InnoDB DEFAULT CHARSET=utf8\npartition by KEY(uuid) partitions 7;\n<\/code><\/pre>\n<h1>\u5408\u7406\u9009\u62e9\u5206\u533a\u952e<\/h1>\n<p>1)\u4f18\u5148\u4e3b\u952e<\/p>\n<p>2)\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4<\/p>\n<p>3)\u5b9a\u4e49\u5206\u533a\u6570\u91cf<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fMysql\u8868\u5206\u533a \u5c06\u4e00\u4e2a\u8868\u6216\u8005\u7d22\u5f15\u5206\u89e3\u4e3a\u591a\u4e2a\u66f4\u5c0f\u3001\u66f4\u53ef\u7ba1\u7406\u7684\u90e8\u5206\u3002 \u4ece\u903b\u8f91\u4e0a\u5c06\uff0c\u53ea\u6709\u4e00\u4e2a\u8868\u6216\u8005\u7d22\u5f15\uff0c\u4f46\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=\/wp\/v2\/posts\/241"}],"collection":[{"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=241"}],"version-history":[{"count":4,"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=\/wp\/v2\/posts\/241\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.isvmspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}