提高laravel 5.6 API速度

时间:2018-09-26 10:01:39

标签: laravel laravel-5.6

我正在使用laravel 5.6为我的网站创建API。但是API响应速度很慢,加载大约需要16秒。如何提高API响应速度?  我正在API调用内的SQL操作下面运行

$master_data        =   DB::table('native_visitors_main AS nvm')
                                    ->leftJoin('ad_campaigns AS ac','ac.id','=','nvm.ad_campaign_id')
                                    ->select(
                                            'ac.id as campaign_id',
                                            'ac.campaign_name as campaign_name',
                                            'nvm.current_cpc as cpc',
                                            DB::raw("SUM(CASE WHEN nvm.gmt_time BETWEEN '".$start_date."' and '".$end_date."'  THEN 1 ELSE 0 END) as total_click"),
                                            DB::raw("SUM(CASE WHEN nvm.is_unique > 0 AND nvm.gmt_time BETWEEN '".$start_date."' and '".$end_date."'   THEN 1 ELSE 0 END) as unique_click"),
                                            DB::raw("SUM(CASE WHEN nvm.is_unique > 0 AND nvm.pixel_status!=''  AND nvm.gmt_time BETWEEN '".$start_date."' and '".$end_date."'  THEN 1 ELSE 0 END) as ap_click"),
                                            DB::raw("SUM(CASE WHEN nvm.pixel_status   IN('TY','cake')  AND nvm.pixel_fired_on BETWEEN '".$start_date."' and '".$end_date."'  THEN 1 ELSE 0 END) as result"),
                                            DB::raw("SUM(CASE WHEN nvm.is_unique > 0 AND nvm.pixel_status  IN('LP','TY','cake')  AND nvm.pixel_fired_on BETWEEN '".$start_date."' and '".$end_date."' THEN 1 ELSE 0 END) as lp_clicks"),
                                            DB::raw("ROUND(SUM(CASE WHEN nvm.gmt_time BETWEEN '".$start_date."' and '".$end_date."' THEN nvm.current_cpc ELSE 0 END  ),2) as  total_cpc")
                                        )
                                        ->groupBy('ac.id')
                                        ->havingRaw("total_click >0")
                                        ->havingRaw("campaign_name != ''")
                                        ->get();

1 个答案:

答案 0 :(得分:1)

您的SQL查询具有其自身的性质,执行起来很慢。

采用反应式方法,我们采用了缓存机制来将结果保存以供将来使用。

您可以:

  1. 手动编码缓存系统:https://laravel.com/docs/5.7/cache
  2. 依靠库执行智能缓存:https://github.com/spiritix/lada-cache