Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to do multiple selects from multiple tables or 1 select of all your data from all the tables?

Tags:

sql

sql-server

I have multiple tables that data can be queried from with joins.

In regards to database performance:

Should I run multiple selects from multiple tables for the required data?

or

Should I write 1 select that uses a bunch of Joins to select the required data from all the tables at once?

EDIT:

The where clause I will be using for the select contains Indexed fields of the tables. It sounds like because of this, it will be faster to use 1 select statement with many joins. I will however still test the performance difference between the 2.

Thanks for all the great answers.

like image 216
Mausimo Avatar asked Dec 06 '22 13:12

Mausimo


1 Answers

Just write one query with joins. If you are concerned about performance there are a number of options including:

  • Creating indexes that will help the performance of your selects
  • Create a persisted denormalized form of the data you want so you can query one table. This would most likely be an indexed view or another table.
like image 51
Abe Miessler Avatar answered Jan 05 '23 14:01

Abe Miessler