-1

There are two tables with a common field id. What I want to do is select all attributes for a specific id, and I'm wondering which way is more efficient.

  1. Using INNER JOIN, and then a single SELECT * operation is done.
  2. Select from the smaller table first, if the id exist, then select from the larger table.
2
  • Likely the JOIN will be fastest.
    – Hart CO
    Jul 19, 2013 at 2:47
  • This will vary tremendously from DBMS to DBMS and will vary tremendously for the same DBMS in a different environment. Any answer would be specific to a particular DBMS in a particular Environment with a particular pair of tables. Jul 19, 2013 at 3:12

3 Answers 3

2

In most databases, you want to do the join:

select *
from bigtable b join
     smalltable s
     on b.id = s.id
where b.id = @id;

SQL engines have an optimizer to determine the best execution plan for a query. As mentioned in the comment, having an index woiuld often speed this up.

By selecting from one table and then the other, you are forcing a particular execution plan.

In general, you should trust the SQL engine to produce the best execution plan. In some cases, it may be better to do one and then the other, but generally that is not true.

0

This will vary based on each circumstance. You can't make a generic statement saying one will always be better . To compare you can look at execution plans, or simply run both and compare based on execution time.

Ex: if it's rare to find data in the second table, then over time it might be better to run the single query etc

0

I suggest you take the 2nd way.

It is a good practice to keep some main/primary info in a index table, then put extra / detail info on another big table.

To divide info into two part (main/primary | extra / detail), because most of the time, we only the the first part info, it can save the cost of large query, large data transfer, the net bandwidth.

Not the answer you're looking for? Browse other questions tagged or ask your own question.