i’ve got this union query:
(SELECT INSTALLER, INSTALLTIME, RESULT, JOBNUMBER, HONAME, ADDRESS, CITY, STATE, ZIP, NOTES, SMNOTES, '' as priority, PAFS, upsell, TERM, MMRUPGRADE, WARRANTY, EFT FROM ACCOUNTS
WHERE INSTALLDATE = '$date' && FUNDINGSTATUS !='DEAD')
UNION
(SELECT technician, servicetime, result, ID, Customername, address, city, state, zip, notes, board, priority, '', '', '', '', '', '' FROM service
WHERE serviceday = '$date')
ORDER BY INSTALLER, priority
i’m curious if putting an index on the date field will help speed up both queries? or will the fact that i use FUNDINGSTATUS in the first where clause will make that query not utilize the index?
Answering your very question:
If the condition on
installdateandservicedayis selective (that is few rows satisfy it), then yes, it will help.Date fields usually tend to be selective.
Yes, the index will still be used.
The engine will use the index to select only the records with
installdate = $dateand the will additionally filter on the value offundingstatus.For best results, create the following indexes:
If
DEADis a frequent value forfundingstatus, it may be better to rewrite this query like this:so that the range access on both fields
(installdate, fundingstatus)can be used.