{"id":272,"date":"2011-07-16T22:26:37","date_gmt":"2011-07-16T14:26:37","guid":{"rendered":"http:\/\/guoh.ai\/lifelog\/?p=272"},"modified":"2011-07-18T09:11:22","modified_gmt":"2011-07-18T01:11:22","slug":"temporary-tables-in-mysql","status":"publish","type":"post","link":"https:\/\/guoh.ai\/lifelog\/2011\/07\/temporary-tables-in-mysql\/","title":{"rendered":"MySQL\u4e34\u65f6\u8868\u7528\u6cd5"},"content":{"rendered":"<p>MySQL\u76ee\u524d\u8fd8\u4e0d\u652f\u6301\u52a8\u6001\u6e38\u6807(\u53ef\u80fd\u8fd9\u4e2a\u540d\u8bcd\u7528\u5728\u8fd9\u91cc\u4e0d\u51c6\u786e)\uff0c\u4f46\u5f88\u591a\u65f6\u5019\u6211\u4eec\u9700\u8981\u8fd9\u6837\u7684\u4e00\u4e2a\u529f\u80fd(http:\/\/bugs.mysql.com\/bug.php?id=17153)<br \/>\n\u6bd4\u5982\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u52a8\u6001\u67e5\u8be2\u51fa\u6570\u636e\uff0c\u7136\u540e\u904d\u5386\u6570\u636e\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5f53\u7136\u60f3\u52a8\u6001\u7684\u6784\u5efa\u51fa\u8fd9\u4e2a\u6e38\u6807\u51fa\u6765<\/p>\n<p>\u4eba\u6c11\u5927\u4f17\u5c31\u5f00\u59cb\u60f3\u5c3d\u529e\u6cd5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u4e0b\u9762\u7684\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u8868\u6765\u5b8c\u6210<\/p>\n<p>mysql> CREATE TEMPORARY TABLE tmp_data (uid int(11) NOT NULL) ENGINE=MEMORY DEFAULT CHARSET=utf8;<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u4e3a\u4e86\u63d0\u9ad8\u901f\u5ea6\u7528\u7684\u662fMEMORY\u5f15\u64ce\uff0c\u5f53\u7136\u5b83\u6709\u4e00\u4e9b\u9650\u5236\u884c\u7684\u4e1c\u897f\uff0c\u6bd4\u5982\u8868\u7684\u5927\u5c0f\u9650\u5236\uff0c\u67d0\u4e9b\u7c7b\u578b\u7684\u5b57\u6bb5\u4e0d\u80fd\u4fdd\u5b58<br \/>\n\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u60c5\u51b5\u6765\u6289\u62e9<br \/>\n\u66f4\u591a\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/memory-storage-engine.html<br \/>\n\u8fd8\u6709\u4e00\u4e9b\u53c2\u8003\u610f\u4e49\u7684\u6d4b\u8bc4\u4e5f\u53ef\u4ee5\u501f\u9274\uff0c\u6bd4\u5982http:\/\/www.mysqlperformanceblog.com\/2007\/08\/16\/how-much-overhead-is-caused-by-on-disk-temporary-tables\/<\/p>\n<p>\u8fd9\u91cc\u8fd8\u7ed9\u5b83\u589e\u52a0\u4e86\u4e2a\u7d22\u5f15<br \/>\nmysql> ALTER TABLE tmp_data ADD INDEX idx_uid (uid);<br \/>\nmysql> SHOW INDEX FROM tmp_data;<\/p>\n<p>\u5f53\u7136\u7528\u5b8c\u4e4b\u540e\u8bb0\u5f97\u6e05\u7406\u8868\uff0c\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f<br \/>\nmysql> DROP TEMPORARY TABLE tmp_data;<\/p>\n<p>\u4e34\u65f6\u8868\u5728\u4e0d\u540c\u8fde\u63a5\u4e4b\u95f4\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u4f46\u662f\u5728\u591a\u7ebf\u7a0b\u7684\u73af\u5883\u4e2d\uff0c\u8fd9\u4e2a\u8fd8\u662f\u5e94\u8be5\u503c\u5f97\u6ce8\u610f<\/p>\n<p>\u8fd8\u6709\u66f4\u591a\u7684\u95ee\u9898\u503c\u5f97\u7814\u7a76\uff0c\u6bd4\u5982create\u548cdrop\u4e34\u65f6\u8868\u4e0d\u4f1a\u4ea7\u751f\u4e8b\u52a1\u63d0\u4ea4<br \/>\n\u4f46\u76f4\u63a5DROP TABLE tmp_data;\u4e5f\u4f1a\u5220\u9664\u4e34\u65f6\u8868\uff0c\u4f46\u662f\u4f1a\u63d0\u4ea4\u524d\u9762\u6253\u5f00\u4e8b\u52a1<\/p>\n<p>\u4e34\u65f6\u8868\u548c\u6b63\u5f0f\u8868\u91cd\u540d\u673a\u5236\u7b49\u7b49<\/p>\n<p>\u53e6\u5916\uff0c\u5176\u5b9e\u5728MySQL\u5185\u90e8\u5b83\u4e5f\u5927\u91cf\u4f7f\u7528\u4e86\u4e34\u65f6\u8868\uff0c\u5f53\u7136\u5bf9\u7a0b\u5e8f\u5458\u6765\u8bf4\u90fd\u662f\u900f\u660e\u7684\uff0c\u4f60\u4e0d\u7528\u5173\u5fc3\u5b83\u4ec0\u4e48\u65f6\u5019\u7528\uff0c\u6570\u636e\u662f\u653e\u5728\u5185\u5b58\u8fd8\u662f\u653e\u5728\u786c\u76d8\u4e0a\u7684<br \/>\n\u60f3\u770b\u7684\u8bdd\u53ef\u4ee5\u901a\u8fc7explain\u67e5\u770b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u76ee\u524d\u8fd8\u4e0d\u652f\u6301\u52a8\u6001\u6e38\u6807(\u53ef\u80fd\u8fd9\u4e2a\u540d\u8bcd\u7528\u5728\u8fd9\u91cc\u4e0d\u51c6\u786e)\uff0c\u4f46\u5f88\u591a\u65f6\u5019\u6211\u4eec\u9700\u8981\u8fd9\u6837\u7684\u4e00\u4e2a\u529f\u80fd(http:\/\/bugs.mysql.com\/bug.php?id=17153) \u6bd4\u5982\u5728\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u52a8\u6001\u67e5\u8be2\u51fa\u6570\u636e\uff0c\u7136\u540e\u904d\u5386\u6570\u636e\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5f53\u7136\u60f3\u52a8\u6001\u7684\u6784\u5efa\u51fa\u8fd9\u4e2a\u6e38\u6807\u51fa\u6765 \u4eba\u6c11\u5927\u4f17\u5c31\u5f00\u59cb\u60f3\u5c3d\u529e\u6cd5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u4e0b\u9762\u7684\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u8868\u6765\u5b8c\u6210 mysql> CREATE TEMPORARY TABLE tmp_data (uid int(11) NOT NULL) ENGINE=MEMORY DEFAULT CHARSET=utf8; \u6ce8\u610f\u8fd9\u91cc\u4e3a\u4e86\u63d0\u9ad8\u901f\u5ea6\u7528\u7684\u662fMEMORY\u5f15\u64ce\uff0c\u5f53\u7136\u5b83\u6709\u4e00\u4e9b\u9650\u5236\u884c\u7684\u4e1c\u897f\uff0c\u6bd4\u5982\u8868\u7684\u5927\u5c0f\u9650\u5236\uff0c\u67d0\u4e9b\u7c7b\u578b\u7684\u5b57\u6bb5\u4e0d\u80fd\u4fdd\u5b58 \u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u60c5\u51b5\u6765\u6289\u62e9 \u66f4\u591a\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/memory-storage-engine.html \u8fd8\u6709\u4e00\u4e9b\u53c2\u8003\u610f\u4e49\u7684\u6d4b\u8bc4\u4e5f\u53ef\u4ee5\u501f\u9274\uff0c\u6bd4\u5982http:\/\/www.mysqlperformanceblog.com\/2007\/08\/16\/how-much-overhead-is-caused-by-on-disk-temporary-tables\/ \u8fd9\u91cc\u8fd8\u7ed9\u5b83\u589e\u52a0\u4e86\u4e2a\u7d22\u5f15 mysql> ALTER TABLE tmp_data ADD INDEX idx_uid (uid); mysql> SHOW INDEX FROM tmp_data; \u5f53\u7136\u7528\u5b8c\u4e4b\u540e\u8bb0\u5f97\u6e05\u7406\u8868\uff0c\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f mysql> DROP TEMPORARY TABLE tmp_data; \u4e34\u65f6\u8868\u5728\u4e0d\u540c\u8fde\u63a5\u4e4b\u95f4\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u4f46\u662f\u5728\u591a\u7ebf\u7a0b\u7684\u73af\u5883\u4e2d\uff0c\u8fd9\u4e2a\u8fd8\u662f\u5e94\u8be5\u503c\u5f97\u6ce8\u610f \u8fd8\u6709\u66f4\u591a\u7684\u95ee\u9898\u503c\u5f97\u7814\u7a76\uff0c\u6bd4\u5982create\u548cdrop\u4e34\u65f6\u8868\u4e0d\u4f1a\u4ea7\u751f\u4e8b\u52a1\u63d0\u4ea4 \u4f46\u76f4\u63a5DROP TABLE tmp_data;\u4e5f\u4f1a\u5220\u9664\u4e34\u65f6\u8868\uff0c\u4f46\u662f\u4f1a\u63d0\u4ea4\u524d\u9762\u6253\u5f00\u4e8b\u52a1 \u4e34\u65f6\u8868\u548c\u6b63\u5f0f\u8868\u91cd\u540d\u673a\u5236\u7b49\u7b49 \u53e6\u5916\uff0c\u5176\u5b9e\u5728MySQL\u5185\u90e8\u5b83\u4e5f\u5927\u91cf\u4f7f\u7528\u4e86\u4e34\u65f6\u8868\uff0c\u5f53\u7136\u5bf9\u7a0b\u5e8f\u5458\u6765\u8bf4\u90fd\u662f\u900f\u660e\u7684\uff0c\u4f60\u4e0d\u7528\u5173\u5fc3\u5b83\u4ec0\u4e48\u65f6\u5019\u7528\uff0c\u6570\u636e\u662f\u653e\u5728\u5185\u5b58\u8fd8\u662f\u653e\u5728\u786c\u76d8\u4e0a\u7684 \u60f3\u770b\u7684\u8bdd\u53ef\u4ee5\u901a\u8fc7explain\u67e5\u770b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_gfmr_meta_descriptions":[],"_gfmr_multilingual_taxonomy_terms":[],"footnotes":""},"categories":[13,8],"tags":[12],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-database","category-paste","tag-mysql"],"_links":{"self":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":4,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":278,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/272\/revisions\/278"}],"wp:attachment":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}