{"id":80,"date":"2024-05-17T17:08:31","date_gmt":"2024-05-17T09:08:31","guid":{"rendered":"https:\/\/www.sretalk.com\/?p=80"},"modified":"2024-05-20T10:04:40","modified_gmt":"2024-05-20T02:04:40","slug":"%e8%ae%b0%e4%b8%80%e6%ac%a1-mysql-%e5%af%bc%e5%85%a5%e5%af%bc%e5%87%ba%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.sretalk.com\/?p=80","title":{"rendered":"\u8bb0\u4e00\u6b21 MySQL \u5bfc\u5165\u5bfc\u51fa\u7684\u95ee\u9898"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00 \u6982\u8ff0<\/h2>\n\n\n\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\u5728\u505a MySQL \u7684\u5907\u4efd\u8fd8\u539f\u65f6\uff0c\u90fd\u4e60\u60ef\u6027\u7684\u4f7f\u7528 <code>mysqldump<\/code> \u548c <code>mysql &lt; backup.sql<\/code> \u7684\u65b9\u5f0f\u8fdb\u884c\u3002\u6700\u8fd1\u521a\u597d\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u505a\u6570\u636e\u5e93\u7684\u8fc1\u79fb\u65f6\uff0c\u628a\u5bfc\u51fa\u6765\u7684\u6570\u636e\u5bfc\u5230\u65b0\u7684\u5b9e\u4f8b\u4e0a\u65f6\uff0c\u53d1\u73b0\u65b0\u65e7\u4e24\u4e2a\u5e93\u6240\u5360\u7528\u7684\u78c1\u76d8\u7a7a\u95f4\u5927\u5c0f\u4e0d\u4e00\u6837\uff0c\u65b0\u8fc1\u79fb\u7684\u5e93\u6bd4\u539f\u6709\u7684\u5e93\u5360\u7528\u78c1\u76d8\u7a7a\u95f4\u66f4\u5927\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c \u5177\u4f53\u95ee\u9898<\/h2>\n\n\n\n<p>\u5176\u5b9e\u6839\u636e\u7ecf\u9a8c\u7684\u7b2c\u4e00\u53cd\u5e94\uff0c\u5c31\u5e94\u8be5\u662f\u4ea7\u751f\u4e86\u8868\u7a7a\u95f4\u7684\u788e\u7247\uff0c\u9700\u8981\u8fdb\u884c <code>optimize table<\/code> \u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u4e3a\u4ec0\u4e48\u4f1a\u6709\u788e\u7247\uff1f<\/h3>\n\n\n\n<p>\u7ecf\u8fc7\u67e5\u9605\u8d44\u6599\uff0c\u5728\u4f7f\u7528 InnoDB \u4f5c\u4e3a\u5b58\u50a8\u5f15\u64ce\u65f6\uff0c\u5b83\u7684\u6700\u5c0f\u7684\u7269\u7406\u5b58\u50a8\u5355\u5143\u662f <strong>\u9875(page)<\/strong>\uff0c\u4e5f\u5c31\u662f\u8bf4\u6570\u636e\u5e93\u6bcf\u6b21\u8fdb\u884c\u8bfb\u5199\u65f6\u90fd\u662f\u4ee5 <strong>\u9875<\/strong> \u4e3a\u5355\u4f4d\u8fdb\u884c\u64cd\u4f5c\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6bcf\u4e00\u9875\u662f <strong>16KB(16384)<\/strong>\uff0c\u4e5f\u5c31\u662f\u6700\u591a\u53ef\u4ee5\u8fde\u7eed\u4f7f\u7528\u7684\u5b58\u50a8\u7a7a\u95f4\u662f 16KB\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code language-sql\"><code>SHOW VARIABLES LIKE 'innodb_page_size';\n\n-- \u8fd4\u56de\uff1a\n-- +------------------+-------+\n-- | Variable_name    | Value |\n-- +------------------+-------+\n-- | innodb_page_size | 16384 |\n-- +------------------+-------+<\/code><\/pre>\n\n\n\n<p>\u56e0\u6b64\u5f53\u6211\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u7a7a\u8868\u65f6\uff0c\u5b83\u7684\u5bb9\u91cf\u4e5f\u662f 16KB:<\/p>\n\n\n\n<pre class=\"wp-block-code language-sql\"><code>-- 1. \u521b\u5efa\u8868\nCREATE TABLE t1(id int, name varchar(10));\n\n-- 2. \u67e5\u770b\u8868\u7a7a\u95f4\u5927\u5c0f\nSELECT\n  table_schema AS '\u6570\u636e\u5e93',\n  table_name AS '\u8868\u540d',\n  table_rows AS '\u8bb0\u5f55\u6570',\n  data_length as '\u6570\u636e\u5bb9\u91cf(Bytes)',\n  index_length '\u7d22\u5f15\u5bb9\u91cf(Bytes)'\nFROM\n  information_schema.tables\nWHERE\n  table_schema='sretalk' \/* \u6570\u636e\u5e93\u540d *\/\nORDER BY\n  table_rows DESC,\n  index_length DESC;\n\n-- \u8fd4\u56de:\n-- +-----------+--------+-----------+---------------------+---------------------+\n-- | \u6570\u636e\u5e93    | \u8868\u540d   | \u8bb0\u5f55\u6570    | \u6570\u636e\u5bb9\u91cf(Bytes)     | \u7d22\u5f15\u5bb9\u91cf(Bytes)     |\n-- +-----------+--------+-----------+---------------------+---------------------+\n-- | sretalk   | t1     |         0 |               16384 |                   0 |\n-- +-----------+--------+-----------+---------------------+---------------------+<\/code><\/pre>\n\n\n\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\u8fdb\u884c\u5927\u91cf\u7684 INSERT\u3001UPDATE \u6709\u53ef\u80fd\u5bfc\u81f4 <strong>\u9875\u5206\u88c2<\/strong> \u4ece\u800c\u4ea7\u751f\u788e\u7247<em>\uff08\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u4e00\u4e2a\u9875\u6ee1\u4e86\u4f46\u8fd8\u9700\u8981\u63d2\u5165\u65b0\u884c\u65f6\uff0cMySQL \u53ef\u80fd\u4f1a\u5bf9\u9875\u8fdb\u884c\u5206\u88c2\u6210\u4e24\u4e2a\uff0c\u5e76\u5c06\u65b0\u884c\u63d2\u5165\u5230\u5176\u4e2d\u4e00\u9875\uff09<\/em>\u3002<\/p>\n\n\n\n<p>\u800c DELETE \u5c31\u66f4\u597d\u7406\u89e3\uff0c\u56e0\u4e3a DELETE \u8bed\u53e5\u5b9e\u9645\u4e0a\u662f\u7ed9\u8be5\u6570\u636e\u6253\u4e86\u4e2a <strong>\u5df2\u5220\u9664<\/strong> \u7684\u6807\u7b7e\uff0c\u5e76\u4e0d\u662f\u8fdb\u884c\u7269\u7406\u6027\u7684\u5220\u9664\uff0c\u56e0\u6b64\u5c31\u8fd9\u90e8\u5206\u7684\u78c1\u76d8\u7a7a\u95f4\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002\u5e76\u4e14\u5982\u679c\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u4e00\u76f4\u6ca1\u6709\u627e\u5230\u5408\u9002\u5927\u5c0f\u7684\u6570\u636e\u8fdb\u884c\u586b\u5145\u590d\u5199\uff0c\u5c31\u4f1a\u5f62\u6210\u4e00\u4e2a\u8868\u7a7a\u95f4\u7684\u7a7a\u6d1e\uff0c\u8fd9\u90e8\u5206\u4e5f\u662f\u788e\u7247\u7684\u4e00\u79cd\u3002<\/p>\n\n\n\n<p>\u800c\u4f7f\u7528 mysqldump \u7684\u65b9\u5f0f\u8fdb\u884c\u5bfc\u51fa\u7684\u6570\u636e\uff0c\u57fa\u672c\u90fd\u662f\u4ee5 <code>INSERT INTO xxxx<\/code> \u7684\u8bed\u53e5\u8fdb\u884c\u8bb0\u5f55\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u5bfc\u51fa\u7684\u6570\u636e\u91cf\u5927\u65f6\uff0c\u5728\u5bfc\u5165\u5230\u65b0\u5e93\u65f6\u4e5f\u7b49\u4e8e\u5728\u8fdb\u884c\u4e00\u6b21\u5927\u6279\u91cf\u7684\u63d2\u5165\u52a8\u4f5c\uff0c\u4e5f\u5c31\u6709\u53ef\u80fd\u4ea7\u751f\u4e00\u5b9a\u7684\u8868\u7a7a\u95f4\u788e\u7247\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code language-sql\"><code>-- \u67e5\u770b\u8868\u72b6\u6001\nSHOW TABLE STATUS LIKE '%t1%'\\G;\n\n-- *************************** 1. row ***************************\n--            Name: t1\n--          Engine: InnoDB\n--         Version: 10\n--      Row_format: Dynamic\n--            Rows: 30360\n--  Avg_row_length: 52\n--     Data_length: 1589248\n-- Max_data_length: 0\n--    Index_length: 0\n--       Data_free: 4194304\n--  Auto_increment: NULL\n--     Create_time: 2024-05-17 16:38:47\n--     Update_time: 2024-05-17 16:38:52\n--      Check_time: NULL\n--       Collation: utf8_general_ci\n--        Checksum: NULL\n--  Create_options:\n--         Comment:\n-- 1 row in set (0.00 sec)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u540c\u65f6\u4e5f\u4f7f\u7528\u4ee5\u4e0b SQL \u4e00\u5e76\u68c0\u67e5\u5f53\u524d MySQL \u5b9e\u4f8b\u7684\u6240\u6709\u5df2\u7ecf\u4ea7\u751f\u788e\u7247\u7684\u8868\uff0c\u4ee5\u53ca\u788e\u7247\u7387\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n  table_schema AS '\u6570\u636e\u5e93\u540d', \n  table_name AS '\u8868\u540d', \n  engine AS '\u6570\u636e\u5e93\u5f15\u64ce',\n  table_rows AS '\u5f53\u524d\u884c', \n  ROUND(data_free\/1024\/1024, 2) AS '\u603b\u788e\u7247\u7a7a\u95f4\u5927\u5c0f(MB)', \n  ROUND((data_length+index_length)\/1024\/1024,2) '\u8868\u5927\u5c0f(MB)', \n  ROUND(data_free\/(data_free + data_length+index_length),2) AS '\u788e\u7247\u7387'\nFROM\n  information_schema.tables   \nWHERE\n  table_schema not in ('information_schema', 'mysql') \nAND\n  data_free > 0 \nORDER BY \n  '\u603b\u788e\u7247\u7a7a\u95f4\u5927\u5c0f(MB)' DESC,\n  '\u788e\u7247\u7387' DESC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u5982\u4f55\u5904\u7406\uff1f<\/h3>\n\n\n\n<p>\u53ea\u6709\u4e00\u79cd\u5904\u7406\u65b9\u5f0f\uff0c\u5c31\u662f\u6e05\u7406\u8868\u7a7a\u95f4\u7684\u788e\u7247\uff0c\u4f46\u4e0d\u540c\u7684\u6570\u636e\u5e93\u5f15\u64ce\u6240\u63a8\u8350\u7684\u6e05\u7406\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- MyISAM:\nOPTIMIZE TABLE \u8868\u540d;\n\n-- InnoDB:\nALTER TABLE \u8868\u540d ENGINE=InnoDB;<\/code><\/pre>\n\n\n\n<p>\u65e0\u8bba\u54ea\u79cd\u64cd\u4f5c\uff0c\u5728\u6e05\u7406\u788e\u7247\u65f6\u90fd\u4f1a\u8fdb\u884c\u9501\u8868\u3002\u5e76\u4e14\u6570\u636e\u91cf\u8d8a\u5927\uff0c\u6e05\u7406\u7684\u65f6\u95f4\u4e5f\u8d8a\u957f\uff0c\u9501\u8868\u7684\u65f6\u95f4\u4e5f\u8d8a\u957f\u3002\u5728\u751f\u4ea7\u73af\u5883\u4e0a\u9700\u8981\u6ce8\u610f\u4e0d\u8981\u5728\u5de5\u4f5c\u65f6\u95f4\u8fdb\u884c\u8868\u7a7a\u95f4\u7684\u6e05\u7406\uff0c\u5e76\u4e14\u6e05\u7406\u5468\u671f\u4e0d\u9700\u8981\u592a\u9891\u7e41\uff0c\u4e00\u822c\u4ee5\u5468\u6216\u8005\u6708\u4e3a\u5355\u4f4d\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u540c\u65f6\uff0c\u5982\u679c\u6570\u636e\u5e93\u6709\u505a\u4e3b\u4ece\u7b49\u67b6\u6784\u7684\uff0c\u5728\u6267\u884c\u8868\u7a7a\u95f4\u6e05\u7406\u65f6\u4f1a\u5f71\u54cd\u4e3b\u4ece\u7684\u540c\u6b65\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u8fdb\u884c\u6e05\u7406\u524d\u5148\u7981\u6b62\u8bb0\u5f55 binlog\uff0c\u518d\u5206\u522b\u5728\u4e3b\u4ece\u4e24\u8fb9\u8fdb\u884c\u6e05\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code language-sql\"><code>-- \u7981\u6b62\u8bb0\u5f55\nSET sql_bin_log = OFF;\n\n-- \u6e05\u7406\u788e\u7247\nALTER TABLE \u8868\u540d ENGINE=InnoDB;\n\n-- \u5f00\u542f\u8bb0\u5f55\nSET sql_bin_log = ON;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><\/p>\n<cite>Notes: <br>\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8be5\u52a8\u4f5c\u53ea\u662f\u91cd\u65b0\u8fdb\u884c\u6574\u7406\u788e\u7247\uff0c\u4f46\u662f\u4e0d\u6392\u9664\u56e0\u4e3a\u6570\u636e\u5927\u5c0f\u4e0d\u4e00\u7684\u95ee\u9898\uff0c\u5728\u6574\u7406\u5b8c\u4e4b\u540e\u4f9d\u7136\u53ef\u80fd\u5b58\u5728\u7a7a\u6d1e\uff0c\u53ea\u80fd\u6e05\u7406\u4e86\u4e00\u90e8\u5206\u7684 <code>Data_free<\/code>\u3002<\/cite><\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00 \u6982\u8ff0 \u4e00\u822c\u60c5\u51b5\u4e0b\u5728\u505a MySQL \u7684\u5907\u4efd\u8fd8\u539f\u65f6\uff0c\u90fd\u4e60\u60ef\u6027\u7684\u4f7f\u7528 mysqldump \u548c mysql &#038;lt [&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,5,1],"tags":[],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-mysql","category-5","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.sretalk.com\/index.php?rest_route=\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sretalk.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sretalk.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sretalk.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sretalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=80"}],"version-history":[{"count":6,"href":"https:\/\/www.sretalk.com\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/www.sretalk.com\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions\/86"}],"wp:attachment":[{"href":"https:\/\/www.sretalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sretalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sretalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}