如何优化重复查询?

时间:2018-12-20 09:33:34

标签: php laravel-5

我必须输入日期和结束日期进行查询。在这里,我在查询中多次重复了相同的代码。 在下面的示例中,只有第一个db_A和db_b仅在条件不同且开始和结束值相同的情况下获得。

对于db_C,我没有“ where('a.compid','9')”条件,并且开始/结束日期不同。

有人可以帮助我如何优化此代码吗?

这是我尝试过的代码。

$start = date("Y-m-01", strtotime($inputdate));
$end = $inputdate;

$db_A = DB::table('standard as a')
            ->join(DB::raw("(select distinct salid, cuscod, compid from stdmap_cus) b"), function($join){
                $join->on('a.compid', '=', 'b.compid')->on('a.cuscod', '=', 'b.cuscod');
            })
            ->select('b.salid', DB::raw('sum(turnover) as sumturn'))
            ->where('a.compid', '99999999')
            ->whereBetween('date', [$start, $end])
            ->groupBy('b.salid')
            ->get()->toArray();

$res_A = $this->getSalTurn(json_decode(json_encode($db_A), true));

$db_B = DB::table('standard as a')
            ->join(DB::raw("(select distinct salid, cuscod, compid from stdmap_cus) b"), function($join){
            $join->on('a.compid', '=', 'b.compid')->on('a.cuscod', '=', 'b.cuscod');
            })
            ->select('b.salid', DB::raw('sum(turnover) as sumturn'))
            ->where('a.compid', '!=', '99999999')
            ->whereBetween('date', [$startMTD, $endMTD])
            ->groupBy('b.salid')
            ->get()->toArray();

$res_B = $this->getSalTurn(json_decode(json_encode($db_B), true));

$start = date("Y-m-01", strtotime("-1 year", strtotime($inputdate)));
$end = date("Y-m-t", strtotime("-1 year", strtotime($inputdate)));


$db_C = DB::table('standard as a')
            ->join(DB::raw("(select distinct salid, cuscod, compid from stdmap_cus) b"), function($join){
                $join->on('a.compid', '=', 'b.compid')->on('a.cuscod', '=', 'b.cuscod');
            })
            ->select('b.salid', DB::raw('sum(turnover) as sumturn'))
            ->whereBetween('date', [$startMLY, $endMLY])
            ->groupBy('b.salid')
            ->get()->toArray();
$res_C = $this->getSalTurn(json_decode(json_encode($db_C), true));

1 个答案:

答案 0 :(得分:1)

您可以设置默认查询,然后将其克隆。

$start = '...';
$end = '...';

$startMTD = '...';
$endMTD = '...';

$startMLY = '...';
$endMLY = '...';

$defaultQuery = DB::table('standard as a')
      ->join(DB::raw("(select distinct salid, cuscod, compid from stdmap_cus) b"), function($join){
          $join->on('a.compid', '=', 'b.compid')->on('a.cuscod', '=', 'b.cuscod');
      })
      ->select('b.salid', DB::raw('sum(turnover) as sumturn'))
      ->groupBy('b.salid');

$db_A = (clone $defaultQuery)
      ->where('a.compid', '99999999')
      ->whereBetween('date', [$start, $end])
      ->get();

$db_B = (clone $defaultQuery)
      ->where('a.compid', '99999999')
      ->whereBetween('date', [$startMTD, $endMTD])
      ->get();

$db_C = (clone $defaultQuery)
      ->whereBetween('date', [$startMLY, $endMLY])
      ->get();

$res_A = $this->getSalTurn($db_A->toArray();
$res_B = $this->getSalTurn($db_B->toArray();
$res_C = $this->getSalTurn($db_C->toArray());

我认为您不需要使用json_decode(json_encode($db_A), true),它会返回您已经拥有的数组。