Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: multiple queries or UNION

I've 16 queries that I need to execute. All these queries have the same format :

SELECT string, number, number 

Make sense for me to group all these data together as it's to create a dashboard with all the results.

My question is : do you think a UNION ALL will be faster then executing all the queries one by one?

like image 611
Loïc Bar Avatar asked Dec 05 '22 23:12

Loïc Bar


1 Answers

UNION ALL is faster than UNION.

In case of UNION, if you have 10 columns and 100 rows, it will compare each cell (i.e. 10*100) to get distinct values, while in UNION ALL this is not the case. So the cost of getting distinct is high.

like image 173
Adeel Avatar answered Jan 02 '23 06:01

Adeel