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
  • 22,521
  • 7
  • 40
  • 63
fatuhoku
  • 4,815
  • 3
  • 30
  • 70

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
  • 112,967
  • 15
  • 204
  • 232
  • 1
    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