2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CakePHP3のクエリービルダーでUSE INDEXを使いたい

Last updated at Posted at 2021-06-10

#前提

  • CakePHP 3系
  • MySQL 5.7

CakePHPのクエリービルダーを使ってSQLに USE INDEX (カラム名) を追加したい。
公式ドキュメントを確認したがクエリビルダーのページに記載はなかった。

#対応方法
from句に記載する

SampleController.php
$articles = $this->Articles->find()
->from('articles Articles USE INDEX (category)') //'テーブル名 モデル名 USE INDEX (INDEX名)'
->where(['category' => 'test'])
->toArray();

以下のようなSQLが発行されます。

use_index.sql
SELECT Articles.id AS `Articles__id`, 
FROM articles Articles USE INDEX (category)
WHERE Articles.category = test

#参考

CakePHP2より前のバージョンではまた別の方法を使っていたようです。

2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?