2017-10-04 7 views
0

Eloquent 쿼리 빌더를 사용하여 작성된 쿼리가 커서 사용할 인덱스를 추가하고 싶습니다. DB를 사용하여 쿼리를 다시 작성하고 싶지 않습니다.이 쿼리가 시스템의 많은 쿼리에 포함되어있어 문제가 발생할 수 있고 쿼리에 많은 시간이 소요됩니다.Eloquent use index (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)을 대체합니다.

도움 주셔서 감사합니다.

는 참고 :이

답변

0

을 중요한 경우 여기 Laravel 4.2을 사용하고 나는 해결책을 발견

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