Skip to content

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

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

Open
wants to merge 3 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 @@
* @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 @@
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)

Check warning on line 847 in framework/db/Query.php

View check run for this annotation

Codecov / codecov/patch

framework/db/Query.php#L847

Added line #L847 was not covered by tests
{
$this->forceIndex = $forceIndex;
return $this;

Check warning on line 850 in framework/db/Query.php

View check run for this annotation

Codecov / codecov/patch

framework/db/Query.php#L849-L850

Added lines #L849 - L850 were not covered by tests
}

/**
* Sets the WHERE part of the query.
*
Expand Down Expand Up @@ -1381,6 +1406,7 @@
'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 @@

$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 @@ -1272,17 +1272,24 @@
/**
* @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}) ";

Check warning on line 1289 in framework/db/QueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

framework/db/QueryBuilder.php#L1289

Added line #L1289 was not covered by tests
}

return $from;
}

/**
Expand Down