Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which of the following SQL queries would be faster? A join on two tables or successive queries?

I have two tables here:

ITEMS
ID| DETAILS| .....| OWNER

USERS:
ID| NAME|....

Where ITEMS.OWNER = USERS.ID

I'm listing the items out with their respective owners names. For this I could use a join on both tables or I could select all the ITEMS and loop through them making a sql query to retrieve the tuple of that itmes owner. Thats like:

1 sql with a JOIN versus 1x20 single table sql queries

Which would be a better apporach to take in terms of speed? Thanks

like image 744
Ali Avatar asked Nov 27 '25 20:11

Ali


1 Answers

Of course a JOIN will be faster.

Making 20 queries will imply:

  • Parsing them 20 times
  • Making 20 index seeks to find the start of the index range on items
  • Returning 20 recordsets (each with its own metadata).
like image 158
Quassnoi Avatar answered Nov 30 '25 08:11

Quassnoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!