Prashanth vadla
Prashanth vadla

Reputation: 133

Convert Joins to Subqueries in SQL (laravel controller )

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);

Upvotes: 3

Views: 71

Answers (1)

Sagar Gautam
Sagar Gautam

Reputation: 9369

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.

Upvotes: 1

Related Questions