2016-02-25 18 views
7

C'è qualche differenza tra queste due prestazioni?UNION versus SELECT DISTINCT e UNION ALL Performance

-- eliminate duplicates using UNION 
SELECT col1,col2,col3 FROM Table1 
UNION SELECT col1,col2,col3 FROM Table2 
UNION SELECT col1,col2,col3 FROM Table3 
UNION SELECT col1,col2,col3 FROM Table4 
UNION SELECT col1,col2,col3 FROM Table5  
UNION SELECT col1,col2,col3 FROM Table6  
UNION SELECT col1,col2,col3 FROM Table7  
UNION SELECT col1,col2,col3 FROM Table8  

-- eliminate duplicates using DISTINCT  
SELECT DISTINCT * FROM 
(  
    SELECT col1,col2,col3 FROM Table1 
    UNION ALL SELECT col1,col2,col3 FROM Table2 
    UNION ALL SELECT col1,col2,col3 FROM Table3 
    UNION ALL SELECT col1,col2,col3 FROM Table4 
    UNION ALL SELECT col1,col2,col3 FROM Table5  
    UNION ALL SELECT col1,col2,col3 FROM Table6  
    UNION ALL SELECT col1,col2,col3 FROM Table7  
    UNION ALL SELECT col1,col2,col3 FROM Table8  
) x 
+0

Wrapping tutto in un "SELECT distict" crea una tabella temporanea che è (tipo di) costoso. A parte questo, non vedo perché DISTICNT ... UNION ALL sarebbe più veloce di (distinto) UNION – apokryfos

+0

Entrambi mostrano diversi piani di esecuzione in alcuni casi, ma lo stesso in altri, e questa è una grande confusione ora. –

+0

Mi farebbe affidamento su Query Optimizer per capire l'unione – Paparazzi

risposta

7

La differenza tra Unione e Union tutto è che UNION ALL non eliminerà le righe duplicate, invece semplicemente tira tutte le righe di tutte le tabelle che corrispondono ai tuoi specifiche query e li combina in una tabella.

A l'istruzione UNION fa effettivamente un SELECT DISTINCT sul set di risultati.

Se si seleziona Distinto da Unione Tutti i risultati impostati, quindi l'uscita sarà uguale a il set di risultati dell'Unione.

Edit:

prestazioni sul costo della CPU:

Mi spiego con Esempio:

Ho due domande. uno è Union altro è Union All

SET STATISTICS TIME ON 
GO 

select distinct * from (select * from dbo.user_LogTime 
union all 
select * from dbo.user_LogTime) X 
GO 

SET STATISTICS TIME OFF 

SET STATISTICS TIME ON 
GO 

select * from dbo.user_LogTime 
union 
select * from dbo.user_LogTime 
GO 

SET STATISTICS TIME OFF 

Ho eseguito entrambi nella stessa finestra di query in SMSS. Vediamo il Piano di esecuzione in SMSS:

The Execution Plan

Quello che succede è, la query con Unione Tutto e distinto avrà CPU costano più di query con Unione.

prestazione in tempo:

UNION ALL:

(1172 row(s) affected) 

SQL Server Execution Times: 
    CPU time = 0 ms, elapsed time = 39 ms. 

UNION:

(1172 row(s) affected) 

SQL Server Execution Times: 
    CPU time = 10 ms, elapsed time = 25 ms. 

Così dell'Unione è molto meglio che l'Unione Il tutto con Distinti in alla performance saggio

+0

So che le uscite saranno uguali per entrambe le query. La mia domanda era sulla differenza di prestazioni. Entrambi mostrano diversi piani di esecuzione in alcuni casi, ma lo stesso in altri, e questa è una grande confusione ora. –

+0

Cosa succede se ci sono da 6 a 8 tavoli in gioco? –

+0

Ho usato 11 'UNION' /' UNION ALL' e sorprendentemente ho ottenuto il comando 'UNION ALL' più veloce di' UNION'. Con un numero minore di tabelle, 'UNION' sembra essere più veloce. Puoi verificare questo? –

0

Un altro esempio on-punto che illustra i quattro casi possibili:

/* with each case we should expect a return set: 
(1) DISTINCT UNION {1,2,3,4,5} - is redundant with case (2) 
(2) UNION {1,2,3,4,5} - more efficient? 
(3) DISTINCT UNION ALL {1,2,2,3,3,4,4,5} 
(4) UNION ALL {1,1,2,2,2,3,3,4,4,5} 
*/ 

declare @t1 table (c1 varchar(15)); 
declare @t2 table (c2 varchar(15)); 

insert into @t1 values ('1'),('1'),('2'),('3'),('4'); 

insert into @t2 values ('2'),('2'),('3'),('4'),('5'); 


select DISTINCT * from @t1 --case (1) 
UNION 
select DISTINCT * from @t2 order by c1 

select * from @t1 --case (2)  
UNION 
select * from @t2 order by c1 

select DISTINCT * from @t1 --case (3) 
UNION ALL 
select DISTINCT * from @t2 order by c1 

select * from @t1 --case (4) 
UNION ALL 
select * from @t2 order by c1