Query - Running slow - Advice needed (1 Viewer)

Ceebee86

Registered User.
Local time
Today, 03:54
Joined
Sep 9, 2019
Messages
25
Hi Access world,

I have a complicated query (well it is to me! :) ) that I have written with some help along the way. I want to stream line it, and I am quite new to access, been using it for about 3 months or so.

It's purpose is to provide an overview of information in continuous forms, that displays pre advised deliveries that are due to arrive, it then unions this against another query that displays deliveries that have arrived and are in the warehouse.

These two queries that are union'd take their information from other queries and I believe the correct term for this is 'stacked queries' ?

The code for my query is below. Takes about 2.5 secs to load if i count out loud. How do I measure this through access?

Also, on the form itself, I have a few textboxes that have Dlookup's to just look up info, and a few IIf statements.

Also, I have some code for the On Load event of the form, which doesn't seem to work 100% of the time (it positions the form to a certain position.) If this fails, all the Dlookup's and IIf statements fail, and show the #Name? error. So I'm assuming this is to do with the VBA not working, as when I load the form on its own, I get the error 'The expression On Load you entered as the event property setting produced the following error: Return without GoSub.' Is this a question for another topic?

Any help, advice I received, I am very grateful in advance. :)

Query Code:

Code:
SELECT [AGLGRNextended].[AGL99], [AGLGRNextended].CODE,[AGLGRNextended].[AGLGRN],[AGLGRNextended].[GS1SKU],[AGLGRNextended].[Balance], [AGLGRNextended].[GRNID],[AGLGRNextended].[GRN], [AGLGRNextended].[CUSTOMERPO], [AGLGRNextended].[SUPPLIERPO], [AGLGRNextended].[COMPANYID], [AGLGRNextended].[SCANINID], [AGLGRNextended].[Packages],[AGLGRNextended].[Location], [AGLGRNextended].[ProdAGLGRN], [AGLGRNextended].[GROSSWGT], [AGLGRNextended].[NETWGT], [AGLGRNextended].[CBM],[AGLGRNextended].[PROD], [AGLGRNextended].[BBD], [AGLGRNextended].[GRNDATE], ([tblPRODUCT].[productSUPPLIERSKU]&" - "&[tblPRODUCT].[productDESCRIPTION]) AS Description, tblPRODUCT.[productSUPPLIERNAME], tblAGLGRN.[aglgrnCREATED], tblSCANIN.[scaninUSER], tblSCANIN.[scaninUSERTIME], tblUser.[UserName], tblUser.[UserFIRSTNAME], Val(AGLGRNextended.[PROD]) AS PROD1, Format(tblAGLGRN.aglgrnDATEEXPECTED,'Short Date') AS DateExpected, tblAGLGRN.aglgrnUSER AS AGLGRNUSER,tblUser.UserFIRSTNAME AS AGLGRNFIRSTNAMEUSER, 2 AS DeliveryType, tblAGLGRN.aglgrnADVISEDQTY
FROM ((((((AGLGRNextended LEFT JOIN tblPRODUCT ON [tblPRODUCT].[productGS1SKU]=[AGLGRNextended].[GS1SKU]) LEFT JOIN tblAGLGRN ON [AGLGRNextended].[AGLGRN]=Format(tblAGLGRN.AGLGRN,'00000')) LEFT JOIN tblSCANIN ON [tblSCANIN].[ID]=[AGLGRNextended].[SCANINID]) LEFT JOIN tblUser ON [tblUser].[UserLogin]=[tblAGLGRN].[aglgrnUSER])) LEFT JOIN SupplierAndCustomerRefs ON Format([SupplierAndCustomerRefs].[aglgrnID],'00000') = Format(tblAGLGRN.AGLGRN,'00000')) LEFT JOIN OnHoldSSCCqry ON Format([OnHoldSSCCqry].[aglGRNID],'00000') = [AGLGRNextended].[AGLGRN]


