Results 1 to 8 of 8
  1. #1
    pio92 is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    19

    Simpy query take 10 minutes for execute -

    I have a simply query in MS Access:



    SELECT *
    FROM D1
    WHERE D1.ID NOT IN (SELECT cID FROM D2)

    D1 is a query with one column (ID) and contain ca. 2100 records.
    D2 is a query with one column (cID) and contain ca. 300 records.

    Why my query need ca. 10 minutes for execute ?
    How can I optimize it ?

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    It would be better to use an left join rather than a sub query

    SELECT D1.*
    FROM D1 LEFT JOIN D2 On D2.cID = D1.ID
    WHERE D2.cID IS NULL

    Air code untested
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    perhaps your D1 and D2 queries are not optimised - but agree with Minty's suggestion

  4. #4
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    I agree with the suggestion in post #2. That is what you get using an unmatched query wizard.
    Subqueries are always slow and should be avoided if other methods are available.

    Even so, it shouldn't take more than a few seconds to run your query. Are both fields indexed?

    For more general advice on optimising queries, see my article Speed Comparison Tests 8 (isladogs.co.uk)
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  5. #5
    Join Date
    Apr 2017
    Posts
    1,792
    No way so slow work of so simple query from so small tables is caused by design only. Probably the reason is outside of Access (antivirus, network connection problems, etc.)

    Some 20 years ago I had a similar situation (a query from table with subquery from another table), and the reason was antivirus, which did make full scan of of both tables for every row of main table. We switched the antivirus off, and instead of 20 minutes the query finished in couple of seconds.

  6. #6
    pio92 is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    19
    "It would be better to use an left join rather than a sub query

    SELECT D1.*
    FROM D1 LEFT JOIN D2 On D2.cID = D1.ID
    WHERE D2.cID IS NULL

    Air code untested"


    Thank you very much, now its take only 5 seconds for execute.

  7. #7
    pio92 is offline Novice
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    19
    BTW.
    I have two queries more they are very slow too.

    UPDATE temp SET F10 = DLookUp("scanned","4b","[4a.ID] =" & temp.ID)

    is here a way for optimalize ?

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Again Join the data and use appropriate criteria, something like

    UPDATE temp
    Inner Join 4B ON 4B.ID = Temp.ID
    Set F10 = 4b.Scanned

    Again air code untested

    You really should give your tables and fields meaningful names, if you revisit this in 6 months you wouldn't have clue what it was doing.
    ID of what ? As you can see having all your Autonumber fields called ID makes this difficult to follow the logic.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-23-2019, 02:35 PM
  2. RunSQL vs. QueryDef.Execute vs. CurrentDB.Execute
    By caubetiep1337 in forum Queries
    Replies: 8
    Last Post: 12-16-2015, 05:35 PM
  3. Replies: 10
    Last Post: 02-27-2015, 11:28 AM
  4. Replies: 4
    Last Post: 06-12-2013, 10:20 AM
  5. slow query when asking for sum of minutes
    By sf827 in forum Queries
    Replies: 13
    Last Post: 01-04-2012, 09:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums