mardi 31 mai 2016

Laravel 5.1 ajax get requests takes drastically longer time to load the page

I am using laravel 5.1 and i want to make ajax get request calls as its a admin dashboard.

I am using DB queries for my response return and getting the data on the tables and graph using ajax, since i have dates filter and other filters.

My app is getting too slow to load when i click on apply button which makes different types of request on the page.

So this is what i am doing.

  $data = DB::table("leads")
->select((array(DB::Raw('DATE(leads.created_at) as creation'), 
DB::Raw("COUNT(leads.id) as total_leads"), DB::Raw("leads.id as id"),DB::Raw("leads.customer_name as customer_name" ),DB::Raw("leads.city as city"), DB::Raw("leads.email as email"),DB::Raw("leads.contact as contact"),DB::Raw("leads.source as source"),DB::Raw("leads.campaign_name as campaign"),DB::Raw("leads.ad_group as ad_group"),DB::Raw("leads.ad as ad"),DB::Raw("leads.keyword as keyword") )))
->where("leads.created_at",">=",$startDate)
->where("leads.created_at","<",$endDate)
->groupBy("leads.created_at")
->orderBy("leads.created_at","desc")
->get();

 return view("dashboard",compact("data"));

Now if i use this this query to get the ajax call, which look like this

  $(document).ready(function(){
  $(".applyBtn").on("click",function(){

  var i = $('input[name="daterangepicker_start"]').val();
  var e = $('input[name="daterangepicker_end"]').val();
  console.log(i);

  console.log($("input[id=daterangepicker_start1]").val($('input[name="daterangepicker_start"]').val()));
    console.log($("input[id=daterangepicker_end1]").val($('input[name="daterangepicker_end"]').val()));
  var data = "daterangepicker_start="+i+"&daterangepicker_end="+e;
  $.ajax({
    type:"GET",
    data:data,
    url:'',
    success:function(data)
      {
    $("#ack").html(data);

      }
  })

I am returning another blade template to replace the div ack.

This thing works great in localhost and its working quite faster than its loading on the server,but when i loaded this on linode server, this queries get too slow to work. I have installed the debugbar and it says 400 ms for loading all the queries. Can there be any configuration problem on the server, since its using apache as it was configured before only.

I have too many DB queries on the several pages, say dashboard,leads page.

What should i need to do for optimizing my laravel5.1 app, and how can i serve it easily using ajax.

Please let me know any solutions ,so that the loading request made by ajax take less time to load the other blade template which pulls the data view.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire