平時工作項目上難免會遇到一些數據量非常大的情況去分頁,上網看了很多資料,整理一下自己的觀點看法。
數據量小的時候,我問往往就直接利用limit 直接進行分頁,SQL如下:
<pre name="code" class="sql"><span style="color: rgb(68, 68, 68); font-family: Simsun; font-size: 14px; line-height: 26px;">select * from users order by id asc limit </span><span style="color: rgb(68, 68, 68); font-family: Simsun; font-size: 14px; line-height: 26px;">10,10;</span>
數據量大的時候如下:
<span style="color: rgb(68, 68, 68); font-family: Simsun; font-size: 14px; line-height: 26px;">select id,name,content from users order by id asc limit 100000,10;</span>
掃描100010行
如果記錄了上次的最大ID,如下
<span style="color: rgb(68, 68, 68); font-family: Simsun; font-size: 14px; line-height: 26px;"><span style="color: rgb(68, 68, 68); font-family: Simsun; font-size: 14px; line-height: 26px;">select id,name,content from users where id>100000 order by id asc limit 10;</span></span>
掃描10行。
總數據有500萬左右,以下例子