matley
matley

Reputation: 99

Optimize or simplify MS Access query

I have Access query which really takes time to execute. Right now, I had to go through to at least four queries to get the result I want which is Top 4 percentage by group. Here are the queries.

  1. Query1 - Add calculated field which is column3.
  2. Query2 - Add cumulative field for column3.
  3. Query3 - Add calculated fields in which I used column3 for calculation. calcfield1, calcfield2, calcfield3 combined them in one column (NewColumn) using UNION query. Sum the NewColumn and grouped them by GroupID.
  4. Query4 - Selects the Top 4 values in NewColumn by GroupID.

Sample Output (Top 4 values of NewColumn by GroupID):

Group1 1.45
Group1 1.00
Group1 0.45
Group1 0.20
Group2 8.20
Group2 4.48
Group2 3.44
Group2 2.00

When I run Query4, it takes a while to execute (at least 10 mins.). The queries has to return all record because of the cumulative percentage calculation.

I tried HAVING clause in Query3 so that rows in Query4 would be minimized when I select the Top 4 but I couldn't get it worked. I also tried getting the Top 4 in Query2 so that succeeding query has less rows to work with but the query takes more time that I have to break the execution of query.

I'd like to simplify or at least optimize the query.

Table Structure:

 Table1:
 ID - Autonumber
 GroupID
 Tip1
 Tip2
 Tip3
 Tip4

Sample data Table1:
GroupID    Tip1      Tip2        Tip3      Tip4
171        1          5           4          8
172        2          7           5          3
173        8          16          10          

Table2:
ID - Autonumber
GroupID
Result1
Result2
Result3

Sample data Table2:
GroupID    Result1  Result2     Result3
171        1          5           4    
172        12         7           5    
173        8          6           3   

Table1 and Table2 are joined using GroupID

Upvotes: 1

Views: 1409

Answers (1)

PowerUser
PowerUser

Reputation: 11801

I had a similar situation once where I had to rank data through a complicated self-join. It was very slow until I first dumped the data into a temp table and then ran update queries on it. Still slow, but not nearly as bad.

Perhaps you could use a staging table instead of a series of joined queries? Then #1,#2, and #3 could be update queries that act on that staging table.

Upvotes: 1

Related Questions