【问题标题】:Eloquent use index (IndexRaw) not working properly with join and hasEloquent 使用索引 (IndexRaw) 不能与 join 一起正常工作并且有
【发布时间】:2018-03-16 04:17:21
【问题描述】:

我有一个使用 Eloquent 查询构建器编写的非常大的查询,我想添加要使用的索引。 我不想使用 DB 重写查询,因为如果我做错了什么会导致应用程序出现很多问题(此查询是系统中许多查询的一部分)并且会花费大量时间(查询很大)

我简化查询只是为了说明问题:

$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

如您所见,builder 很愚蠢,将table USE INDEX(allowed_index, status_index) 替换为表名。

感谢您的帮助!

仅供参考:如果重要的话,我在这里使用 Laravel 4.2

【问题讨论】:

    标签: php mysql laravel-4 eloquent laravel-eloquent


    【解决方案1】:

    我找到了解决办法

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

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 1970-01-01
      • 2018-11-10
      • 2014-07-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多