Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for index / query hints in query builder #19946

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions framework/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class Query extends Component implements QueryInterface, ExpressionInterface
* @see from()
*/
public $from;
/**
* @var string|null force to use specific index. For example, `'userTimeCreated'`.
* This is used to construct the FROM clause in a SQL statement.
* @see forceIndex()
*/
public $forceIndex;
/**
* @var array|null how to group the query results. For example, `['company', 'department']`.
* This is used to construct the GROUP BY clause in a SQL statement.
Expand Down Expand Up @@ -825,6 +831,25 @@ public function from($tables)
return $this;
}

/**
* Sets the FORCE INDEX part of the query.
* @param string $forceIndex
*
* Here are some examples:
*
* ```php
* // SELECT * FROM `user` FORCE INDEX (userTimeCreated);
* $query = (new \yii\db\Query)->from('user')->forceIndex('userTimeCreated');
* ```
*
* @return $this the query object itself
*/
public function forceIndex($forceIndex)
{
$this->forceIndex = $forceIndex;
return $this;
}

/**
* Sets the WHERE part of the query.
*
Expand Down Expand Up @@ -1381,6 +1406,7 @@ public static function create($from)
'selectOption' => $from->selectOption,
'distinct' => $from->distinct,
'from' => $from->from,
'forceIndex' => $from->forceIndex,
'groupBy' => $from->groupBy,
'join' => $from->join,
'having' => $from->having,
Expand Down
13 changes: 10 additions & 3 deletions framework/db/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function build($query, $params = [])

$clauses = [
$this->buildSelect($query->select, $params, $query->distinct, $query->selectOption),
$this->buildFrom($query->from, $params),
$this->buildFrom($query->from, $params, $query->forceIndex),
$this->buildJoin($query->join, $params),
$this->buildWhere($query->where, $params),
$this->buildGroupBy($query->groupBy),
Expand Down Expand Up @@ -1273,17 +1273,24 @@ public function buildSelect($columns, &$params, $distinct = false, $selectOption
/**
* @param array $tables
* @param array $params the binding parameters to be populated
* @param string $forceIndex
* @return string the FROM clause built from [[Query::$from]].
*/
public function buildFrom($tables, &$params)
public function buildFrom($tables, &$params, $forceIndex = null)
{
if (empty($tables)) {
return '';
}

$tables = $this->quoteTableNames($tables, $params);

return 'FROM ' . implode(', ', $tables);
$from = 'FROM ' . implode(', ', $tables);

if (!empty($forceIndex)) {
$from .= " FORCE INDEX ({$forceIndex}) ";
}

return $from;
}

/**
Expand Down
Loading