0

Is it better (in terms of performance, speed, etc.) to write

SELECT * FROM a WHERE (EXISTS (SELECT * FROM b))

or

SELECT * FROM a WHERE (EXISTS (SELECT 1 FROM b))

in PostgreSQL?

p.s. This question answers my question for MS SQL Server, but what about PostgreSQL?

S-Man
  • 19,905
  • 6
  • 27
  • 48
fatuhoku
  • 4,556
  • 2
  • 29
  • 68

1 Answers1

3

Per the documentation:

Since the result depends only on whether any rows are returned, and not on the contents of those rows, the output list of the subquery is normally unimportant.

klin
  • 99,138
  • 12
  • 177
  • 203
  • What does `unimportant` mean. They're obviously semantically the same but want I wish to know is whether one is more performant than the other (and I'm not sure how to go about testing this out) – fatuhoku Jul 19 '16 at 10:03
  • 1
    In the context of *The subquery will generally only be executed long enough to determine whether at least one row is returned, not all the way to completion* (for the documentation) I understand there is no difference. You can test it with `explain analyse` - i've got exactly the same executing plans. – klin Jul 19 '16 at 10:18
  • Thanks @klin, that's really good to know! – fatuhoku Jul 19 '16 at 11:15