Which UNION query runs faster - or is better to use as a subquery.

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,296
Office Version
  1. 365
Platform
  1. Windows
EDIT: The heading should just read 'Which UNION query runs faster'

Hi all,

I need to use a subquery to exclude records from the main query. Which way of writing it would be better?

This way unions it all together before checking the ListDate and I need to use DISTINCT to exclude duplicates as the ListDate changes on each record:
Code:
SELECT DISTINCT User
FROM    (
         SELECT          User,
                         ListDate
         FROM            Contact_List_CL

         UNION SELECT    User,
                         ListDate
         FROM            Contact_List_IN
                
         UNION SELECT    User,
                         ListDate
         FROM            Contact_List_OL
        )
WHERE   ListDate BETWEEN [Forms].[frmCalendar].[clndr]-1
        AND [Forms].[frmCalendar].[clndr]-6

Or this way, which has to look the calendar form three times:
Code:
SELECT          User,
FROM            Contact_List_CL
WHERE           ListDate BETWEEN [Forms].[frmCalendar].[clndr]-1
                AND [Forms].[frmCalendar].[clndr]-6
         
         
UNION SELECT    User,
FROM            Contact_List_IN
WHERE           ListDate BETWEEN [Forms].[frmCalendar].[clndr]-1
                AND [Forms].[frmCalendar].[clndr]-6
                
                
UNION SELECT    User,
FROM            Contact_List_OL
WHERE           ListDate BETWEEN [Forms].[frmCalendar].[clndr]-1
                AND [Forms].[frmCalendar].[clndr]-6

I'm thinking it's probably the first query as there's less calls to the form?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I probably would go with the second query on the reasoning that the first selects all the data first, then filters it -- unless the tables aren't very large, as in many hundred thousands of records, in which case it probably doesn't matter. In principle SQL is a fourth generation language meaning you tell the DB what you want and the DB decides how to get it for you, including things like re-arranging your query to optimize it. I wouldn't expect Access to be as sophisticated on this point as an enterprise server system but it does seem to be quite smart most of the time. I.e., in the ideal it will run both queries pretty much the same way.

If you are accessing a form variable 3 times in a query it is quite likely that the value is cached in RAM for quick access so that may not be a concern at all. I think you'd have to time them in a series of repetitions to find out for sure which is fastest.

Note: it may be a help to index ListDate, if you haven't already done so. That can have a big impact on a query - in which case, you notice right away if it made a difference or not.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,030
Messages
6,053,129
Members
444,640
Latest member
Dramonzo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top