优化许多" BETWEEN?和?"哪个条款

时间:2016-07-01 22:14:21

标签: php mysql sql laravel laravel-4

我试图抓住里程数在$range$interval之内的车辆。目前,有大量between ? and ?条款正在大大减慢查询速度。有没有更好的方法来实现这一目标?我认为模运算可能会起作用,尽管$range会使其变得更加困难。

$interval = 15000;
$range = 5000;
$max = 3000000;

for ($i = $interval; $i < $max; $i += $interval)
{
    $between = [
        $i - $range,
        $i + $range,
    ];

    // generates "WHERE mileage BETWEEN ? AND ? OR mileage BETWEEN ? AND ? ..."
    $query->orWhereBetween('mileage', $between);
}

1 个答案:

答案 0 :(得分:3)

select .. where (mileage>=15000) and ((mileage-10000) mod 15000)<=10000