Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join on 1=1 versus cross join

Tags:

sql

join

I am joining a large table in postgresql to a table with 1 row in it. Yes, I know I could just take the values from this single row table and put them in my query written out, but there's 210 columns.

So my question is this: should I join up the single row table to everything using a cross join or using a regular join on a tautology (1 = 1 or something). Is either of those ways bound to be slower?

Or is there a third faster way?

like image 255
John Avatar asked Jan 09 '14 19:01

John


People also ask

What is the difference between join and cross join?

1. Natural Join joins two tables based on same attribute name and datatypes. Cross Join will produce cross or cartesian product of two tables . 2.

What does a join on 1 1 do?

What does ON 1=1 mean here? It just ensures the join will return a match -- 1=1 is the same as true . Given the subquery, it will only return a single row -- min(modified) . That value will be combined to the other joins.

When should you use cross join?

The CROSS JOIN is used to generate a paired combination of each row of the first table with each row of the second table. This join type is also known as cartesian join. Suppose that we are sitting in a coffee shop and we decide to order breakfast.


1 Answers

One thing to note is a cross join will result in an empty table if one of the tables is empty. If one of your tables might be empty and you still want records, you may want an outer join (e.g. left, right, or full) on 1=1.

like image 159
FistOfFury Avatar answered Oct 19 '22 07:10

FistOfFury