{"id":20585,"date":"2026-07-13T17:11:44","date_gmt":"2026-07-13T09:11:44","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=20585"},"modified":"2026-07-13T17:59:33","modified_gmt":"2026-07-13T09:59:33","slug":"mysql-11","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2026\/07\/13\/mysql-11\/","title":{"rendered":"MySQL \u6309\u65f6\u95f4\u5206\u6279\u51b7\u6570\u636e\u5f52\u6863"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e1a\u52a1\u9700\u6c42<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e3b\u8868\uff1a<code>operation_log<\/code>\uff0c\u65f6\u95f4\u5b57\u6bb5 <code>create_time DATETIME<\/code><\/li>\n\n\n\n<li>\u5f52\u6863\u89c4\u5219\uff1a<strong>\u81ea\u52a8\u8fc1\u79fb 1 \u4e2a\u6708\u524d\u5168\u90e8\u6570\u636e\u5230\u5f52\u6863\u8868 <code>operation_log_archive<\/code><\/strong>\uff0c\u8fc1\u79fb\u5b8c\u6210\u540e\u539f\u8868\u5220\u9664\u5bf9\u5e94\u6570\u636e<\/li>\n\n\n\n<li>\u6838\u5fc3\u4f18\u52bf\uff1a<strong>\u5c0f\u6279\u91cf\u5faa\u73af + \u77ed\u4e8b\u52a1 + \u6279\u6b21\u4f11\u7720<\/strong>\uff0c\u5de5\u4f5c\u65f6\u95f4\u6267\u884c\u4ec5\u8f7b\u5fae\u77ac\u65f6\u538b\u529b\uff0c\u4e0d\u4f1a\u9501\u8868\u963b\u585e\u7ebf\u4e0a\u4e1a\u52a1<\/li>\n\n\n\n<li>\u9002\u7528\u5f15\u64ce\uff1aInnoDB\uff08\u7ebf\u4e0a\u6807\u51c6\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5f52\u6863<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u4e3b\u8868 &amp; \u5f52\u6863\u8868\u7ed3\u6784<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u4e1a\u52a1\u4e3b\u8868\r\nCREATE TABLE `operation_log` (\r\n  id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '\u81ea\u589e\u4e3b\u952e',\r\n  user_id INT NOT NULL,\r\n  operate_type TINYINT,\r\n  content VARCHAR(1000),\r\n  create_time DATETIME NOT NULL COMMENT '\u5f52\u6863\u65f6\u95f4\u7b5b\u9009\u5b57\u6bb5'\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '\u4e1a\u52a1\u64cd\u4f5c\u65e5\u5fd7\u4e3b\u8868';\r\n\r\n-- \u5fc5\u987b\u7ed9\u65f6\u95f4\u5b57\u6bb5\u52a0\u7d22\u5f15\uff08\u5f52\u6863\u6027\u80fd\u6838\u5fc3\uff0c\u65e0\u7d22\u5f15\u7981\u6b62\u5f52\u6863\uff09\r\nALTER TABLE operation_log ADD INDEX idx_create_time(create_time);\r\n\r\n-- \u521b\u5efa\u5f52\u6863\u8868\uff0c\u5b8c\u5168\u590d\u5236\u4e3b\u8868\u7ed3\u6784\r\nCREATE TABLE `operation_log_archive` LIKE `operation_log`;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u524d\u7f6e\u6821\u9a8c SQL\uff08\u6267\u884c\u5f52\u6863\u524d\u5fc5\u8dd1\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- 1. \u7edf\u8ba1\u5f85\u5f52\u6863\u603b\u6570\u636e\u91cf\uff0c\u9884\u4f30IO\u538b\u529b\r\nSELECT COUNT(*) AS total_archive_rows \r\nFROM operation_log \r\nWHERE create_time &lt; DATE_SUB(NOW(), INTERVAL 1 MONTH);\r\n\r\n-- 2. \u6821\u9a8c\u7d22\u5f15\u662f\u5426\u751f\u6548\uff0ctype=range \u4ee3\u8868\u7d22\u5f15\u6b63\u5e38\uff1btype=ALL \u5168\u8868\u626b\u63cf\u7981\u6b62\u5f52\u6863\r\nEXPLAIN \r\nSELECT * FROM operation_log \r\nWHERE create_time &lt; DATE_SUB(NOW(), INTERVAL 1 MONTH) LIMIT 10000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u6570\u636e\u5e93\u8d26\u53f7\u9700\u8981\u6743\u9650\uff1a<code>SELECT, INSERT, DELETE, LOCK TABLES<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5f52\u6863\u4e13\u7528\u8d26\u53f7\u6388\u6743\u793a\u4f8b\r\nCREATE USER 'archive_user'@'localhost' IDENTIFIED BY 'Archive@123456';\r\nGRANT SELECT,INSERT,DELETE ON \u4e1a\u52a1\u5e93\u540d.* TO 'archive_user'@'localhost';\r\nFLUSH PRIVILEGES;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u5b8c\u6574\u5206\u6279\u5f52\u6863 Shell \u811a\u672c\uff08\u751f\u4ea7\u53ef\u7528\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\r\n##########################################################################\r\n# MySQL \u5206\u6279\u51b7\u6570\u636e\u5f52\u6863\u811a\u672c\r\n# \u529f\u80fd\uff1a\u8fc1\u79fb1\u4e2a\u6708\u524d\u6570\u636e\u5230\u5f52\u6863\u8868\uff0c\u5206\u6279\u5faa\u73af\uff0c\u81ea\u5e26\u4f11\u7720\u9650\u6d41\uff0c\u4e0d\u963b\u585e\u7ebf\u4e0a\u4e1a\u52a1\r\n##########################################################################\r\n# ====================== \u914d\u7f6e\u533a\uff08\u6309\u9700\u4fee\u6539\uff09======================\r\nDB_HOST=\"127.0.0.1\"\r\nDB_PORT=\"3306\"\r\nDB_USER=\"archive_user\"\r\nDB_PWD=\"Archive@123456\"\r\nDB_NAME=\"\u4f60\u7684\u4e1a\u52a1\u6570\u636e\u5e93\u540d\"\r\nMAIN_TABLE=\"operation_log\"       # \u4e1a\u52a1\u4e3b\u8868\r\nARCH_TABLE=\"operation_log_archive\" # \u5f52\u6863\u8868\r\nBATCH_SIZE=5000                  # \u6bcf\u6279\u5904\u7406\u884c\u6570\uff0c\u767d\u5929\u5efa\u8bae5000\uff0c\u51cc\u6668\u53ef10000\r\nSLEEP_SECONDS=1                  # \u6bcf\u6279\u5904\u7406\u5b8c\u6210\u4f11\u7720\u65f6\u957f\uff0c\u9650\u6d41\u7528\r\nRETENTION_MONTH=1                # \u4fdd\u7559\u8fd11\u4e2a\u6708\u6570\u636e\uff0c1\u4e2a\u6708\u524d\u5f52\u6863\r\nLOG_FILE=\"\/var\/log\/mysql_archive.log\" # \u5f52\u6863\u65e5\u5fd7\u8def\u5f84\r\n# ==============================================================\r\n\r\n# \u65e5\u5fd7\u6253\u5370\u51fd\u6570\r\nlog_print() {\r\n    echo \"&#91;$(date +'%Y-%m-%d %H:%M:%S')] $1\" >> $LOG_FILE\r\n    echo \"&#91;$(date +'%Y-%m-%d %H:%M:%S')] $1\"\r\n}\r\n\r\n# \u521d\u59cb\u5316\u65e5\u5fd7\r\nlog_print \"===== \u5f52\u6863\u4efb\u52a1\u5f00\u59cb =====\"\r\n\r\n# \u5faa\u73af\u5206\u6279\u8fc1\u79fb\uff0c\u76f4\u5230\u65e0\u65e7\u6570\u636e\r\nloop_count=0\r\nwhile true; do\r\n    loop_count=$((loop_count + 1))\r\n    # \u6267\u884c\u5355\u6279\u6b21\u4e8b\u52a1\uff1a\u5148\u590d\u5236\uff0c\u518d\u5220\u9664\uff0c\u539f\u5b50\u64cd\u4f5c\r\n    mysql -h$DB_HOST -P$DB_PORT -u$DB_USER -p$DB_PWD $DB_NAME &lt;&lt; EOF\r\nSTART TRANSACTION;\r\n-- \u590d\u5236\u4e00\u6279\u65e7\u6570\u636e\u5230\u5f52\u6863\u8868\r\nINSERT INTO $ARCH_TABLE\r\nSELECT * FROM $MAIN_TABLE \r\nWHERE create_time &lt; DATE_SUB(NOW(), INTERVAL $RETENTION_MONTH MONTH)\r\nLIMIT $BATCH_SIZE;\r\n\r\n-- \u5220\u9664\u539f\u8868\u5df2\u590d\u5236\u7684\u6570\u636e\r\nDELETE FROM $MAIN_TABLE \r\nWHERE create_time &lt; DATE_SUB(NOW(), INTERVAL $RETENTION_MONTH MONTH)\r\nLIMIT $BATCH_SIZE;\r\nCOMMIT;\r\nEOF\r\n\r\n    # \u83b7\u53d6\u672c\u6b21\u64cd\u4f5c\u5f71\u54cd\u884c\u6570\r\n    affected_rows=$(mysql -s -h$DB_HOST -P$DB_PORT -u$DB_USER -p$DB_PWD $DB_NAME -e \"SELECT ROW_COUNT();\")\r\n\r\n    # \u5f71\u54cd\u884c\u6570\u4e3a0\uff0c\u4ee3\u8868\u65e0\u6570\u636e\u53ef\u5f52\u6863\uff0c\u9000\u51fa\u5faa\u73af\r\n    if &#91; $affected_rows -eq 0 ]; then\r\n        log_print \"\u7b2c${loop_count}\u6279\uff1a\u65e0\u5f85\u5f52\u6863\u6570\u636e\uff0c\u5f52\u6863\u6d41\u7a0b\u7ed3\u675f\"\r\n        break\r\n    fi\r\n\r\n    log_print \"\u7b2c${loop_count}\u6279\uff1a\u6210\u529f\u5904\u7406${affected_rows}\u6761\u6570\u636e\uff0c\u4f11\u7720${SLEEP_SECONDS}\u79d2\u91ca\u653e\u8d44\u6e90\"\r\n    sleep $SLEEP_SECONDS\r\ndone\r\n\r\nlog_print \"===== \u5f52\u6863\u4efb\u52a1\u5168\u90e8\u5b8c\u6210 =====\"\r\necho -e \"\\n\" >> $LOG_FILE<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u6267\u884c\u65f6\u6bb5<\/th><th>BATCH_SIZE<\/th><th>SLEEP_SECONDS<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td>\u51cc\u6668\u4f4e\u5cf0<\/td><td>10000<\/td><td>0.5<\/td><td>\u65e0\u4e1a\u52a1\u6d41\u91cf\uff0c\u5168\u901f\u5f52\u6863<\/td><\/tr><tr><td>\u5348\u95f4\u4f4e\u8c37<\/td><td>5000<\/td><td>1.0<\/td><td>\u5c11\u91cf\u7528\u6237\u8bbf\u95ee<\/td><\/tr><tr><td>\u767d\u5929\u5de5\u4f5c\u65f6\u95f4<\/td><td>3000<\/td><td>1.5<\/td><td>\u6b63\u5e38\u4e1a\u52a1\u6d41\u91cf\uff0c\u6781\u81f4\u9650\u6d41<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">5\u3001\u56de\u6536 InnoDB \u78c1\u76d8\u7a7a\u95f4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">InnoDB DELETE \u4e0d\u4f1a\u81ea\u52a8\u91ca\u653e\u78c1\u76d8\uff0c\u5f52\u6863\u5b8c\u6210\u6267\u884c\u4f18\u5316\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OPTIMIZE TABLE operation_log;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u5343\u4e07\u7ea7\u5927\u8868 OPTIMIZE \u4f1a\u77ed\u6682\u9501\u8868\uff0c\u5efa\u8bae\u51cc\u6668\u6267\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">6\u3001\u5f52\u6863\u8868\u538b\u7f29\u4f18\u5316\uff08\u51b7\u6570\u636e\u53ea\u8bfb\uff0c\u8282\u7701\u5b58\u50a8\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE operation_log_archive ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u56de\u6eda<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u573a\u666f1: \u5f52\u6863\u4e2d\u9014\u4e1a\u52a1\u62a5\u9519\uff0c\u9700\u8981\u6062\u590d\u5df2\u8fc1\u79fb\u6570\u636e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u505c\u6b62\u5f52\u6863\u811a\u672c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u628a\u5f52\u6863\u8868\u6570\u636e\u5bfc\u56de\u4e3b\u8868\uff08\u6309\u65f6\u95f4\u533a\u95f4\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>START TRANSACTION;\r\nINSERT INTO operation_log\r\nSELECT * FROM operation_log_archive \r\nWHERE create_time &lt; DATE_SUB(NOW(), INTERVAL 1 MONTH);\r\nCOMMIT;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u573a\u666f2:  \u8868\u91cd\u590d\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6309\u4e3b\u952e\u53bb\u91cd\uff0c\u5220\u9664\u91cd\u590d\u884c\r\nDELETE t1 FROM operation_log_archive t1\r\nJOIN operation_log_archive t2 \r\nWHERE t1.id = t2.id AND t1.create_time &lt; DATE_SUB(NOW(), INTERVAL 1 MONTH);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e1a\u52a1\u9700\u6c42 \u5f52\u6863 1\u3001\u4e3b\u8868 &amp; \u5f52\u6863\u8868\u7ed3\u6784 2\u3001\u524d\u7f6e\u6821\u9a8c SQL\uff08\u6267\u884c\u5f52\u6863\u524d\u5fc5\u8dd1\uff09\uff1a 3\u3001\u6570\u636e\u5e93\u8d26\u53f7\u9700\u8981 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":20588,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-20585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/20585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=20585"}],"version-history":[{"count":7,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/20585\/revisions"}],"predecessor-version":[{"id":20594,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/20585\/revisions\/20594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/20588"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=20585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=20585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=20585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}