I am performing a large number of insert operations using VBA - Access. the VBA code is in a file "control.accdb" performing operations on "data.accdb" Few tables in the process are created from the VBA , few remain as such.
After finishing Phase 1 of the process , I do a Compact And Repair either manually or from DBEngine.Compact VBA code. The second phase (same code - next iteration) becomes very very slow.
Here is the Insert Code :
SQL = "INSERT INTO ExpectedResult " _
& "( DLID, NumRows, Total, SubjectBlock, NextSubjectBlockSeq ) " _
& "SELECT INVDL.DLID, 1 AS Expr1, INVDL.Amount, " _
& "INVDL.SubjectBlock,INVDL.SubjectBlockSeq+1 AS Expr2 " _
& "FROM INVDL where SubjectBlock > 0;"
cdb.Execute (SQL)
TargetNumRows = 2
Do While TargetNumRows < MaxSubjectBlockSeq + 1
Set qdf = cdb.QueryDefs("pq_appendToExpectedResult")
qdf!TargetNumRows = TargetNumRows '' parameter value
qdf.Execute
TargetNumRows = TargetNumRows + 1
Set qdf = Nothing
Loop
where pq_appendToExpectedResult :
PARAMETERS TargetNumRows Long;
INSERT INTO ExpectedResult _
( DLID, NumRows, Total, SubjectBlock, NextSubjectBlockSeq ) _
SELECT INVDL.DLID+1-[TargetNumRows], [TargetNumRows] AS Expr1, _
[ExpectedResult].[Total]+[INVDL].[Amount] AS NewTotal, _
INVDL.SubjectBlock, [INVDL].[SubjectBlockSeq]+1 AS Expr2 _
FROM INVDL INNER JOIN ExpectedResult _
ON (INVDL.SubjectBlock = ExpectedResult.SubjectBlock) _
AND (INVDL.SubjectBlockSeq = ExpectedResult.NextSubjectBlockSeq) _
WHERE (((INVDL.SubjectBlockSeq)>=[TargetNumRows]) _
AND (ExpectedResult.NumRows=[TargetNumRows]-1));
To the best of my knowledge , all indexes in INVDL are untouched. I drop the table ExpectedResult and recreate it with indexes after each Phase (iteration). Other tables - dont think will make any difference.