使用 drupal 查询构建器添加 FORCE INDEX [英] Add FORCE INDEX with drupal query builder
问题描述
我正在使用 Drupal 7
开发一个网站.我在函数 db_query
的帮助下使用查询.但现在我打算对其进行重组并使用 db_select
格式.
I am developing a website using Drupal 7
. I was using a query with the help of function db_query
. But now I am planning to restructure it and use the db_select
format.
但在某一部分,我使用的是
But at one portion, I am using a
FORCE INDEX()
强制使用特定索引来克服旧 MySql
版本的缺点.
to force the use a particular index to overcome a drawback of old MySql
version.
有什么办法可以在db_select中用join()
添加这个力索引吗?
Is there any way to add this force index with the join()
in db_select?
推荐答案
我也需要视图的自定义查询(带强制索引).我是这样做的:
I needed custom query (with force index) for views too. I did it this way:
A] 创建 SelectQueryExtender
类
class CustomSQLQuery extends SelectQueryExtender
{
private $customQuery;
private $args;
static function query($query, $args) {
$inst = new CustomSQLQuery(db_select('node', 'n'), Database::getConnection());
$inst->setQuery($query, $args);
return $inst;
}
public function setQuery($query, $args) {
$this->customQuery = $query;
$this->args = $args;
}
public function execute()
{
if (!$this
->preExecute()) {
return null;
}
return $this->connection
->query((string)$this->customQuery, $this->args);
}
}
B] 在 hook_pre_execute(&$view)
中使用此扩展程序.例如
B] use this extender in the hook_pre_execute(&$view)
. E.g.
function yourmodule_views_pre_execute(&$view) {
if ($view->current_display == "xyz") {
$rawQuery = "SELECT something where field = :arg1 ";
$q = CustomSQLQuery::query($rawQuery, [
":arg1" => "foo"
]);
$view->build_info['query'] = $q;
$view->build_info['count_query'] = $q;
}
}
这篇关于使用 drupal 查询构建器添加 FORCE INDEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!