讓天下沒有難用的資料庫 » 大資料分頁的一種優化方法
阿新 • • 發佈:2018-12-29
通常應用需要對錶中的資料進行翻頁,如果資料量很大,往往會帶來效能上的問題:
[email protected] 07:16:25>select count(*) from reply_0004 where thread_id = 5616385 and deleted = 0; +———-+ | count(*) | +———-+ | 1236795 | +———-+ 1 row in set (0.44 sec) [email protected] 07:16:30>select id from reply_0004 where thread_id = 5616385 and deleted = 0 order by id asc limit 1236785, 10 ; +———–+ | id | +———–+ | 162436798 | | 162438180 | | 162440102 | | 162442044 | | 162479222 | | 162479598 | | 162514705 | | 162832588 | | 162863394 | | 162899685 | +———–+ 10 rows in set (1.32 sec) [email protected] 07:16:49>select * from (select id
-> from group_thread_reply_0004 where thread_id = 5616385 and deleted = 0
-> order by id desc limit 0, 10)t order by t.id asc;
+———–+
| id |
+———–+
| 162436798 |
| 162438180 |
| 162440102 |
| 162442044 |
| 162479222 |
| 162479598 |
| 162514705 |
| 162832588 |
| 162863394 |
| 162899685 |
+———–+
10 rows in set (0.87 sec)
可以看到效能提升了50%以上。