{"id":718,"date":"2025-09-11T17:17:38","date_gmt":"2025-09-11T09:17:38","guid":{"rendered":"https:\/\/znl.pub\/?p=718"},"modified":"2025-09-11T17:35:10","modified_gmt":"2025-09-11T09:35:10","slug":"mysql%e6%b7%bb%e5%8a%a0%e5%a4%8d%e5%90%88%e7%b4%a2%e5%bc%95-%e6%a8%a1%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/znl.pub\/?p=718","title":{"rendered":"Mysql\u6dfb\u52a0\u590d\u5408\u7d22\u5f15-\u6a21\u677f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u521d\u59cb\u5316\u573a\u666f\uff1a\u6ca1\u6709\u5c31\u521b\u5efa\uff0c\u6709\u7684\u8bdd\u5c31\u4e0d\u505a\u4efb\u4f55\u7684\u5904\u7406<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5b9a\u4e49\u6570\u636e\u5e93\u3001\u8868\u540d\u3001\u7d22\u5f15\u540d\u3001\u4f5c\u4e3a\u590d\u5408\u7d22\u5f15\u7684\u5b57\u6bb5\uff08\u53ea\u6709\u4e00\u4e2a\u4e5f\u884c\uff0c\u5c31\u662f\u7ed9\u67d0\u4e2a\u5b57\u6bb5\u52a0\u7d22\u5f15\u4e86\uff09\nSET @db_name = 'mailboxes';\nSET @table_name = 'task_list';\nSET @index_name = 'idx_status_mailbox';\nSET @columns = 'control_status, mailbox_id';\n\n-- \u521b\u5efa\u4e34\u65f6\u8868\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5df2\u5b58\u5728\nDROP TEMPORARY TABLE IF EXISTS tmp_index_check;\nCREATE TEMPORARY TABLE tmp_index_check AS\nSELECT *\nFROM INFORMATION_SCHEMA.STATISTICS\nWHERE TABLE_SCHEMA = @db_name\n  AND TABLE_NAME = @table_name\n  AND INDEX_NAME = @index_name;\n\n-- \u5982\u679c\u7d22\u5f15\u4e0d\u5b58\u5728\uff0c\u5c31\u521b\u5efa\u7d22\u5f15\uff0c\u5426\u5219\u4ec0\u4e48\u90fd\u4e0d\u505a\nSET @index_exists = (SELECT COUNT(*)\n                     FROM tmp_index_check);\nSET @sql = IF(@index_exists = 0,\n              CONCAT('CREATE INDEX ', @index_name, ' ON ', @db_name, '.', @table_name, ' (', @columns, ')'),\n              CONCAT('SELECT ', ' \"Index ', @index_name, ' already exists\"', ' AS message')\n           );\nPREPARE stmt FROM @sql;\nEXECUTE stmt;\nDEALLOCATE PREPARE stmt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u66f4\u65b0\u573a\u666f\uff1a\u4fdd\u6301\u4e0e\u4e3b\u7ebf\u5206\u652f\u4e00\u81f4\uff08\u4e5f\u9002\u7528\u4e8e\u521d\u59cb\u5316\u573a\u666f\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5b9a\u4e49\u6570\u636e\u5e93\u3001\u8868\u540d\u3001\u7d22\u5f15\u540d\u3001\u4f5c\u4e3a\u590d\u5408\u7d22\u5f15\u7684\u5b57\u6bb5\uff08\u53ea\u6709\u4e00\u4e2a\u4e5f\u884c\uff0c\u5c31\u662f\u7ed9\u67d0\u4e2a\u5b57\u6bb5\u52a0\u7d22\u5f15\u4e86\uff09\nSET @db_name = 'mailboxes';\nSET @table_name = 'email_list';\nSET @index_name = 'idx_download_retry';\nSET @columns = 'download_retry, mailbox_id';\n\n-- \u521b\u5efa\u4e34\u65f6\u8868\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5df2\u5b58\u5728\nDROP TEMPORARY TABLE IF EXISTS tmp_index_check;\nCREATE TEMPORARY TABLE tmp_index_check AS\nSELECT *\nFROM INFORMATION_SCHEMA.STATISTICS\nWHERE TABLE_SCHEMA = @db_name\n  AND TABLE_NAME = @table_name\n  AND INDEX_NAME = @index_name;\nSET @index_exists = (SELECT COUNT(*)\n                     FROM tmp_index_check);\n\n-- \u5982\u679c\u7d22\u5f15\u5b58\u5728\uff0c\u5219\u5220\u9664\nSET @sql = IF(@index_exists = 0,\n              CONCAT('SELECT  \"Index ', @index_name, ' is not exists, will be created\"  AS message'),\n              CONCAT('DROP INDEX ', @index_name, ' ON ', @db_name, '.', @table_name)\n           );\nPREPARE stmt FROM @sql;\nEXECUTE stmt;\nDEALLOCATE PREPARE stmt;\n\n-- \u521b\u5efa\u7d22\u5f15\nSET @sql = CONCAT('CREATE INDEX ', @index_name, ' ON ', @db_name, '.', @table_name, ' (', @columns, ');');\nPREPARE stmt FROM @sql;\nEXECUTE stmt;\nDEALLOCATE PREPARE stmt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u521d\u59cb\u5316\u573a\u666f\uff1a\u6ca1\u6709\u5c31\u521b\u5efa\uff0c\u6709\u7684\u8bdd\u5c31\u4e0d\u505a\u4efb\u4f55\u7684\u5904\u7406 \u66f4\u65b0\u573a\u666f\uff1a\u4fdd\u6301\u4e0e\u4e3b\u7ebf\u5206\u652f\u4e00\u81f4\uff08\u4e5f\u9002\u7528\u4e8e\u521d\u59cb\u5316\u573a\u666f\uff09<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-28"],"_links":{"self":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=718"}],"version-history":[{"count":2,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":720,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/718\/revisions\/720"}],"wp:attachment":[{"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}