ページネーションの方式 名前 クエリ例 Cost 簡単 途中から 一貫性 オフセット OffsetPagination /?limit=20&offset=100 X O O X キーセット KeysetPagination /?limit=20&key__lte=hoge O △ (クライアントが大 変) X O シーク SeekPagination /?limit=20&after_id=hoge O △ X O カーソル CursorPagination /?cursor=hogehoge O X X O 今日紹介する4方式
OffsetPagination Benefits & Downsides 欠点 ● 大きいオフセット値を使うとパフォーマンスが下がる ○ SQLでOffsetNinを使う為 ○ The larger the offset, the slower the request is, up until the point that it times out. https://www.shopify.com/partners/blog/relative-pagination ● 新しいデータが挿入されると一貫性を保てない(ページドリフト) ○ 時間でソートされている時最初に20件取得したあと15件の新規レコードが挿入されたと仮 定する ○ 2回めのリクエストでは新しく5件しか取得できない 挿入回数が少ない・データの上限が小さいアプリケーションにむいてる
参考 ● REST API Design: Filtering, Sorting, and Pagination ○ https://www.moesif.com/blog/technical/api-design/REST-API-Design-Filtering-Sort ing-and-Pagination/ ● DjangoRestFramework: Pagination ○ https://www.django-rest-framework.org/api-guide/pagination/ ● How to Use Relative Pagination In Your Application ○ https://www.shopify.com/partners/blog/relative-pagination