草庐IT

mysql - Laravel 数据库 MySQL 查询需要很长时间

coder 2023-10-13 原文

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

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

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



    }

最佳答案

首先,这听起来很长。

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

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

你没有提到的是:

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

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

关于mysql - Laravel 数据库 MySQL 查询需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38998686/

有关mysql - Laravel 数据库 MySQL 查询需要很长时间的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - rspec 需要 .rspec 文件中的 spec_helper - 2

    我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只

  5. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  6. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  7. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  8. ruby - 为什么在 ruby​​ 中创建 Rational 不需要新方法 - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?

  9. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  10. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

随机推荐