Join 1 = 1 Against the Cross

I am joining a large table in postgresql to a table with 1 row in it. Yes, I know that I can just take the values ​​from this table with one row and put them in my query, but there are 210 columns.

So my question is this: should I join a table with one row to everything using a cross join or using a regular tautology join (1 = 1 or something else). Is one of these methods slower?

Or is there a third faster way?

+7
sql join
source share
2 answers

The reason 1=1 is to make it easier to create dynamic sql statements by combining the rows together (with the usual protections like parameterization, of course).

Having a predefined WHERE clause with 1=1 in it allows you to add additional WHERE clauses in SQL without having to check the existence of the WHERE clause in the first place, and the SQL engine usually optimizes 1=1 so there is no difference in performance.

In any other context, 1=1 usually harmless, but not particularly useful.

+4
source share

It should be noted that cross join will result in an empty table if one of the tables is empty. If one of your tables may be empty and you still need records, you might want an outer join (e.g. left, right, or full) at 1 = 1.

+3
source share

All Articles