Instead of joining tables, merging objects: Is this bad practice?

Is this a bad practice, and is it necessary to shoot for this code?

function get_business_addresses($business_id) { $query = $this->db->get_where('contact_business_addr_rel', array('business_id'=> $business_id)); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $address_id = $row->address_id; $address_type_id = $row->address_type_id; $this->db->select('type'); $q = $this->db->get_where('contact_business_address_type',array('id'=> $address_type_id)); $query = $this->db->get_where('contact_business_addresses',array('id'=> $address_id)); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $row2 = $q->row(); $obj_merged = (object) array_merge((array) $row, (array) $row2); $data[] = $obj_merged; } } } } return $data; } 
+4
source share
2 answers

Maybe. I can't say for sure, but it seems like it should be significantly slower than just using JOIN.

I would say that this is accurate, but the rule of thumb that I am making is "if it can be done purely in SQL, the DB engine will probably do it better than I can."

+6
source

In short, yes, that’s bad. For multiple lines (e.g. 10) this is not a big deal. However, as soon as your table has 10,000 rows, this means that you first need to get 10,000 rows, then scroll through each row and combine all the things. A simple JOIN in this case will be much faster.

+1
source

Source: https://habr.com/ru/post/1313823/


All Articles