3

I have more than two tables, while fetching 2,00,000 records from database taking too much time what I am expecting, so I need to convert it to sub queries?

The following code:

->join('track_details','track_details.code','=',$esealTable.'.primary_id')
->join('track_history as th','th.track_id','=','track_details.track_id')
->join('locations as l','l.location_id','=','th.src_loc_id')
->join('products','products.product_id','=',$esealTable.'.pid')
->where(['level_id'=>0, 'products.product_type_id'=>8003])
->whereIn('l.location_type_id',[741,744])
->whereIn('th.transition_id',[537,569]);
->get(['primary_id as iot','products.material_code','th.sync_time as datetime'])
->take(200000);
6
  • I think 2,00,000 is not too much while you fetch data from query. But it takes time while you process data with custom methods and loops. Commented Jul 3, 2017 at 5:30
  • yeah thanks @Sagar Gautam Actually i have array_map() and array_unique methods while retrieving this data to json. Commented Jul 3, 2017 at 5:39
  • So, you have to look for better way to retrieve data at first rather than query optimization Commented Jul 3, 2017 at 5:43
  • 1
    Ok Thanks @SagarGautam Commented Jul 3, 2017 at 5:46
  • By the way when i use order by clause to particular query it will taking too much time, without order by clause, it is faster. Commented Jul 3, 2017 at 6:11

1 Answer 1

1

If you need all these data retrieved with query in json,

Laravel provides toJson() function similar to toArray() function. See docs here. You just need to add toJson() at the end of query like this:

->join('track_details','track_details.code','=',$esealTable.'.primary_id')
...
...
..
->get(['primary_id as iot','products.material_code','th.sync_time as datetime'])
->take(200000)
->toJson();

In this way, you can directly convert fetched data to json format.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.