Jump to content

What approach to use for large queries


fastsol

Recommended Posts

I am in the process of rebuilding my business sites in the Slim3 MVC framework and using Laravel's Eloquent ORM along with several other dependencies (not that it matters for this question).  I am getting the basic hang of Eloquent and Models.  My old code has lots of full sql queries throughout, most of which I believe I can easily transform into Models and use simple Eloquent processing.  But I have a number of complex queries that either I don't have a clue how to construct with models or can't or maybe shouldn't be done with models.

 

Take these 2 queries for example:

"SELECT
	 `".BRANDS."`.*,
	 `".PRODUCTS."`.*,
	 `".CATEGORIES."`.*,
	 `".BANNERS."`.`banner_name`,
	 `".BANNERS."`.`banner_tag`,
	 `".BANNERS."`.`banner_image`
FROM `".PRODUCTS."`
INNER JOIN `".BRANDS."` ON `".BRANDS."`.`id` = `".PRODUCTS."`.`brand_id`
INNER JOIN `".CAT_RELATION."` ON `".CAT_RELATION."`.`product_id` = `".PRODUCTS."`.`id`
INNER JOIN `".CATEGORIES."` ON `".CATEGORIES."`.`cat_id` = `".CAT_RELATION."`.`category_id`
LEFT JOIN `".BANNERS."` ON `".BANNERS."`.`id` = `".PRODUCTS."`.`banner_id` AND `".BANNERS."`.`active` = TRUE 
WHERE `".PRODUCTS."`.`active`=TRUE ".$where."
GROUP BY `".PRODUCTS."`.`id`
ORDER BY $order_by"
"SELECT `".QUOTE_RESPONSES."`.*,
   `".QUOTE_ITEMS."`.*,
   `".PRODUCTS."`.*,
   `".QUOTE_DEPOSITS."`.`id` AS `deposit_id`,
   `".QUOTE_DEPOSITS."`.`product_id` AS `deposit_prod`,
   `".QUOTE_DEP_RESP."`.`id` as `dep_resp_id`,
   `".QUOTE_REQUESTS."`.`cookie_code`,
   `".QUOTE_REQUESTS."`.`id` as `orig_quote_id`,
   ".CONCAT_VEHICLE." AS `vehicle`,
   `".QUOTE_REQUESTS."`.`vehicle_sufix`,
   `".QUOTE_REQUESTS."`.`name`,
   `".SCH."`.`schedule_id`,
   `".SCH."`.`install_time`,
   `".SCH."`.`cancelled`,
   product_videos.video 
FROM `".QUOTE_RESPONSES."`
INNER JOIN `".QUOTE_ITEMS."` ON `".QUOTE_ITEMS."`.`response_id` = `".QUOTE_RESPONSES."`.`id`
INNER JOIN `".PRODUCTS."` ON `".PRODUCTS."`.`id` = `".QUOTE_ITEMS."`.`product_id`
INNER JOIN `".QUOTE_REQUESTS."` ON `".QUOTE_REQUESTS."`.`id` = `".QUOTE_RESPONSES."`.`request_id`
".VEHICLE_JOIN."
LEFT JOIN `".QUOTE_DEPOSITS."` ON `".QUOTE_DEPOSITS."`.`q_id` = `".QUOTE_RESPONSES."`.`id`
LEFT JOIN `".QUOTE_DEP_RESP."` ON `".QUOTE_DEP_RESP."`.`deposit_id` = `".QUOTE_DEPOSITS."`.`id`
LEFT JOIN `".SCH."` ON `".SCH."`.`deposit_id` = `".QUOTE_DEPOSITS."`.`id`
LEFT JOIN product_videos ON product_videos.id = `".QUOTE_RESPONSES."`.`video_id` AND `product_videos`.`active` = TRUE
WHERE `".QUOTE_RESPONSES."`.`id`=$id AND `code`= ?"

They are big and full of joins and such to minimize query calls and maximize speed with a single query.  Now I totally know how to gather all this info in Eloquent by using multiple queries, but that is going to be a lot of queries instead of 1.

 

I guess my question is: Is it worth trying to figure out how to do this with models or should I just use the Eloquent Query Builder similar to this

$this->DB->table('vehicle_attributes_relation')
	->join('vehicle_attributes', 'vehicle_attributes.v_att_id', '=', 'vehicle_attributes_relation.att_id')
	->join('v_year', 'v_year.id', '=', 'vehicle_attributes_relation.year_id')
	->join('v_model', 'v_model.id', '=', 'vehicle_attributes_relation.model_id')
	->join('v_make', 'v_make.id', '=', 'v_model.v_make_id')
	->select('vehicle_attributes.*', 'v_make.v_make', 'v_model.v_model', 'v_year.v_year')
	->where('is_private', 0)
	->where('vehicle_attributes.v_att_id', $id)
	->orderBy('v_make', 'asc')
	->orderBy('v_model', 'asc')
	->orderBy('v_year', 'asc')
	->get()

I want this rebuild to be fast and efficient like it is now and I'm willing to give up a slight bit of performance and query calls, but I think the performance would be really hurt trying to do these examples in models.  I just don't want to get into the spaghetti of sql code again in this rebuild.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.