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 * FROM table LIMIT rows | `rows OFFSET offset ` (LIMIT offset, `length`)SELECT*FROM tablewhere condition1 = 0and condition2 = 0and condition3 = -1and condition4 = -1order by id ascLIMIT 2000 OFFSET 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> SELECT * FROM table LIMIT 5,10; // 检索记录行 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> SELECT * FROM table LIMIT 95,-1; // 检索记录行 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> SELECT * FROM table LIMIT 5;</code> //检索前 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> 等价于 <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 ... FROM ... WHERE ... ORDER BY ... LIMIT ...</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 * FROM articles WHERE category_id = 123 ORDER BY id LIMIT 50, 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 * FROM articles WHERE id >=
(SELECT id FROM articles WHERE category_id = 123 ORDER BY id LIMIT 10000, 1) LIMIT 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 * FROM `content` AS t1
JOIN (SELECT id FROM `content` ORDER BY id desc LIMIT ".($page-1)*$pagesize.", 1) AS t2
WHERE t1.id <= t2.id ORDER BY t1.id desc LIMIT $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 select_type table type possible_keys key key_len ref rows Extra1 PRIMARY system NULL NULL NULL NULL 1 1 PRIMARY t1 range PRIMARY PRIMARY 4 NULL 6264 Using where2 DERIVED content index NULL PRIMARY 4 NULL 27085 Using 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秒 SELECT * FROM `cdb_posts` ORDER BY pid LIMIT 1000000 , 30SQL代码2:平均用时0.6秒 SELECT * FROM `cdb_posts` WHERE pid >= (SELECT pid FROM `cdb_posts` ORDER BY pid LIMIT 1000000 , 1) LIMIT 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]