{"id":15,"date":"2026-11-08T13:15:40","date_gmt":"2026-11-08T13:15:40","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=15"},"modified":"2026-11-08T13:15:40","modified_gmt":"2026-11-08T13:15:40","slug":"mysql-%e7%b4%a2%e5%bc%95%e5%8e%9f%e7%90%86%e4%b8%8e%e6%85%a2%e6%9f%a5%e8%af%a2%e6%8e%92%e6%9f%a5%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=15","title":{"rendered":"MySQL \u7d22\u5f15\u539f\u7406\u4e0e\u6162\u67e5\u8be2\u6392\u67e5\u5165\u95e8"},"content":{"rendered":"<p>\u63a5\u53e3\u53d8\u6162\uff0c\u5341\u6709\u516b\u4e5d\u8ddf SQL \u548c\u7d22\u5f15\u6709\u5173\u3002\u7406\u89e3 B+Tree \u7d22\u5f15\u3001\u4f1a\u770b\u6267\u884c\u8ba1\u5212\uff0c\u662f\u540e\u7aef\u548c\u6570\u636e\u5206\u6790\u540c\u5b66\u7684\u5171\u540c\u5fc5\u4fee\u8bfe\u3002<\/p>\n<h2>\u7d22\u5f15\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898<\/h2>\n<p>\u6ca1\u6709\u7d22\u5f15\u65f6\uff0cMySQL \u53ef\u80fd\u6574\u8868\u626b\u63cf\uff1b\u6709\u5408\u9002\u7d22\u5f15\u65f6\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u5230\u76ee\u6807\u884c\u3002\u4ee3\u4ef7\u662f\uff1a\u5360\u7528\u7a7a\u95f4\uff0c\u62d6\u6162\u5199\u5165\u3002<\/p>\n<h2>\u6700\u5de6\u524d\u7f00\u539f\u5219<\/h2>\n<pre><code>-- \u8054\u5408\u7d22\u5f15 (city, status, created_at)\nSELECT * FROM orders WHERE city='\u5317\u4eac' AND status='paid';           -- \u80fd\u7528\nSELECT * FROM orders WHERE city='\u5317\u4eac';                             -- \u80fd\u7528\nSELECT * FROM orders WHERE status='paid';                           -- \u901a\u5e38\u7528\u4e0d\u4e0a\u8be5\u8054\u5408\u7d22\u5f15\n<\/code><\/pre>\n<h2>\u7528 EXPLAIN \u770b\u6267\u884c\u8ba1\u5212<\/h2>\n<pre><code>EXPLAIN SELECT * FROM orders WHERE user_id = 10086 AND status='paid';\n<\/code><\/pre>\n<p>\u91cd\u70b9\u770b\uff1a<\/p>\n<ul>\n<li><code>type<\/code>\uff1aall \u5f88\u5371\u9669\uff1bref\/range \u901a\u5e38\u53ef\u63a5\u53d7<\/li>\n<li><code>key<\/code>\uff1a\u5b9e\u9645\u7528\u4e86\u54ea\u4e2a\u7d22\u5f15<\/li>\n<li><code>rows<\/code>\uff1a\u9884\u4f30\u626b\u63cf\u884c\u6570<\/li>\n<li><code>Extra<\/code>\uff1a\u662f\u5426 Using filesort \/ Using temporary<\/li>\n<\/ul>\n<h2>\u6162\u67e5\u8be2\u600e\u4e48\u5f00<\/h2>\n<pre><code>SET GLOBAL slow_query_log = ON;\nSET GLOBAL long_query_time = 1;  -- \u8d85\u8fc7 1 \u79d2\u8bb0\u4e0b\u6765\n<\/code><\/pre>\n<h2>\u5e38\u89c1\u4f18\u5316\u601d\u8def<\/h2>\n<ol>\n<li>\u7ed9 WHERE \/ JOIN \/ ORDER BY \u9ad8\u9891\u5b57\u6bb5\u5efa\u5408\u9002\u7d22\u5f15<\/li>\n<li>\u907f\u514d\u5728\u7d22\u5f15\u5217\u4e0a\u5305\u51fd\u6570\uff1a<code>WHERE DATE(created_at)=...<\/code><\/li>\n<li>\u53ea\u67e5\u9700\u8981\u7684\u5217\uff0c\u614e\u7528 <code>SELECT *<\/code><\/li>\n<li>\u6df1\u5206\u9875\u6539\u6210\u57fa\u4e8e\u4e0a\u6b21\u6700\u5927 ID \u7684\u7ffb\u9875<\/li>\n<\/ol>\n<pre><code>-- \u6df1\u5206\u9875\u4e0d\u53cb\u597d\nSELECT * FROM orders ORDER BY id LIMIT 100000, 20;\n\n-- \u66f4\u597d\nSELECT * FROM orders WHERE id &gt; 100000 ORDER BY id LIMIT 20;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u53e3\u53d8\u6162\uff0c\u5341\u6709\u516b\u4e5d\u8ddf SQL \u548c\u7d22\u5f15\u6709\u5173\u3002\u7406\u89e3 B+Tree \u7d22\u5f15\u3001\u4f1a\u770b\u6267\u884c\u8ba1\u5212\uff0c\u662f\u540e\u7aef\u548c\u6570\u636e\u5206\u6790\u540c\u5b66\u7684\u5171\u540c\u5fc5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}