【问题标题】:Laravel Database MySQL Queries take very longLaravel 数据库 MySQL 查询需要很长时间
【发布时间】:2016-08-17 13:53:37
【问题描述】:

我有一个连接到我的数据库的页面,但在收集所有数据后加载页面大约需要 1 分钟。

我做错了什么,或者我可以做些什么来加快这个过程?

控制器

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'));



    }

【问题讨论】:

  • 嗯,你正在像机关枪一样向你的数据库发射繁重的命令。如果您的数据库中有几千行,那么像这样的操作将占用大量时间。您可以尝试增加数据库的内存或完全转移到另一个更快的服务器。我对 MySQL 的了解不足以帮助您优化这些查询,但我知道这种东西在普通服务器上只会花费很长时间。
  • 天哪。我以前从未见过很多这样的查询..
  • @Loek ,我知道 .... 我有大约 350 000 条记录。我的表也有大约 15 个字段。
  • 可能会移动或要求将您的帖子移动到codereview 网站,他们可能会帮助改善通话和/或查询。
  • @JhonnyWalker,谢谢我这样做了:codereview.stackexchange.com/questions/139038/…

标签: mysql laravel laravel-5


【解决方案1】:

首先,这听起来很长。

查询看起来确实很详细,但应该不会超过 1 分钟。

您可以尝试使用 eloquent,但这只会比原始查询快一点。

你没有提到的是:

这是本地服务器还是远程服务器? 如果您使用的是远程服务器,我的解决方案是在 mysqld 下的 my.ini / my.cnf 中使用 "skip-name-resolve" 并更新您的 key_buffer_size。

如果这不能提高速度,则可以查看该特定服务器的资源。

【讨论】:

  • 我更新了我的 key_buffer_size 并添加了 skip_name_resolve。我不得不重新启动mysql服务器。这工作 100%,感谢@MatHatrik
  • 谢谢你们俩。你节省了我很多时间。
猜你喜欢
  • 2017-05-14
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
相关资源
最近更新 更多