Updated: 8/14/2003; 1:26:04 AM.
Distressed Fabric
Mcgyver5's Radio Weblog
        

Friday, January 31, 2003

This query:

1   SELECT COUNT(*) FROM

 2      (SELECT WATER_DOW,NAME,COUNTY FROM LKDBMAIN

 3      UNION SELECT ID_NO,NAME,COUNTY FROM STREAMS

 4*     )

takes 10 -12 seconds to return about 25,000 records

 

this query:

SELECT COUNT(*) FROM

 (SELECT WATER_DOW,NAME,COUNTY,ALT_NAME,'' FROM LKDBMAIN

 UNION SELECT ID_NO,NAME,COUNTY,ALT_NAME,AREA_NO FROM STREAMS

    )

takes 10 – 12 seconds.

 

But This Query:

 

 

and this query:

 1   SELECT COUNT(*) FROM

 2      (SELECT WATER_DOW,NAME FROM LKDBMAIN

 3      UNION SELECT ID_NO,NAME FROM STREAMS

 4*     )

Takes less than one second.

 

Why do I pay so big a price for the third column in a UNION and then subsequent columns seem to be free of charge?

Because UNION is doing a full table scan for each column you request to avoid duplicates.

If I am certain that LAKES and STREAMS tables don't share any records, I should use UNION ALL instead of UNION because UNION does a full join to avoid returning duplicated records.  UNION ALL just returns one table appended to the other and so is much faster.

 


2:27:14 PM    comment []


© Copyright 2003 mcgyver5.
 
January 2003
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Dec   Feb

Frequent Visits
Categories

Click here to visit the Radio UserLand website.

Subscribe to "Distressed Fabric" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.