As per this answer, a UNION
query by definition is a distinct query (assuming the columns in the two results are the same). A UNION ALL
returns duplicates if they exist.
So, in Laravel 5.1 (at least), running a union as per the example in the documentation is, by default, distinct:
$first = DB::table('users')
->whereNull('first_name');
$users = DB::table('users')
->whereNull('last_name')
->union($first)
->get();
I'm not going to repeat what has been said well in that answer regarding performance (it's worth reading) but in summary a DISTINCT
is slower than a DISTINCT ALL
.