# How would you do it?

## SELECT * FROM t1, t2 WHERE t1.key=1 AND t2.key=2 AND t1.value=t2.value

##

- ## One possible approach:

  Select from one table using its key field (assume both tables have an index on key)
  Then, loop for each row returned, and...
  select from the other table using its key field and the current row’s value field

- ## But which table to select first?

  To keep it simple, assume that both tables have the same value in all rows

- ## If we know that t1.key=1 matches 1000 rows and t2.key=2 matches 1

  then we know that we should select from t2 first
  because that way we only have to select from each table once

- ## If we selected from t1 first

  then we’d have to select from t2 1000 times!

- ## An alternative approach would be to select from both and merge

## .

Previous slide | Next slide | Back to first slide | View graphic version