RaiderRAFC's avatar

Pagination with last row lookup

Hey everyone,

I have a table with about 2.7 million rows which I want to paginate.

However because of the size of the table I'm running against the late row caveat: how higher the page, how slower the load times.

I've come accross a couple of threads which speak of the following trick:


SELECT  t.*
FROM    (
        SELECT  id
        FROM    mytable
        ORDER BY
                id
        LIMIT 10000, 30
        ) q
JOIN    mytable t
ON      t.id = q.id

Tests in Workbench confirm that this solves the performance issue. But I'm having a hard time implenting this with a pagination control. My code now looks like this:

public function index() {
        $perPage = 18;
        $currentPage = Input::get('page') - 1;
        $KBO = DB::table('kbo_denomination')
            ->join(
                DB::raw('(SELECT id FROM kbo_denomination order by `denomination` asc limit 18 offset 2400000) AS t'),
                't.id', '=', 'kbo_denomination.id'
            )
            ->orderBy('denomination', 'asc')->get();
        $Paginator = new LengthAwarePaginator($KBO, Count($KBO), $perPage, $currentPage);
        return view ('kbo.list', compact(['KBO', 'Paginator']));
    }

However the pagination control isn't displaying and a dd returns the following:

LengthAwarePaginator {#326 ▼
  #total: 1
  #lastPage: 1
  #items: Collection {#325 ▼
    #items: array:18 [▼
      0 => {#327 ▶}
      1 => {#328 ▶}
      2 => {#329 ▶}
      3 => {#330 ▶}
      4 => {#331 ▶}
      5 => {#332 ▶}
      6 => {#333 ▶}
      7 => {#334 ▶}
      8 => {#335 ▶}
      9 => {#336 ▶}
      10 => {#337 ▶}
      11 => {#338 ▶}
      12 => {#339 ▶}
      13 => {#340 ▶}
      14 => {#341 ▶}
      15 => {#342 ▶}
      16 => {#343 ▶}
      17 => {#344 ▶}
    ]
  }
  #perPage: 18
  #currentPage: 1
  #path: "/"
  #query: []
  #fragment: null
  #pageName: "page"
}

I really don't know how I need to implement this further. Any help would be appreciated.

0 likes
1 reply
jlrdw's avatar
jlrdw
Best Answer
Level 75

I have never heard of paginating that much, better to narrow down results, or chunk somehow. Just my 2 cents.

Please or to participate in this conversation.