Results 1 to 10 of 10
  1. #1
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35

    Question Need help in query optimization when updating column values

    Hi,


    Below is the scenario.
    I am tyring to updates the column values in my access table by matching the ID from SQL tables. If it matches pull records from table 1 or else table 2
    Somehow this query is taking 10 mins to refresh 440 records into the access table.
    AccessTable A has only 1 column (col_pk) while I'm tyring to match the same column from other tables, and if it matches then add new column and its respective records.

    Do I have any levy of optimizing the query?

    Code:
    strSQL = "SELECT * FROM AccessTable A;"
    '440 records currently
    X.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    
    
    Do While X.EOF = False
    
    
    strSQL = "SELECT Col1, Col12, NAME AS Col13, Col14, Col15 FROM dbo.SQL_Table WHERE REP_ID = '" & X("Col_PK") & "';"
    ' 
    Y.Open strSQL, DC.BODS_REP, adOpenKeyset, adLockOptimistic
    
    
    If Y.RecordCount <> 0 Then
    X("Col1") = Y("Col1")
    X("Col12") = Y("Col12")
    X("Col13") = Y("Col13")
    X("Col14") = Y("Col14")
    X("Col15") = Y("Col15")
    X.Update
    Y.Close
    
    
    
    
    Else
    Y.Close
    
    
    strSQL = "SELECT MgrID AS Col1, RepStatus As Col12, RepType As Col14 from dbo.SQL_Table2 WHERE RepID = '" & X("Col_PK") & "';"
    Y.Open strSQL, DC.BODS_REP, adOpenKeyset, adLockOptimistic
    X("Col1") = Y("Col1")
    X("Col12") = Y("Col12")
    X("Col14") = Y("Col14")
    X.Update
    Y.Close
    
    
    End If
    X.MoveNext
    Loop
    X.Close
    Record count (SQL_Table)-128,813
    Record count (SQL_Table2)-204,218
    I think it is a simple query. nothing complex going on. But any ideas/input would be appreciated.

    Thanks!

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Not queries though, but recordsets, which I think they call RBAR (row by agonising row)
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35
    Sorry, I didnt understand your response. Would you mind elaborating?

    Quote Originally Posted by Welshgasman View Post
    Not queries though, but recordsets, which I think they call RBAR (row by agonising row)

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You are not using queries directly but via recordsets, which are generally slower in a great deal.

    A proper Update query would be in this syntax
    https://learn.microsoft.com/en-us/of...oft-access-sql
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35
    Quick clarification:
    in thebelow code, there is only 1 column, Col_PK
    Code:
    SELECT * FROM AccessTable A;"
    then based on the matching of Col_pk with REP_ID, I will be inserting new columns and records associated with it.

    Would the update syntax still be applicable in this case?

    Quote Originally Posted by Welshgasman View Post
    You are not using queries directly but via recordsets, which are generally slower in a great deal.

    A proper Update query would be in this syntax
    https://learn.microsoft.com/en-us/of...oft-access-sql

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well I have never been a query expert, I have to experiment, but you seem to want to update from table1 if it has matching records in A, else table2.
    Could you have data in both for an id?
    Why select * if you are just using one field? That would slow it down as well.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    usertest is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2023
    Posts
    35
    yes correct. If ID matches with Table 1 then add those new columns from Table 1 into Table A else it matches with Table 2 and add those columns into Table A.

    The select * is basically select ID from table.
    There is only 1 column in table A. I think it doesnt matter if I do select * or that column name

    Quote Originally Posted by Welshgasman View Post
    Well I have never been a query expert, I have to experiment, but you seem to want to update from table1 if it has matching records in A, else table2.
    Could you have data in both for an id?
    Why select * if you are just using one field? That would slow it down as well.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well if the ID can only be in one or the other, I might just run two queries regardless, knowing one will never produce any data, as opposed to checking which table to use?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    zpy2 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2023
    Posts
    4

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You might want to look at what's commonly referred to as an "upsert" query, although it's not an official Access thing.
    What I have to wonder about is the notion of adding columns in such a manner. It indicates a poorly normalized db, and that is the cause for many db issues, including slowness. That assumes there is nothing wrong with your network load at the time you run that code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 3
    Last Post: 05-14-2020, 04:11 PM
  2. Union query optimization and form criteria
    By tbrown1110 in forum Queries
    Replies: 8
    Last Post: 01-24-2019, 02:27 AM
  3. Replies: 4
    Last Post: 12-29-2015, 03:25 PM
  4. Replies: 14
    Last Post: 07-13-2015, 12:47 PM
  5. Query Optimization Inquiry
    By Nobody in forum Queries
    Replies: 1
    Last Post: 07-29-2010, 08:53 AM

Tags for this Thread

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