2017-10-04 181 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.')')); 
}