Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2011
    Posts
    18

    Need help deleting large records by chunk

    I have a table that is over 50000. When I do a delete query it takes a while, is there a way that I can delete records by 1000 at a time?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Is there a unique identifier field such as an autonumber? Apply filter criteria.

    Is deleting 50000 records a regular process - why?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Aug 2011
    Posts
    18
    Quote Originally Posted by June7 View Post
    Is there a unique identifier field such as an autonumber? Apply filter criteria.

    Is deleting 50000 records a regular process - why?
    It is a daily process, new records are added every day and information changes every day.

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I find running it from a Macro and suppressing the warnings goes much faster. I used the RunSQL action, i.e.
    Code:
    DELETE [TableName].* FROM [TableName];
    Note that if you are deleting that many records every day, be sure to Compact and Repair your database often so the size does not get bloated and you run out of space.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Or in VBA:

    CurrentDb.Execute "DELETE FROM [TableName]"

    Since entire record is deleted, there is no need to specify fields, not even with wildcard.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Join Date
    Aug 2011
    Posts
    18
    Quote Originally Posted by June7 View Post
    Or in VBA:

    CurrentDb.Execute "DELETE FROM [TableName]"

    Since entire record is deleted, there is no need to specify fields, not even with wildcard.
    How would I delete 2000 records at a time?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Is there a unique identifier? An autonumber field might serve. Something like:

    intRecs = DCount("*", "tablename")
    If intRecs>0 Then
    dblGrps = intRecs / 2000
    dblGrps = dblGrps + IIf(dblGrps - Int(dblGrps) > 0, 1, 0)
    x = 1
    For n = 1 to dblGrps
    CurrentDb.Execute "DELETE FROM [TableName] WHERE ID BETWEEN " & x & " AND " & x + 1999
    x = x + 2000
    Next
    End If

    Really should figure out why delete takes so long. I just tested deleting over 66900 records and it was faster than a blink. Is your database split and backend on a file server?
    Last edited by June7; 03-05-2015 at 11:13 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Join Date
    Aug 2011
    Posts
    18
    Quote Originally Posted by June7 View Post
    Is there a unique identifier? An autonumber field might serve. Something like:

    intRecs = DCount("*", "tablename")
    If intRecs>0 Then
    dblGrps = intRecs / 2000
    dblGrps = dblGrps + IIf(dblGrps - Int(dblGrps) > 0, 1, 0)
    x = 1
    For n = 1 to dblGrps
    CurrentDb.Execute "DELETE FROM [TableName] WHERE ID BETWEEN " & x & " AND " & x + 1999
    x = x + 2000
    Next
    End If

    Really should figure out why delete takes so long. I just tested deleting over 66900 records and it was faster than a blink. If your database split and backend on a file server?
    Yes, my database is split. The backend is on sharepoint. When I manually delete, select a number of rows, it's quicker than selecting all rows and then deleting it.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Never used SharePoint. Guess that explains why slow.

    Another possible option for the delete SQL.

    DELETE FROM TableName WHERE [uniqueID field] IN (SELECT TOP 2000 [uniqueID field] FROM TableName)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Join Date
    Aug 2011
    Posts
    18
    How can the code below be translated into VBA?

    Code from: https://www.virtualobjectives.com.au...arge_table.htm

    -- Deleting records from a large table.

    DECLARE @ROWCNT INT , @NUMROWS INT, @TOPNUM INT
    SET @ROWCNT = 0
    SET @NUMROWS = 0
    SET @TOPNUM = 100000 --<--- Set this to the number of rows to delete per batch.

    WHILE 1 = 1
    BEGIN
    PRINT CONVERT(varchar(19), GETDATE(), 120) + ' Start'

    DELETE TOP (@TOPNUM)
    FROM YourTable
    WHERE SomeField = xxx

    SET @NUMROWS = @@ROWCOUNT
    PRINT 'Deleting ' + STR(@NUMROWS) + ' records.'
    SET @ROWCNT = @ROWCNT + @NUMROWS
    IF @NUMROWS = 0 BREAK
    PRINT CONVERT(varchar(19), GETDATE(), 120) + ' Finished.'

    -- A delay will help prevent locking/blocking if required.
    -- WAITFOR DELAY '00:00:03'
    END
    PRINT 'TOTAL: ' + STR(@ROWCNT) + ' Records Deleted.'

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Only way i know to calc a row counter is with DCount()
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Loop skips a large number of records unless stepped through
    By Monterey_Manzer in forum Programming
    Replies: 1
    Last Post: 10-04-2013, 05:01 PM
  2. Deleting records on a form using VBA
    By lawdy in forum Programming
    Replies: 5
    Last Post: 05-06-2013, 06:06 PM
  3. Query speed over a large number of records
    By GrantRawlinson in forum Queries
    Replies: 2
    Last Post: 06-15-2012, 11:03 AM
  4. deleting records
    By radicalrik in forum Queries
    Replies: 2
    Last Post: 07-14-2010, 03:10 PM
  5. deleting records off a subform
    By jamin14 in forum Programming
    Replies: 10
    Last Post: 04-22-2010, 08:47 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