简体   繁体   中英

Quick retrieve data from different tables using Laravel eloquent relationships

My Application having 10 millions of records.

Company Table:

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

Country Table:

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

Industry Table:

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

Finance Table:

 +---+------------+------+--------+---------+------------+
 |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 |
 +---+------------+------+--------+---------+------------+

This is my database structure.

Expected output:

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

I am getting same result what i want, i am using laravel eager loading and yajra laravel datatables.

Now, i need to filter data through range slider, it's takes long time to retrieve data from DB.

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

Thanks in advance

If you only need to run this query infrequently (ie once a week), then off load the task to the queue . I would suggest using redis for the driver.

Start by creating a Job

// 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
}

Next setup a scheduled task to queue the job

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

Next, add an entry to cron to run the scheduler

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

Last, use supervisor to start the queue worker and keep it running

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