{"id":191200,"date":"2024-11-12T16:26:22","date_gmt":"2024-11-12T08:26:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/191200\/"},"modified":"2024-11-12T16:26:23","modified_gmt":"2024-11-12T08:26:23","slug":"%e8%a9%b3%e8%a7%a3mysql-limit%e6%9f%a5%e8%a9%a2%e5%84%aa%e5%8c%96%e7%9a%84%e5%af%a6%e9%9a%9b%e6%93%8d%e4%bd%9c%e6%ad%a5%e9%a9%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/191200\/","title":{"rendered":"\u8a73\u89e3MySQL limit\u67e5\u8a62\u512a\u5316\u7684\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f"},"content":{"rendered":"<h1 id=\"%e8%a9%b3%e8%a7%a3mysql-limit%e6%9f%a5%e8%a9%a2%e5%84%aa%e5%8c%96%e7%9a%84%e5%af%a6%e9%9a%9b%e6%93%8d%e4%bd%9c%e6%ad%a5%e9%a9%9f-JkzFFOiGzS\">\u8a73\u89e3MySQL limit\u67e5\u8a62\u512a\u5316\u7684\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cMySQL\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u67e5\u8a62\u6027\u80fd\u7684\u512a\u5316\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u7279\u5225\u662f\u5728\u4f7f\u7528 <code>LIMIT<\/code> \u5b50\u53e5\u9032\u884c\u67e5\u8a62\u6642\uff0c\u5982\u4f55\u6709\u6548\u5730\u512a\u5316\u67e5\u8a62\u6027\u80fd\u662f\u6bcf\u4f4d\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\u6311\u6230\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u4e2d <code>LIMIT<\/code> \u67e5\u8a62\u7684\u512a\u5316\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"1-%e7%90%86%e8%a7%a3limit%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95-JkzFFOiGzS\">1. \u7406\u89e3LIMIT\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p><code>LIMIT<\/code> \u5b50\u53e5\u7528\u65bc\u9650\u5236\u67e5\u8a62\u7d50\u679c\u7684\u6578\u91cf\uff0c\u901a\u5e38\u8207 <code>OFFSET<\/code> \u4e00\u8d77\u4f7f\u7528\u3002\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT * FROM table_name LIMIT offset, count;<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>offset<\/code> \u662f\u958b\u59cb\u8fd4\u56de\u8a18\u9304\u7684\u884c\u6578\uff0c\u800c <code>count<\/code> \u662f\u8981\u8fd4\u56de\u7684\u8a18\u9304\u6578\u3002\u4f8b\u5982\uff0c<code>LIMIT 10, 5<\/code> \u5c07\u8fd4\u56de\u5f9e\u7b2c11\u884c\u958b\u59cb\u76845\u689d\u8a18\u9304\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8%e7%b4%a2%e5%bc%95%e4%be%86%e5%84%aa%e5%8c%96%e6%9f%a5%e8%a9%a2-JkzFFOiGzS\">2. \u4f7f\u7528\u7d22\u5f15\u4f86\u512a\u5316\u67e5\u8a62<\/h2>\n<p>\u5728\u57f7\u884c\u5e36\u6709 <code>LIMIT<\/code> \u7684\u67e5\u8a62\u6642\uff0c\u7d22\u5f15\u7684\u4f7f\u7528\u81f3\u95dc\u91cd\u8981\u3002\u7d22\u5f15\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u5927\u6578\u64da\u96c6\u4e0a\u3002\u78ba\u4fdd\u67e5\u8a62\u4e2d\u6d89\u53ca\u7684\u5217\u6709\u9069\u7576\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>CREATE INDEX idx_column_name ON table_name(column_name);<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u52a0\u901f\u67e5\u8a62\u904e\u7a0b\uff0c\u6e1b\u5c11\u5168\u8868\u6383\u63cf\u7684\u9700\u8981\u3002<\/p>\n<h2 id=\"3-%e9%81%bf%e5%85%8d%e4%bd%bf%e7%94%a8offset-JkzFFOiGzS\">3. \u907f\u514d\u4f7f\u7528OFFSET<\/h2>\n<p>\u96d6\u7136 <code>OFFSET<\/code> \u53ef\u4ee5\u65b9\u4fbf\u5730\u5be6\u73fe\u5206\u9801\u529f\u80fd\uff0c\u4f46\u5728\u6578\u64da\u91cf\u8f03\u5927\u6642\uff0c\u4f7f\u7528 <code>OFFSET<\/code> \u6703\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002\u56e0\u70baMySQL\u9700\u8981\u8df3\u904e\u6307\u5b9a\u7684\u884c\u6578\uff0c\u9019\u5728\u6578\u64da\u91cf\u9f90\u5927\u6642\u6703\u8b8a\u5f97\u975e\u5e38\u4f4e\u6548\u3002\u70ba\u4e86\u512a\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u5982\u57fa\u65bc\u4e3b\u9375\u7684\u5206\u9801\uff1a<\/p>\n<pre><code>SELECT * FROM table_name WHERE id &gt; last_seen_id LIMIT count;<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u4f7f\u7528 <code>OFFSET<\/code>\uff0c\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8explain%e5%88%86%e6%9e%90%e6%9f%a5%e8%a9%a2-JkzFFOiGzS\">4. \u4f7f\u7528EXPLAIN\u5206\u6790\u67e5\u8a62<\/h2>\n<p>\u5728\u512a\u5316\u67e5\u8a62\u4e4b\u524d\uff0c\u4f7f\u7528 <code>EXPLAIN<\/code> \u8a9e\u53e5\u4f86\u5206\u6790\u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9019\u53ef\u4ee5\u5e6b\u52a9\u4f60\u4e86\u89e3\u67e5\u8a62\u7684\u6027\u80fd\u74f6\u9838\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>EXPLAIN SELECT * FROM table_name LIMIT 10;<\/code><\/pre>\n<p>\u901a\u904e\u5206\u6790\u7d50\u679c\uff0c\u4f60\u53ef\u4ee5\u770b\u5230MySQL\u5982\u4f55\u57f7\u884c\u67e5\u8a62\uff0c\u4e26\u6839\u64da\u9700\u8981\u9032\u884c\u8abf\u6574\u3002<\/p>\n<h2 id=\"5-%e6%b8%9b%e5%b0%91%e8%bf%94%e5%9b%9e%e7%9a%84%e5%88%97%e6%95%b8-JkzFFOiGzS\">5. \u6e1b\u5c11\u8fd4\u56de\u7684\u5217\u6578<\/h2>\n<p>\u5728\u67e5\u8a62\u4e2d\uff0c\u50c5\u9078\u64c7\u5fc5\u8981\u7684\u5217\u53ef\u4ee5\u6e1b\u5c11\u6578\u64da\u50b3\u8f38\u91cf\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u907f\u514d\u4f7f\u7528 <code>SELECT *<\/code>\uff0c\u800c\u662f\u660e\u78ba\u6307\u5b9a\u9700\u8981\u7684\u5217\uff1a<\/p>\n<pre><code>SELECT column1, column2 FROM table_name LIMIT 10;<\/code><\/pre>\n<h2 id=\"6-%e8%80%83%e6%85%ae%e4%bd%bf%e7%94%a8%e7%89%a9%e5%8c%96%e8%a6%96%e5%9c%96-JkzFFOiGzS\">6. \u8003\u616e\u4f7f\u7528\u7269\u5316\u8996\u5716<\/h2>\n<p>\u5982\u679c\u67e5\u8a62\u975e\u5e38\u8907\u96dc\u4e14\u7d93\u5e38\u57f7\u884c\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u7269\u5316\u8996\u5716\u4f86\u5b58\u5132\u67e5\u8a62\u7d50\u679c\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u67e5\u8a62\u7684\u8a08\u7b97\u6642\u9593\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-JkzFFOiGzS\">\u7e3d\u7d50<\/h2>\n<p>\u512a\u5316MySQL\u4e2d\u7684 <code>LIMIT<\/code> \u67e5\u8a62\u662f\u4e00\u500b\u591a\u65b9\u9762\u7684\u904e\u7a0b\uff0c\u6d89\u53ca\u7d22\u5f15\u7684\u4f7f\u7528\u3001\u67e5\u8a62\u7d50\u69cb\u7684\u8abf\u6574\u4ee5\u53ca\u6027\u80fd\u5206\u6790\u7b49\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u6578\u64da\u96c6\u6642\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0eMySQL limit\u67e5\u8a62\u512a\u5316\u7684\u5be6\u969b\u64cd\u4f5c\u6b65\u9a5f\uff0c\u63d0\u5347\u6578\u64da\u6aa2\u7d22\u6548\u7387\uff0c\u8b93\u4f60\u7684\u6578\u64da\u5eab\u6027\u80fd\u66f4\u4e0a\u4e00\u5c64\u6a13\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-191200","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/191200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=191200"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/191200\/revisions"}],"predecessor-version":[{"id":191201,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/191200\/revisions\/191201"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=191200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=191200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=191200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}