Query Calculation...slow must be a better way

gremo

New member
Local time
Today, 04:02
Joined
Aug 2, 2016
Messages
8
My database setup is this. It pulls the part number, costs, price etc from our ERP and allow (via a form) to fill in the Bill of Materials for the particular part. All of the components are based off of combo boxes which are based on queries from the inventory master table. The form I designed works fine, after it opens. What I am trying to do is pull the cost for each component, and calculate the cost of the component (Qty * Cost). Then total up the cost of the part.

I have the calculations in a query. The work fine and calculate correctly. The problem is that It takes forever for the form to load and worse if I try to run the query with the calculations alone. I'm not talking about a few minutes, I'm talking 10 or 15 for it to initially run, if it runs at all.

I suspect that all of the "testing for null" with NZ and or IIF is causing part of the ruckus, but I'm not sure how to get around that as some of the fields will be null.
Any suggestions on how to set this up to run faster, or at least fast enough that we can use it.
 
(Qty * Cost) should take zero time. why would you need IIF?
 
QTy * cost does take 0 time. It's the grand total that I use the IIF.
Part 1 qty * cost =
Part 2 no cost
part 3 qty * cost=
grand total= tests for null values returns 0 if null then calculates the total
 
Do you happen to be sorting on a calculated field? That can really put the brakes on a query.

It's strange that it's taking so long for the form to load. I thought Record Source queries just retrieved what they needed to display the form, i,e., a query might have count of a million but it doesn't retrieve all of them on form open.

I'd be interested in how long the query takes to get let's say 100 records. Would you mind making a copy of the Record Source query of the form and modifying the SQL. Put TOP 100 after the after the SELECT keyword and see how long that takes to run.
 
I tried 25 records earlier and can't get that to run either. Why would the form open (eventually) but the query will not. I've waited nearly an hour.

If the form will open and show the calculations correctly, why won't the query itself :banghead:open?
 
Last edited:
I suggest you make a copy of your database, strip it down if necessary ,zip and upload your database so that we can take a look.
 
See attached. One of the files is a linked file to our ERP but you should be able to see the queries and how they are built.
 

Attachments

Your data structure is completely wrong. You need to study the principles of relational data structures then start again.

Your queries are slow because the design has all the arithmetic being done in the form instead of the database engine.
 
I figured as much... Galaxiom I put the calculations in a query....can you explain how they are being performed in the form rather than the query?
 
I have not looked that closely. If you structure it correctly the summation is done as an aggregate on a single field by the database engine. Your structure appears to have it calculating with data pulled from all over the place.
 
That's what I need to do. Pull data from several different tables into one part. Any quick suggestions on how to accomplish that?
 
The current database is structured somewhat horizontally, a structure commonly seen in spreadsheets, but one that just doesn’t work well in relational databases. In this post I intend to demonstrate how you could restructure the database to get it in normal form. I will be referring to the attached database where I normalized the assembly labor costs as an example. This need to be repeated for each of the problem areas which are those where you see repeating groups such as Inventory Components and Components.

The repeating groups need to be organized into to single tables. For the assembly costs I first created a query for each of the six groups (qryAssembly1 – qryAssembly6) in the attached database. These are identical in structure and just pull the data from six repeating sections in the BOM table. I also included in each query an expression which identifies the group it came from. I first didn’t think this would be necessary, but in a few cases the assembly type is repeated for the same part with different times. More about this later. To get the data into a single table the data from these queries needs to be stacked together vertically. We can do this with a UNION query.

Access doesn’t do much for you when it comes to UNION queries. When you create a UNION query with the Query Designer all it gives you is a blank area in which you can copy and paste in the SQL. So for each of the queries (qryAssembly1 – qryAssembly6) I opened them in design view, switch to SQL view, copied the SQL, switched to the UNION query and pasted the SQL. Then I put UNION ALL between these SELECT queries. The finally result is qryAssemblyUnion. You can use this UNION query as a source in make table query and create this table. In the attached database I name this table ASSEMBLY.

If you look at the contents of ASSEMBLY you will see that it only has 1598 records. This highlights the advantage to having a proper structure. In the existing structure there are six groups of these fields for each of the 1339 records in the BOM table. That means 6 times 1339 or 8034 calculations for this horizontal structure versus 1598 for the proper vertical structure.

Now please look at Relationships under the DATABASE tab. This shows how this new table is related to the BOM2 table and the LABOR table. Note that BOM2 is just the BOM table with the fields that were moved to the ASSEMBLY table removed.

qryAssemblyLabor is a query which joins the ASSEMBLY and LABOR tables and computes the Assembly Cost. This is the record source for the AssemblyLaborSub form. This is used in the subform of the BOM2 form.

Please open the BOM2 form. This shows how a one to many relationship such as BOM to ASSEMBLY is typically set up. The first ten records in the BOM2 table don’t have any related assemblies so you will have to scroll through some records before you will any data there but please use the Go To Part combo box to go to part 1422. This part demonstrates why I included the Assembly No in the table. There seem to be some meaning assigned to the positions as the ASSEMBLY type appears twice with different times. This is a design question for you. How do you want to distinguish duplicate assemble types for the same part? Maybe there is something better than just a number or can the duplicates be combined.

The BOM2 form also includes the sum of the assembly labor costs from the subform. This was added with the instructions from this site.

Getting the database structure correct will help a lot in solving this slow query. Aside from reducing the computations this demo reduced the number of joins by five. These numerous join are also slowing down your query.

I suggest you read up on relational database design and normalization. Just google these subjects. You will need to know the basics of this to make sure your design is correct.
 

Attachments

Thanks for the info. I've already started redesigning this.... after it's done and set up correct is there a way to get the data that has been entered into the new database?
 
Thanks for the info. I've already started redesigning this.... after it's done and set up correct is there a way to get the data that has been entered into the new database?
That was one of the major points of my post. It showed how to move the data to the new structure. Sorry you didn't understand it. Can you tell me where you got lost?
 
After I posted the stupid question, I realized that..... sorry
 

Users who are viewing this thread

Back
Top Bottom