-
-
Save alexcesaro/5642126 to your computer and use it in GitHub Desktop.
USE INDEX / FORCE INDEX in a Doctrine2 DQL query (with support for multiple tables in the FROM clause).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use UseIndexWalker; | |
use Doctrine\ORM\Query | |
$query = $em->createQuery("SELECT f FROM Foo f WHERE f.a = 1 and f.b = 2"); | |
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'UseIndexWalker'); | |
$query->setHint(UseIndexWalker::HINT_USE_INDEX, 'some_index_name'); | |
$query->getResult(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\Query\SqlWalker; | |
class UseIndexWalker extends SqlWalker | |
{ | |
const HINT_USE_INDEX = 'UseIndexWalker.UseIndex'; | |
public function walkFromClause($fromClause) | |
{ | |
$sql = parent::walkFromClause($fromClause); | |
$index = $this->getQuery()->getHint(self::HINT_USE_INDEX); | |
return preg_replace('/( INNER JOIN| LEFT JOIN|$)/', sprintf(' USE INDEX(%s)\1', $index), $sql, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment