LoginSignup
4
1

More than 3 years have passed since last update.

[CakePHP]SimplePaginatorでDB負荷を軽減する

Last updated at Posted at 2020-08-11

CakePHPのページネーションは便利な機能ですが、
COUNTクエリを発行するため、テーブルの件数が増えるとDB負荷が大きくなり、
レスポンスが悪くなります。

image.png

この対策として、CakePHP3.9からSimplePaginatorが追加されました。
https://book.cakephp.org/3/en/controllers/components/pagination.html#simple-pagination

Controllerのinitializeメソッドで以下のように記述することで

use Cake\Datasource\SimplePaginator;

class ExamplesController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();

        // Load the paginator component with the simple paginator strategy.
        $this->loadComponent('Paginator', [
            'paginator' => new SimplePaginator(),
        ]);
    }

Back、Nextのみのシンプルなページネーションになります。

image.png

count文を発行しなくなることにより、全件表示ができなくなるため、
全件表示はtemplatesから削除しておきましょう。

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1