| The union
| query essentially appends a single record with the values 0 and "All"
[quoted text clipped - 3 lines]
| (or Top 1),
| as the UNION recordset, such as it is, consists of one "record".
you're wrong!
UNION appends a single record onto another recordset that MUST be
calculated as DISTINCT (!)
| I can't
| imagine there is an efficiency improvement one way or the other.
there is no matter of imagination. Just make tests:
BigTable has 100000 records, indexed on both fields.
ID is PrimaryKey
qr1Union:
-----------------------------------
SELECT ID, Field1 FROM BigTable
UNION
SELECT 0, "<All>" From BigTable
ORDER BY Field1;
qr2UnionAllDistinct:
-----------------------------------
SELECT ID, Field1 FROM BigTable
UNION ALL
SELECT DISTINCT 0, "<All>" From BigTable
ORDER BY Field1;
qr3UnionAllTop1:
-----------------------------------
SELECT ID, Field1 FROM BigTable
UNION ALL
SELECT Top 1 0, "<All>" From BigTable
ORDER BY Field1;
qr4UnionAllTop1Small:
-----------------------------------
SELECT ID, Field1 FROM BigTable
UNION ALL
SELECT Top 1 0, "<All>" From MSysObjects
ORDER BY Field1;
Module:
*****************************************************************
Sub test1()
Dim db As Database
Dim rs As DAO.Recordset
Dim qr As Long, i As Long, loops As Long
Dim t As Currency
Dim qrs
qrs = Array("qr1Union", "qr2UnionAllDistinct", "qr3UnionAllTop1",
"qr4UnionAllTop1Small")
Set db = CurrentDb
loops = 10
For qr = LBound(qrs) To UBound(qrs)
t = Timer
For i = 0 To loops
Set rs = db.OpenRecordset(qrs(qr))
rs.MoveLast
Next
Debug.Print qrs(qr), Timer - t
Next
Debug.Print String(30, "-")
For qr = UBound(qrs) To LBound(qrs) Step -1
t = Timer
For i = 0 To loops
Set rs = db.OpenRecordset(qrs(qr))
rs.MoveLast
Next
Debug.Print qrs(qr), Timer - t
Next
Debug.Print String(30, "-")
Debug.Print String(30, "-")
End Sub
******************** end of module *****************************
results:
no UnionAllTop1 UnionAllTopSmall UnionAllDistinct Union
---------------------------------------------------------
1 32,20 32,66 40,78 60,58
2 15,64 15,69 19,89 27,61
3 15,73 15,69 20,58 27,67
4 15,69 15,75 20,00 27,78
5 15,70 15,86 20,11 27,75
6 15,69 15,70 20,03 27,66
7 15,73 15,66 20,11 28,19
---------------------------------------------------------
Avg: 18,06 18,14 23,07 32,46
Avg: 100,00% 100,48% 127,78% 179,79%
As you can see, using your "simple" UNION is almost 2x slower than
UnionAllTop1.
Is it a big difference ?
Ep, it depends...
Discussion slightly academic (there are not such big combos), but IMHO
worth to know...