1

Consider two tables linked in by the main_table_id:

http://www.zigdigital.com.br/download/descartar/Diagrama1121220131414.jpg

I can fetch data like:

SELECT `main_table`.* , `second_table`.*
FROM `main_table` 
JOIN `second_table`
ON (`main_table`.`main_table_id` = `second_table`.`main_table_id`)
WHERE `main_table`.`main_table_id` = ?
AND `second_table`.`main_table_id` ?

Or I can do the same thing using two queries:

SELECT *
FROM `main_table`
WHERE `main_table_id` = ?

And a second queries:

SELECT *
FROM `second_table`
WHERE `main_table_id` = ?

For me the only difference is that I'm gonna fetch the data all at once in the first case, and in two different arrays in the second case.

But my doubt is: In server performance, which one is the best way to handle the case?

Foreba
  • 410
  • 4
  • 15
  • 5
    Short answer, it depends. http://stackoverflow.com/questions/1067016/join-queries-vs-multiple-queries – jeffjenx Dec 12 '13 at 16:15
  • 2
    It *does* depend, but - to a rough approximation - one trip to the database is going to be shorter than 2. – Strawberry Dec 12 '13 at 16:16

2 Answers2

1

The only thing I can think of is that you'll be making two round trips to the db instead of just one. There are some weird cases where there's a significant difference, but I doubt this is the case here.

willy
  • 1,462
  • 11
  • 12
1

Minimize Server round trips and get all data in one shot unless its really too big and ill slow down a site loading time.

jean
  • 4,159
  • 4
  • 31
  • 52