繁体   English   中英

使用Laravel雄辩的关系从不同的表中快速检索数据

[英]Quick retrieve data from different tables using Laravel eloquent relationships

我的应用程序有1000万条记录。

公司表:

 +---+------+-----------+-------------+------------+
 |id | name |country_id | industry_id | finance_id |
 +---+------+-----------+-------------+------------+
 | 1 |LOrel |         1 |           1 |          1 |
 +---+------+-----------+-------------+------------+

国家表:

 +---+-----------+
 |id | name      |
 +---+-----------+
 | 1 |  USA      |
 +---+-----------+
 | 2 |  Romania  |
 +---+-----------+

行业表:

 +---+-----------+
 |id | name      |
 +---+-----------+
 | 1 |  Pharmacy |
 +---+-----------+
 | 2 |  Software |
 +---+-----------+

财务表:

 +---+------------+------+--------+---------+------------+
 |id | company_id | year |revenue | profit  | loss       |
 +---+------------+------+--------+---------+------------+
 | 1 |         1  | 2017 |  4,125 |    2045 |        750 |
 +---+------------+------+--------+---------+------------+
 | 2 |         1  | 2018 | 10,125 |    7045 |        125 |
 +---+------------+------+--------+---------+------------+
 | 3 |         2  | 2017 |  8,125 |    4045 |        750 |
 +---+------------+------+--------+---------+------------+
 | 4 |         3  | 2018 |  7,125 |    2045 |        125 |
 +---+------------+------+--------+---------+------------+

这是我的数据库结构。

预期产量:

 +---------+---------+----------+-----+--------+---------+
 | Country | company | industry |year |revenue | profit  |
 +---------+---------+----------+-----+--------+---------+
 |  USA    |  LOrel  | Pharmacy |2018 | 10,125 |    7045 |
 +---------+---------+----------+-----+--------+---------+

我得到我想要的结果,我正在使用laravel渴望加载和yajra laravel数据表。

现在,我需要通过范围滑块过滤数据,这需要很长时间才能从数据库检索数据。

$company_data = Company::whereBetween('revenue',$request->slider)
      ->with('country','industry','company_financial')
     ->skip($request->start)->take($request->length)->get();

提前致谢

如果只需要不频繁地运行此查询(即每周一次),则将任务卸载到队列中 我建议使用redis作为驱动程序。

创建工作开始

// WeeklyQueryJob.php

protected $slider;
protected $start;
protected $length;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($slider, $start, $length)
{
    $this->slider = $slider;
    $this->start = $start;
    $this->length = $length;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $company_data = Company::whereBetween('revenue',$this->slider)
                  ->with('country','industry','company_financial')
                  ->skip($this->start)
                  ->take($this->length)
                  ->get();

    // add logic to store/email/whatever the results
}

接下来设置计划任务以将作业排队

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->job(new WeeklyQueryJob)->weekly();
}

接下来, 向cron添加一个条目以运行调度程序

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

最后,使用主管启动队列工作器并使其保持运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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