张义洪 發表於 2025-3-22 00:00:00

Mysql分页查询limit语句的性能分析

<h2 id="item-1" style="box-sizing: border-box; margin: 0px 0px 1rem; font-weight: 500; line-height: 1.2; color: rgb(33, 37, 41); font-size: 1.75rem; padding-bottom: 0.5rem; border-bottom: 1px double rgba(0, 0, 0, 0.1); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">一、limit用法</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能。</p><pre class="hljs language-pgsql" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">SELECT&nbsp;*&nbsp;FROM&nbsp;table&nbsp;LIMIT&nbsp;&nbsp;rows&nbsp;|&nbsp;`rows&nbsp;OFFSET&nbsp;offset&nbsp;`&nbsp;
(LIMIT&nbsp;offset,&nbsp;`length`)SELECT*FROM&nbsp;tablewhere&nbsp;condition1&nbsp;=&nbsp;0and&nbsp;condition2&nbsp;=&nbsp;0and&nbsp;condition3&nbsp;=&nbsp;-1and&nbsp;condition4&nbsp;=&nbsp;-1order&nbsp;by&nbsp;id&nbsp;ascLIMIT&nbsp;2000&nbsp;OFFSET&nbsp;50000</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。<span style="box-sizing: border-box; font-weight: bolder;">LIMIT 接受一个或两个数字参数</span>。参数必须是一个整数常量。如果给定两个参数,<span style="box-sizing: border-box; font-weight: bolder;">第一个参数</span>指定第一个返回记录行的<span style="box-sizing: border-box; font-weight: bolder;"><code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">偏移量</code></span>,<span style="box-sizing: border-box; font-weight: bolder;">第二个参数</span>指定<span style="box-sizing: border-box; font-weight: bolder;">返回记录行的最大数目</span>。<code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">初始记录行的偏移量是 0(而不是 1)</code>: 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。</p><pre class="hljs language-apache" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">mysql&gt;&nbsp;SELECT&nbsp;*&nbsp;FROM&nbsp;table&nbsp;LIMIT&nbsp;5,10;&nbsp;//&nbsp;检索记录行&nbsp;6-15</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:</p><pre class="hljs language-pgsql" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">mysql&gt;&nbsp;SELECT&nbsp;*&nbsp;FROM&nbsp;table&nbsp;LIMIT&nbsp;95,-1;&nbsp;//&nbsp;检索记录行&nbsp;96-last.</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">//如果只给定一个参数,它表示返回最大的记录行数目:<br style="box-sizing: border-box;"/><code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">mysql&gt; SELECT * FROM table LIMIT 5;</code>&nbsp;//检索前 5 个记录行<br style="box-sizing: border-box;"/>//换句话说,<span style="box-sizing: border-box; font-weight: bolder;"><code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">LIMIT n</code>&nbsp;等价于&nbsp;<code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">LIMIT 0,n</code></span>。</p><h2 id="item-2" style="box-sizing: border-box; margin: 2.5rem 0px 1rem; font-weight: 500; line-height: 1.2; color: rgb(33, 37, 41); font-size: 1.75rem; padding-bottom: 0.5rem; border-bottom: 1px double rgba(0, 0, 0, 0.1); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">二、Mysql的分页查询语句的性能分析</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">MySql分页sql语句,如果和MSSQL的TOP语法相比,那么MySQL的LIMIT语法要显得优雅了许多。使用它来分页是再自然不过的事情了。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:=""><span style="box-sizing: border-box; font-weight: bolder;">最基本的分页方式:</span></p><pre class="hljs language-lasso" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">SELECT&nbsp;...&nbsp;FROM&nbsp;...&nbsp;WHERE&nbsp;...&nbsp;ORDER&nbsp;BY&nbsp;...&nbsp;LIMIT&nbsp;...</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">在中小数据量的情况下,这样的SQL足够用了,唯一需要注意的问题就是确保使用了索引:<br style="box-sizing: border-box;"/>举例来说,如果实际SQL类似下面语句,那么在category_id, id两列上建立复合索引比较好:</p><pre class="hljs language-n1ql" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">SELECT&nbsp;*&nbsp;FROM&nbsp;articles&nbsp;WHERE&nbsp;category_id&nbsp;=&nbsp;123&nbsp;ORDER&nbsp;BY&nbsp;id&nbsp;LIMIT&nbsp;50,&nbsp;10</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:=""><span style="box-sizing: border-box; font-weight: bolder;">子查询的分页方式:</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">随着数据量的增加,页数会越来越多,查看后几页的SQL就可能类似:<br style="box-sizing: border-box;"/><span style="box-sizing: border-box; font-weight: bolder;">SELECT * FROM articles WHERE category_id = 123 ORDER BY id LIMIT 10000, 10</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">一言以蔽之,就是越往后分页,<code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">LIMIT语句的偏移量就会越大,速度也会明显变慢</code>。<br style="box-sizing: border-box;"/>此时,我们可以通过子查询的方式来提高分页效率,大致如下:</p><pre class="hljs language-n1ql" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">SELECT&nbsp;*&nbsp;FROM&nbsp;articles&nbsp;WHERE&nbsp;&nbsp;id&nbsp;&gt;=&nbsp;&nbsp;
(SELECT&nbsp;id&nbsp;FROM&nbsp;articles&nbsp;&nbsp;WHERE&nbsp;category_id&nbsp;=&nbsp;123&nbsp;ORDER&nbsp;BY&nbsp;id&nbsp;LIMIT&nbsp;10000,&nbsp;1)&nbsp;LIMIT&nbsp;10</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:=""><span style="box-sizing: border-box; font-weight: bolder;">JOIN分页方式</span></p><pre class="hljs language-n1ql" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">SELECT&nbsp;*&nbsp;FROM&nbsp;`content`&nbsp;AS&nbsp;t1&nbsp;&nbsp;&nbsp;
JOIN&nbsp;(SELECT&nbsp;id&nbsp;FROM&nbsp;`content`&nbsp;ORDER&nbsp;BY&nbsp;id&nbsp;desc&nbsp;LIMIT&nbsp;&quot;.($page-1)*$pagesize.&quot;,&nbsp;1)&nbsp;AS&nbsp;t2&nbsp;&nbsp;&nbsp;
WHERE&nbsp;t1.id&nbsp;&lt;=&nbsp;t2.id&nbsp;ORDER&nbsp;BY&nbsp;t1.id&nbsp;desc&nbsp;LIMIT&nbsp;$pagesize;</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">经过我的测试,join分页和子查询分页的效率基本在一个等级上,消耗的时间也基本一致。<br style="box-sizing: border-box;"/>explain SQL语句:</p><pre class="hljs language-pgsql" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">id&nbsp;select_type&nbsp;table&nbsp;type&nbsp;possible_keys&nbsp;key&nbsp;key_len&nbsp;ref&nbsp;rows&nbsp;Extra1&nbsp;PRIMARY&nbsp;&nbsp;system&nbsp;NULL&nbsp;NULL&nbsp;NULL&nbsp;NULL&nbsp;1&nbsp;&nbsp;1&nbsp;PRIMARY&nbsp;t1&nbsp;range&nbsp;PRIMARY&nbsp;PRIMARY&nbsp;4&nbsp;NULL&nbsp;6264&nbsp;Using&nbsp;where2&nbsp;DERIVED&nbsp;content&nbsp;index&nbsp;NULL&nbsp;PRIMARY&nbsp;4&nbsp;NULL&nbsp;27085&nbsp;Using&nbsp;index</pre><hr style="box-sizing: border-box; margin: 2rem auto; color: rgb(33, 37, 41); border-right: 0px; border-bottom: 0px; border-left: 0px; border-image: initial; opacity: 0.25; max-width: 160px; background-color: rgba(0, 0, 0, 0.5); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:=""/><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">为什么会这样呢?因为子查询是在索引上完成的,而普通的查询时在数据文件上完成的,通常来说,索引文件要比数据文件小得多,所以操作起来也会更有效率。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">实际可以利用类似策略模式的方式去处理分页,比如判断如果是一百页以内,就使用最基本的分页方式,大于一百页,则使用子查询的分页方式。</p><h2 id="item-3" style="box-sizing: border-box; margin: 2.5rem 0px 1rem; font-weight: 500; line-height: 1.2; color: rgb(33, 37, 41); font-size: 1.75rem; padding-bottom: 0.5rem; border-bottom: 1px double rgba(0, 0, 0, 0.1); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">三、对于有大数据量的mysql表来说,使用LIMIT分页存在很严重的性能问题。</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">查询从第1000000之后的30条记录:</p><pre class="hljs language-n1ql" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background-color: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; margin-top: 0px !important;">SQL代码1:平均用时6.6秒&nbsp;SELECT&nbsp;*&nbsp;FROM&nbsp;`cdb_posts`&nbsp;ORDER&nbsp;BY&nbsp;pid&nbsp;LIMIT&nbsp;1000000&nbsp;,&nbsp;30SQL代码2:平均用时0.6秒&nbsp;SELECT&nbsp;*&nbsp;FROM&nbsp;`cdb_posts`&nbsp;WHERE&nbsp;pid&nbsp;&gt;=&nbsp;(SELECT&nbsp;pid&nbsp;FROM&nbsp;&nbsp;`cdb_posts`&nbsp;ORDER&nbsp;BY&nbsp;pid&nbsp;LIMIT&nbsp;1000000&nbsp;,&nbsp;1)&nbsp;LIMIT&nbsp;30</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">因为要<span style="box-sizing: border-box; font-weight: bolder;">取出所有字段内容</span>,第一种需要跨越大量数据块并取出,而第二种基本通过直接<span style="box-sizing: border-box; font-weight: bolder;"><code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">根据索引字段定位后,才取出相应内容</code></span>,效率自然大大提升。对limit的优化,不是直接使用limit,而是首先获取到offset的id,然后直接使用limit size来获取数据。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">可以看出,越往后分页,LIMIT语句的偏移量就会越大,两者速度差距也会越明显。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:="">实际应用中,可以利用类似策略模式的方式去处理分页,比如判断如果是一百页以内,就使用最基本的分页方式,大于一百页,则使用子查询的分页方式。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:=""><span style="box-sizing: border-box; font-weight: bolder;">优化思想:<code style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; color: var(--bs-code-color); overflow-wrap: break-word;">避免数据量大时扫描过多的记录</code></span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:=""><img referrerpolicy="no-referrer" src="https://zhuji.jb51.net/uploads/allimg/20240122/1-240122095246364.png" alt="clipboard.png" title="clipboard.png" style="max-width:100%!important;height:auto!important;box-sizing: border-box; vertical-align: middle; max-width: 100%; cursor: zoom-in; display: block; margin: 0px auto;"/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:=""><img referrerpolicy="no-referrer" src="https://zhuji.jb51.net/uploads/allimg/20240122/1-240122095246116.png" alt="clipboard.png" title="clipboard.png" loading="lazy" style="max-width:100%!important;height:auto!important;box-sizing: border-box; vertical-align: middle; max-width: 100%; cursor: zoom-in; display: block; margin: 0px auto;"/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 1.25rem; overflow-wrap: break-word; word-break: break-all; color: rgb(33, 37, 41); font-family: system-ui, -apple-system, " segoe="" helvetica="" noto="" liberation="" apple="" color="" ui="" white-space:="" background-color:=""><span style="box-sizing: border-box; font-weight: bolder;">为了保证index索引列连续,可以为每个表加一个自增字段,并且加上索引</span></p><p><br/></p>
頁: [1]
查看完整版本: Mysql分页查询limit语句的性能分析