本文介绍了使用Drupal查询构建器添加FORCE INDEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:29