简体   繁体   中英

Laravel Database MySQL Queries take very long

I have a page that connects to my database, but it takes about 1 minute for the page to load after collecting all the data.

Is there something I am doing wrong, or is there something I can do to speed this process up?

Controller

class ReportSummaryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth');
        $user = Auth::user();

        if (@$user->name)
            $details = DB::table('taffiliate')
                ->where('affID', "=", $user->name)
                ->get();
        else
            return redirect('/login');

        view()->share('details', $details);
    }

    public function index()
    {


        $user = Auth::user();
        $affiliate = $user->name;


        $affdata = DB::table('toutcome')->select(DB::raw('sum(LenderCommission) as LenderCommission'), 'AffID', 'AppID')
            ->whereRaw('CompletedDate >= curdate()')
            ->groupBy('AffID')
            ->orderBy('AppID', 'ASC')
            ->get();



        $data3 = DB::table('toutcome')

            ->where('CompletedDate', '>=', \Carbon\Carbon::now()->startOfMonth())
            ->whereRaw('AffID Not Like "MW0050"')

            ->join('tapplicant', 'toutcome.AppID', '=', 'tapplicant.AppID')
            ->select(DB::raw('DATE_FORMAT(CompletedDate, "%d %M %Y") as CompletedDate, 
                              SUM(LenderCommission) as commission, 
                              SUM(AffCommission) as affCommission,
                              COUNT(DISTINCT tapplicant.Email) as leadcount,
                              SUM(Status = "A" AND LenderCommission Not Like "0.00") as acceptcount'))

            ->groupBy(DB::raw('DATE_FORMAT(CompletedDate, "%d %M %Y")'))
            ->get();



        $users = Toutcome::where('CompletedDate', '>=', \Carbon\Carbon::now()->startOfMonth())

            ->join('tapplicant', 'toutcome.AppID', '=', 'tapplicant.AppID')
        ->select(DB::raw('DATE_FORMAT(CompletedDate, "%d %M %Y") as CompletedDate, 
                              SUM(LenderCommission) as Commission, 
                              SUM(AffCommission) as Affiliate_Commission, 
                              COUNT(DISTINCT tapplicant.Email) as Applications,
                              SUM(Status = "A" AND LenderCommission Not Like "0.00") as Sold'))
            ->whereRaw('AffID Not Like "MW0050"')
            ->groupBy(DB::raw('DATE_FORMAT(CompletedDate, "%d %M %Y")'))
            ->get();

$comtotal = DB::table('toutcome')

    ->where('CompletedDate', '>=', \Carbon\Carbon::now()->startOfMonth())
    ->whereRaw('LenderCommission Not Like "0.00"')

    ->sum('LenderCommission');

        $subid = DB::table('tapplicant')
            ->whereRaw('AppAffID Not Like "050"')
            ->whereRaw('AppAffID Not Like "000"')
            ->where('AppDate', '>=', \Carbon\Carbon::now()->startOfMonth())
            ->select('AppAffID')
            ->groupBy('AppAffID')
            ->get();

        $lender = DB::table('toutcome')
            ->select('LenderName')
            ->groupBy('LenderName')
            ->get();

        $imtotal = DB::table('toutcome')
            ->where('CompletedDate', '>=', \Carbon\Carbon::now()->startOfMonth())
            ->whereRaw('LenderCommission Not Like "0.00"')
            ->whereRaw('AffID Not Like "0050"')
            ->count('AppID');

        $cototal = DB::table('toutcome')
            ->where('CompletedDate', '>=', \Carbon\Carbon::now()->startOfMonth())
            ->whereRaw('LenderCommission Not Like "0.00"')
            ->whereRaw('AffID Not Like "0050"')
            ->where('Status', '=', 'A')
            ->count('AppID');

        $comtotal2 = DB::table('toutcome')
            ->where('CompletedDate', '>=', \Carbon\Carbon::now()->startOfMonth())
            ->whereRaw('LenderCommission Not Like "0.00"')
            ->sum('LenderCommission');

        $comtotal3 = DB::table('toutcome')
            ->where('CompletedDate', '>=', \Carbon\Carbon::now()->startOfMonth())
            ->whereRaw('LenderCommission Not Like "0.00"')
            ->sum('AffCommission');

return view('summary', compact('affdata','data3', 'comtotal', 'subid' , 'users', 'lender', 'imtotal', 'cototal', 'comtotal2', 'comtotal3'));



    }

Firstly that sounds really long.

The queries do look quite detailed, but it shouldn't take longer than 1 minute.

You could try using eloquent, but this will be only a little quicker than the raw queries.

Things you didn't mention are :

Is this a local server or remote server ? If you are using a remote server, my solution would be to use the "skip-name-resolve" in your my.ini / my.cnf under mysqld and update your key_buffer_size.

If this does not improve the speed, maybe look at your resources for that particular server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM