如何优化数据库查询并减少数据请求数量

时间:2019-07-29 12:15:32

标签: php mysql laravel

我有一个网页,我从数据库中获取1000条记录。但是网页加载时间太长,甚至野生动物园浏览器都发出警告,指出网页占用了太多内存。现在,我可以减少数据数量,但可以减少请求,减少请求数量的方法是什么。 如果我使用延迟加载查询,网页速度会提高吗?因为我的网页包含很多视频URL,Google驱动器URL和文档,可能是解释器转到该链接并检查并返回到应用程序,我如何在1个请求中完成该操作

这是我的数据显示

https://imgur.com/a/qeX62si

$productsFeatured = Product::select('products.low_emitting_material_prerequisite_file',
            'manufacturers.phone','manufacturers.address','manufacturers.linkedin_url','manufacturers.city','manufacturers.state', 
            'products.specs_file','products.bim_file_url','products.specs_file_url','products.low_emitting_material_credit_file', 
            'products.id', 'products.name', 'products.logo','products.video_file', 'products.manufacturer_id',
            'products.division_id', 'products.section_id','products.datasheet_file', 'products.website', 'divisions.name as division_name', 'divisions.code as division_code',
            'sections.name as section_name', 'sections.code'   
             )
                    ->whereIn('products.status', ['active']);

                    if($request->chps_approved && in_array('63', $request->chps_approved)){
                        $productsFeatured->where(function ($query) {
                            $query->where('products.low_emitting_material_prerequisite_file', '!=', '');
                        });
                    }

                    $productsFeatured->leftJoin('products_selected_leeds', 'products.id', '=', 'products_selected_leeds.product_id')
                    ->leftJoin('sections', 'products.section_id', '=', 'sections.id')
                    ->leftJoin('divisions', 'products.division_id', '=', 'divisions.id');
                    if($request->division_code){

                        $productsFeatured->where('products.division_id', '=', $request->division_code);

                        $data['divisionn_code'] = $request->division_code;
                    }
                 else{

                    $productsFeatured->where('products.division_id', '=', '1');
                    $data['divisionn_code'] = 1;
                    }
                    $productsFeatured->join('manufacturers', 'products.manufacturer_id', '=', 'manufacturers.id')
                    ->join('users', 'manufacturers.user_id', '=', 'users.id')
                    ->orderBy('divisions.code', 'asc')
                    ->orderBy('sections.code', 'asc')
                    ->groupBy('products.id');
                    if($request->ajax()){
                        if($request->manufacturer){
                        $productsFeatured->whereIn('products.manufacturer_id', $request->manufacturer);
                        }
                        if($request->searchTxt){
                        $search_text = $request->searchTxt;

                        $productsFeatured->where(function ($query) use ($search_text) {
                        $query->orWhere('manufacturers.name', 'like', '%' . $search_text . '%');
                        $query->orWhere('sections.name', 'like', '%' . $search_text . '%');
                        $query->orWhere('products.name', 'like', '%' . $search_text . '%');
                        $query->orWhere('sections.name', 'like', '%' . $search_text . '%');
                        $query->orWhere('sections.code', 'like', '%' . $search_text . '%');
                        });
                        }
                        if($request->leedv4){

                        $where_in = $request->leedv4;

                        $query = DB::table('Products_selected_leeds')
                        ->select('product_id')
                        ->whereIn('leed_id',$where_in)
                        ->get();

                        $toCollection = collect($query);

                        $arrayOfIds = $toCollection->pluck('product_id')->toArray();

                        $productsFeatured->whereIn('products.id', $arrayOfIds);


                        }

                        if($request->website){

                            $productsFeatured->where('website','like', '%www%');
                        }  
                         if($request->video_file){

                            $productsFeatured->where('products.video_file','like', '%www%');

                        }if($request->bim_file_url){

                            $productsFeatured->whereNotNull('bim_file_url');

                        }if($request->specs_file){

                            $productsFeatured->where('bim_file_url','like', '%www%');

                        }
                        if($request->featured_products){
                            $featured = $request->featured_products;
                            $productsFeatured->where(function ($query) use ($featured) {
                                $query->orWhere('products.featured',$featured);
                                $query->orWhere('manufacturers.featured',$featured);


                                });

                        }
                        $productsFeatured = $productsFeatured->orderBy('manufacturers.name', 'ASC')->get();

                        }else{
                        $productsFeatured = $productsFeatured->orderBy('manufacturers.name', 'ASC')->get();

                        }

我能做些类似的事情吗?第一次显示的记录是屏幕的大小,当用户向下滚动时,查询会再次从中运行并获得屏幕大小的记录

2 个答案:

答案 0 :(得分:1)

您没有给我们足够的信息来帮助您。请阅读this,特别注意查询性能部分,然后编辑问题以提供更多详细信息。

话说回来,这可能是查询的这一部分,这可能会使查询变慢。

                    $query->orWhere('manufacturers.name', 'like', '%' . $search_text . '%');
                    $query->orWhere('sections.name', 'like', '%' . $search_text . '%');
                    $query->orWhere('products.name', 'like', '%' . $search_text . '%');
                    $query->orWhere('sections.name', 'like', '%' . $search_text . '%');
                    $query->orWhere('sections.code', 'like', '%' . $search_text . '%');

MySQL确实很慢地处理了这个'column LIKE '%search-term%'或级联。

也许可以通过适当的索引来加快速度。但是您可能应该使用FULLTEXT search进行调查。

答案 1 :(得分:0)

您应该使用分页将数据分成几页。这是来自tutorial的示例,可以帮助您入门。

这里还有一些example code可以提供帮助。

就查询性能而言,请看一下answer,它可以为您提供有关如何编写查询的指针。