{"id":254,"date":"2024-07-21T17:36:22","date_gmt":"2024-07-21T09:36:22","guid":{"rendered":"http:\/\/8.138.155.216\/?p=254"},"modified":"2024-07-21T17:46:38","modified_gmt":"2024-07-21T09:46:38","slug":"mysql%e4%b8%ad%e7%9a%84%e5%9b%9e%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/blog.iwonder.cc\/index.php\/2024\/07\/21\/mysql%e4%b8%ad%e7%9a%84%e5%9b%9e%e8%a1%a8\/","title":{"rendered":"MySQL\u4e2d\u7684\u56de\u8868"},"content":{"rendered":"<p>\u5728 MySQL \u4e2d\uff0c\u56de\u8868\uff08Look Up\uff09\u662f\u6307\u6570\u636e\u5e93\u6267\u884c\u67e5\u8be2\u65f6\u7684\u4e00\u79cd\u64cd\u4f5c\u6a21\u5f0f\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u975e\u805a\u7c07\u7d22\u5f15\uff08\u4e5f\u79f0\u4e3a\u4e8c\u7ea7\u7d22\u5f15\u6216\u8f85\u52a9\u7d22\u5f15\uff09\u65f6\u3002<\/p>\n<p>\u5f53\u67e5\u8be2\u4f7f\u7528\u975e\u805a\u7c07\u7d22\u5f15\u65f6\uff0c\u7d22\u5f15\u4e2d\u901a\u5e38\u53ea\u5305\u542b\u90e8\u5206\u5217\u7684\u4fe1\u606f\uff0c\u800c\u5b8c\u6574\u7684\u884c\u6570\u636e\u5219\u5b58\u50a8\u5728\u805a\u7c07\u7d22\u5f15\uff08\u901a\u5e38\u662f\u4e3b\u952e\u7d22\u5f15\uff09\u4e2d\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u67e5\u8be2\u8fc7\u7a0b\u4e2d\uff0cMySQL \u9996\u5148\u4f1a\u4f7f\u7528\u975e\u805a\u7c07\u7d22\u5f15\u6765\u67e5\u627e\u5339\u914d\u7684\u884c\uff0c\u4f46\u8fd9\u901a\u5e38\u53ea\u80fd\u5f97\u5230\u8fd9\u4e9b\u884c\u7684\u4e3b\u952e\u503c\u3002<\/p>\n<p>\u4e00\u65e6 MySQL \u901a\u8fc7\u975e\u805a\u7c07\u7d22\u5f15\u627e\u5230\u4e86\u4e3b\u952e\u503c\uff0c\u5b83\u5c31\u9700\u8981\u201c\u56de\u201d\u5230\u805a\u7c07\u7d22\u5f15\u4e2d\u53bb\u67e5\u627e\u4e0e\u8fd9\u4e9b\u4e3b\u952e\u503c\u76f8\u5bf9\u5e94\u7684\u5b8c\u6574\u884c\u6570\u636e\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u88ab\u79f0\u4e3a\u56de\u8868\u3002<\/p>\n<p>\u56de\u8868\u64cd\u4f5c\u6d89\u53ca\u989d\u5916\u7684\u78c1\u76d8 I\/O \u548c CPU \u5f00\u9500\uff0c\u56e0\u4e3a\u6bcf\u6b21\u90fd\u9700\u8981\u4ece\u78c1\u76d8\u8bfb\u53d6\u6570\u636e\u9875\u6765\u83b7\u53d6\u5b8c\u6574\u7684\u884c\u8bb0\u5f55\u3002<\/p>\n<p>\u56de\u8868\u901a\u5e38\u53d1\u751f\u5728\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a \u4f7f\u7528\u975e\u805a\u7c07\u7d22\u5f15\u8fdb\u884c\u67e5\u8be2\uff0c\u4f46\u67e5\u8be2\u7ed3\u679c\u9700\u8981\u8fd4\u56de\u4e0d\u5728\u8be5\u7d22\u5f15\u4e2d\u7684\u5217\u3002 \u6267\u884c\u8303\u56f4\u67e5\u8be2\u6216\u5168\u8868\u626b\u63cf\u65f6\uff0c\u5982\u679c\u9700\u8981\u83b7\u53d6\u5b8c\u6574\u884c\u6570\u636e\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86\u7d22\u5f15\uff0c\u4e5f\u53ef\u80fd\u9700\u8981\u56de\u8868\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u56de\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u8986\u76d6\u7d22\u5f15\uff08Covering Index\uff09\u7b56\u7565\uff0c\u5373\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u67e5\u8be2\u6240\u9700\u6240\u6709\u5217\u7684\u7d22\u5f15\uff0c\u8fd9\u6837\u67e5\u8be2\u53ef\u4ee5\u76f4\u63a5\u4ece\u7d22\u5f15\u4e2d\u83b7\u53d6\u6240\u6709\u9700\u8981\u7684\u6570\u636e\uff0c\u800c\u65e0\u9700\u8bbf\u95ee\u805a\u7c07\u7d22\u5f15\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002\u6b64\u5916\uff0cMySQL 5.6 \u5f15\u5165\u7684 Multiple Range Read\uff08MRR\uff09\u4f18\u5316\u5668\u53ef\u4ee5\u7f13\u5b58\u591a\u6b21\u968f\u673a\u8bbf\u95ee\u7684\u4e3b\u952e\u503c\uff0c\u7136\u540e\u8fdb\u884c\u6279\u91cf\u8bfb\u53d6\uff0c\u51cf\u5c11\u56de\u8868\u5e26\u6765\u7684\u6027\u80fd\u5f71\u54cd\u3002<\/p>\n<h2>\u793a\u4f8b<\/h2>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a employees \u8868\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE `employees` (\n  `id` INT NOT NULL AUTO_INCREMENT,\n  `first_name` VARCHAR(45) NOT NULL,\n  `last_name` VARCHAR(45) NOT NULL,\n  `email` VARCHAR(100) NOT NULL,\n  PRIMARY KEY (`id`),\n  INDEX `idx_email` (`email`)\n);\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cid \u662f\u805a\u7c07\u7d22\u5f15\uff08\u4e3b\u952e\u7d22\u5f15\uff09\uff0c\u800c email \u662f\u975e\u805a\u7c07\u7d22\u5f15\u3002<\/p>\n<h3>\u56de\u8868\u793a\u4f8b<\/h3>\n<p>\u5982\u679c\u6211\u4eec\u6267\u884c\u4ee5\u4e0b SQL \u67e5\u8be2\uff1a<\/p>\n<pre><code class=\"language-sql\">SELECT id, first_name, last_name FROM employees WHERE email = &#039;zhangsan@example.com&#039;;\n<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0cMySQL \u5c06\u9996\u5148\u4f7f\u7528 email \u7d22\u5f15\u627e\u5230\u5339\u914d\u7684\u8bb0\u5f55\uff0c\u4f46\u662f email \u7d22\u5f15\u4e2d\u4e0d\u5305\u542b first_name \u548c last_name \u5b57\u6bb5\u3002 \u56e0\u6b64\uff0cMySQL \u5fc5\u987b\u56de\u8868\uff0c\u5373\u6839\u636e email \u7d22\u5f15\u4e2d\u627e\u5230\u7684 id \u503c\u518d\u6b21\u8bbf\u95ee\u805a\u7c07\u7d22\u5f15\uff0c\u4ee5\u83b7\u53d6 first_name \u548c last_name \u7684\u503c\u3002<\/p>\n<h3>\u907f\u514d\u56de\u8868\u793a\u4f8b<\/h3>\n<p>\u4e3a\u4e86\u907f\u514d\u56de\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8986\u76d6\u7d22\u5f15\uff0c\u5305\u62ec email \u4ee5\u53ca first_name \u548c last_name \u5b57\u6bb5\uff1a<\/p>\n<pre><code class=\"language-sql\">ALTER TABLE `employees`\nADD INDEX `idx_email_full` (`email`, `first_name`, `last_name`);\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u518d\u6b21\u8fd0\u884c\u76f8\u540c\u7684\u67e5\u8be2\uff0cMySQL \u5c06\u80fd\u591f\u76f4\u63a5\u4ece idx_email_full \u7d22\u5f15\u4e2d\u83b7\u53d6\u6240\u6709\u9700\u8981\u7684\u6570\u636e\uff0c\u65e0\u9700\u56de\u8868\u3002<\/p>\n<pre><code class=\"language-sql\">SELECT id, first_name, last_name FROM employees WHERE email = &#039;john.doe@example.com&#039;;\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u663e\u8457\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\uff0c\u7279\u522b\u662f\u5f53\u8868\u975e\u5e38\u5927\u4e14\u6570\u636e\u5206\u5e03\u5728\u591a\u4e2a\u78c1\u76d8\u5757\u4e0a\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\uff0c\u56de\u8868\uff08Look Up\uff09\u662f\u6307\u6570\u636e\u5e93\u6267\u884c\u67e5\u8be2\u65f6\u7684\u4e00\u79cd\u64cd\u4f5c\u6a21\u5f0f\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u975e\u805a\u7c07\u7d22\u5f15\uff08\u4e5f\u79f0\u4e3a\u4e8c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[24],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-be","tag-mysql"],"_links":{"self":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":4,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":259,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions\/259"}],"wp:attachment":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}