2017-10-04 184 views
0

我使用Eloquent查询生成器编写了一个非常大的查询,我想添加要使用的索引。 我不想重写使用数据库的查询,因为如果我犯了错误(这个查询是系统中许多查询的一部分),它会在应用程序中导致很多问题,并且它花费很多时间(查询很大)雄辩的使用索引(IndexRaw)无法正常使用并且具有

我简化了查询,是想说明这个问题:

$model = $this->model->setConnection($connection); 
if(!is_null($forceIndex)) { 
    $model = $model::IndexRaw('USE INDEX('.$forceIndex.')'); 
} 
$model = $model->has('advertiser') 
dd($model->toSql()); 

查询:

select * from table USE INDEX(allowed_index, status_index) where (select count(*) from `advertisers` where `table USE INDEX(allowed_index, status_index)`.`advertiser_id` = `advertisers`.`id`) >= 1 

,你可以看到建设者我很愚蠢,并以table USE INDEX(allowed_index, status_index)作为表名。

感谢您的帮助!

FYI:我使用Laravel 4.2在这里如果它的事项

回答

0

我找到了解决方案

$model = $this->model->setConnection($connection); 
if(!is_null($forceIndex)) { 
    $model = $model->from(\DB::raw('table USE INDEX('.$forceIndex.')')); 
}