UNION SELECT Null AS Field2, tblCOMPANYDIRECTORY.companyCODE AS Field3, Format(tblAGLGRN.AGLGRN,'00000') AS AGLGRN, Null AS Field4, Null AS Field5, Null AS Field6, Null AS Field7, tblAGLGRN.aglgrnCUSTOMERREF,tblAGLGRN.aglgrnSUPPLIERREF,tblAGLGRN.companyID, Null AS Field9, PackageCountqry.PackageQty AS Packages, Null AS Field10, Null AS Field11, Null AS Field12, Null AS Field13, Null AS Field14, Null AS Field15, Null AS Field16, Null AS Field17, Null AS Field18, tblAGLGRN.aglgrnSUPPLIER AS Field19, tblAGLGRN.aglgrnCREATED AS Field20, Null AS Field21, Null AS Field22, Null AS Field23, Null AS Field24, Null AS Field25, tblAGLGRN.aglgrnDATEEXPECTED AS DateExpected, tblAGLGRN.aglgrnUSER AS Field26, tblUser.UserFIRSTNAME AS AGLGRNFIRSTNAMEUSER, 1 AS DeliveryType, tblAGLGRN.aglgrnADVISEDQTY
FROM (((((tblAGLGRN LEFT JOIN GS1DECODEextended ON Format(tblAGLGRN.AGLGRN,'00000') = Format(GS1DECODEextended.[AGL GRN],'00000')) LEFT JOIN tblPACKAGES ON Format(tblAGLGRN.AGLGRN,'00000') = Format(tblPACKAGES.aglgrnID,'00000')) LEFT JOIN PackageCountqry ON Format(tblAGLGRN.AGLGRN,'00000') = Format(PackageCountqry.AGLGRNPACKAGE,'00000')) LEFT JOIN tblUser ON tblUser.UserLogin = tblAGLGRN.aglgrnUSER) LEFT JOIN tblCOMPANYDIRECTORY ON tblAGLGRN.companyID = tblCOMPANYDIRECTORY.ID) LEFT JOIN SupplierAndCustomerRefs ON Format([SupplierAndCustomerRefs].[aglgrnID],'00000') = Format(tblAGLGRN.AGLGRN,'00000')
WHERE Format(tblAGLGRN.AGLGRN,'00000') <> Format(GS1DECODEextended.[AGL GRN],'00000')
ORDER BY AGLGRNextended.[AGLGRN] DESC;
 
Last edited by a moderator:

kevlray

Registered User.
Local time
Today, 03:54
Joined
Apr 5, 2010
Messages
1,046
First off, I try to avoid left joins unless I am expecting NULL values in the right table. It will affect performance a bit. I am not sure how much the FORMAT statement on a number of joins affects performance.


Then performance comes down to a number of factors that are hard to nail down. What indexes do you have (whenever possible, join on a index field). How big are the tables. The number of columns in the table. Network speed. Those are a few of the things I can think of.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:54
Joined
Jan 20, 2009
Messages
12,851
Applying a function in the join will require every record to be processed and cannot use the indexes.

BTW For the same reason, always avoid using functions in the SELECT criteria.
 

Ceebee86

Registered User.
Local time
Today, 03:54
Joined
Sep 9, 2019
Messages
25
Thanks for the replies.

I do use quite a few functions in my select queries, I'll see if I can cut a few of these out.

I imagine the data tables they are based on will get quite big in the future, the tables have about 5-10 columns in each table on average with the last query having around 30. I will look at cutting this down, and I'll take a closer look at the indexes that they are joining on. :)
 

Minty

AWF VIP
Local time
Today, 11:54
Joined
Jul 26, 2013
Messages
10,368
In addition to the other recommendations, I would have thought you could apply most of that formatting after the union had been done to avoid formatting every record?

That may well simplify/speed up your joins as well as you can't have an index working on a formatted field join.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:54
Joined
Feb 19, 2013
Messages
16,607
as others have said, formatting in your where clause precludes the use of indexes

For example why do you have this

WHERE Format(tblAGLGRN.AGLGRN,'00000') <> Format(GS1DECODEextended.[AGL GRN],'00000')

when

WHERE tblAGLGRN.AGLGRN <> GS1DECODEextended.[AGL GRN]

should work just as well. And providing the fields are indexed, should see a performance improvement
 

RogerCooper

Registered User.
Local time
Today, 03:54
Joined
Jul 30, 2014
Messages
283
Union Select Queries are always slow, significantly slower than running the 2 queries separately.

Could you have a single delivery schedule table, and then update the table when the delivery is received?
 

Users who are viewing this thread

Top Bottom