John,
I actually timed the difference between having an index on the hotel field
and not having it and it did make a big difference. This is however just one
of many queries that I am trying to speed up. The query that I copied below
just deals with one table. Will indexing work when you have multiple tables?
Here is an example where I use a "Translator Table" so that I can type
Arizona instead of PHX, TUS, etc....) The translator table allows me to do
groupings on my destination field. Here is the code.
------------------------------------------------
SELECT TOP 20 IIf([Origin]="FFP","LAND",[Origin]) AS Orig,
Sum(tblMaster.[TTL Price]) AS [Total Revenue], Sum(tblMaster.AWHRev) AS [AWH
Revenue], Sum(tblMaster.PAX) AS Passengers, Count(tblMaster.TRL) AS Packages,
Sum(tblMaster.Duration) AS [Room Nights]
FROM tblTranslator INNER JOIN tblMaster ON tblTranslator.DEST = tblMaster.DES2
WHERE (((tblMaster.GuaranteeYear)="2005") AND ((tblMaster.[Guarantee Date])
Between #1/1/2005# And Date()-1))
GROUP BY IIf([Origin]="FFP","LAND",[Origin]), tblTranslator.GROUP
HAVING (((tblTranslator.GROUP)=[Destination]))
ORDER BY Sum(tblMaster.AWHRev) DESC;
---------------------------------------------------------
I have 2 fields indexed, the DES2 field from tblMaster, the DES field from
tblTranslator.
Again, is it possible for indexing to work when linking tables?
John Vinson said:
Ok... you're calling an inefficient IIF function ON EVERY SINGLE ROW,
and sorting *by this function call*. Obviously a function cannot be
indexed. Might it be possible to run an Update query updating the FFP
values to LAND, or train the users that FFP *means* LAND? That will
make the query MUCH faster right there.
Sum(tblMaster.[TTL Price]) AS [Total Revenue], Sum(tblMaster.AWHRev) AS [AWH
Revenue], Sum(tblMaster.PAX) AS Passengers, Count(tblMaster.TRL) AS Packages,
Sum(tblMaster.Duration) AS [Room Nights]
FROM tblMaster
WHERE (((tblMaster.GuaranteeYear)="2005") AND ((tblMaster.[Guarantee Date])
Between #1/1/2005# And Date()-1) AND ((tblMaster.[Hotel Code])=[HCode]))
For use next year, or the year after, try
Between DateSerial(Year(Date()), 1, 1) AND Date() - 1
GROUP BY IIf([Origin]="FFP","LAND",[Origin])
ORDER BY Sum(tblMaster.AWHRev) DESC;
And you're ordering by a Sum field so that it must first run the
totals query, and *then* sort the results by a (again, non indexed)
field.
I have Indexes on the Hotel Code field.
Try putting an index on GuaranteeYear and GuaranteeDate as well, it'll
help. But this is still going to be slow. Getting rid of the IIF is
the biggest thing you can do though.
John W. Vinson[MVP]