简体   繁体   English

如何使用两个或多个表?

[英]How can I use two or more tables?

I have Two tables, i am fetching the data by left-join but it taking too much time to pull the datas into my dataTable as my database is very big. 我有两个表,我通过左联接来获取数据,但是由于数据库很大,将数据拉入dataTable需要花费太多时间。 Is there any other option? 还有其他选择吗? Kindly help please. 请帮助。

    $allPolicyWithOrganization = $allPolicyWithOrganization
        ->select('policies.*', 
                 'leads.lead_id', 
                 'leads.agent', 
                 'leads.referrer_id', 
                 'leads.vendor_id', 
                 'leads.deleted_at'
                )
        //->where('policies.status', '!=', 1)
        ->leftJoin('leads', 'policies.lead_id', '=', 'leads.lead_id');

I have policy table and lead table..i want to pull the all data from the tables..it taking so much time 我有政策表和潜在顾客表..我想从表中提取所有数据..这花费了很多时间

You should use views of database, and create index of those column where you have set filter criteria. 您应该使用数据库视图,并在设置过滤条件的位置创建这些列的索引。 It will fetch record rapidly. 它将迅速获取记录。 Try out this solution your problem will be solve. 试用此解决方案,您的问题将得到解决。 #Abir Adak #阿比尔·阿达克(Abir Adak)

Apart from using indexes in the fields you need to filter, have you tried using the cache? 除了在需要过滤的字段中使用索引之外,是否尝试过使用缓存?

Schema::table('leads', function (Blueprint $table) {
    $table->index(['lead_id', 
                   'agent', 
                   'referrer_id', 
                   'vendor_id', 
                   'deleted_at'
                   ]);
});

and use cache (in my case Redis) 并使用缓存(在我的情况下为Redis)

use Illuminate\Support\Facades\Cache;
$allPolicyWithOrganization = Cache::tags('policies')
         ->rememberForever('all.policies.with.organization', function () {
            return $allPolicyWithOrganization
                   ->select('policies.*', 
                            'leads.lead_id', 
                            'leads.agent', 
                            'leads.referrer_id', 
                            'leads.vendor_id', 
                            'leads.deleted_at'
                           )
            //->where('policies.status', '!=', 1)
            ->leftJoin('leads', 'policies.lead_id', '=', 'leads.lead_id');
         });

Remember to clear the cache when there are changes in the affected tables. 当受影响的表中有更改时,请记住要清除缓存。

Cache::forget('all.policies.with.organization');

or by tag 或按标签

Cache::tags('policies')->flush();

Cache Laravel 快取Laravel

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

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