0

An MS Access query with a WHERE clause on a UNION query results in a table scan without using the indexes.

Example: There is a table like this:

CREATE TABLE tblFigures
(
    EntryDate DATETIME,
    AmountType NUMBER,
    Amount DOUBLE
);

CREATE INDEX idxAmountType ON tblFigures (AmountType)

If you run the following query and view the query plan in JetShowPlan's showplan.out file, you will see that the index is being used:

SELECT * FROM tblFigures
WHERE AmountType = 1;

If you now run the following UNION query, the query plan shows that a table scan is being performed instead of using the index. I suspect this is because the result set of a UNION query exists only virtually in memory and not in the database. Therefore, there are no indexes on the result set. (Maybe I'm wrong!?)

qryFiguresUnion looks like this:

SELECT * FROM tblFigures
UNION ALL
SELECT * FROM tblFigures

Run qryFiguresUnion with the WHERE clause:

SELECT * FROM qryFiguresUnion
WHERE AmountType = 1;

This poses a major performance problem for my application. For example, there are three amount types: 1, 2, and 3. In the subsequent calculation, the user has the option of performing the calculations based on one of the types or based on the sum of all three types. Therefore, I initially tried writing a UNION query based on tblFigures and a SELECT of the sums of the three types for each EntryDate, and using the UNION query as the record source for the subsequent calculations. This works, but is very, very slow due to the table scanning.

Another idea would be to calculate the sums and insert them into tblFigures, for example, with the amount type 4, and then use tblFigures as the data source for further calculations. I think this would be fast, since the JET engine uses the index. However, I'm not very satisfied with this solution because the data -> sum of the amount types is redundant.

Does anyone have a better idea?

Thanks in advance!

10
  • 2
    Don't put your technology in the title please... thats what tags are for. Commented Aug 25 at 20:53
  • 1
    Please include the table definitions (DDL, including indexes, etc), not descriptions in English. Add data and use cases to make a minimal reproducible example Commented Aug 25 at 21:13
  • 1
    I've read your question three times. I'm not at all clear as to what you're trying to do. This is why minimal reproducible example as so important. Perhaps also read : Why should I provide a Minimal Reproducible Example, even for a very simple SQL query? Commented Aug 25 at 21:20
  • 1
    What "subsequent calculations"? In one place you say there are 3 amount types then in another you reference amount type 4. What are you doing with this query result - report RecordSource? Build a report with table as RecordSource then use its Sorting & Grouping features with aggregate calcs in textboxes. Use VBA to build filter and apply to report: AmtType IN (CSV list of amount types). Commented Aug 25 at 21:49
  • 2
    BTW, the UNION you posted showing just a repetition of same SELECT line makes no sense. Commented Aug 25 at 22:37

1 Answer 1

1

There is no need for UNION query. Options:

  1. VBA and DAO modify query object to apply user-selected filter criteria

  2. dynamic parameterized query object with criteria like:

    WHERE AmountType LIKE IIf(Form!formname.comboboxname=4, "*", Form!formname.comboboxname)
    
  3. report design uses sorting & grouping features and calcs in textboxes, RecordSource uses dynamic query from option 2 or VBA builds filter criteria from user selection and applies to report when opening

  4. VBA and "temp" table to hold record IDs based on user selection and include in query to restrict dataset

Sign up to request clarification or add additional context in comments.

1 Comment

If the op is struggling with runtime being too high, option 2 is going to hurt too much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.