简体   繁体   中英

MS Access SQL performance issues INNER JOIN + Update data

I'm using 2 tables:

First as main table that keeps data

Second one is a temp table to import new report everyday and check the difference between records in new report and main table.

        tablename="Temp"
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel10, tablename,strPath, True, "A:CH"  
        stringSQL = 
        UPDATE 
        Main INNER JOIN Temp ON Main.[PackageNumber] = Temp.[PackageNumber]
        SET 
        Main.[Field1]=Temp.[Field1],
        Main.[Field2]=Temp.[Field2] ... 

If in temp table i can find record with already existing package number I have to update the entire row in main table with data from temp table. There is about 30 columns in main table and about few thousands of records to check in temp report everyday.

Currently i'm meeting performance issues, cause whole operation can take even more than hour! What are the possibilities to make it run faster? I've already tried "Repair and compact db" function.

You need to create an index on the JOIN column of the imported table. I assume it's the primary key.

After running DoCmd.TransferSpreadsheet , do this:

CurrentDb.Execute "CREATE UNIQUE INDEX PrimaryKey ON Temp (PackageNumber) WITH PRIMARY"

Your Main table should already have PackageNumber as primary key.

You can query Excel files directly, avoiding the creation of a temp table, and any overhead associated with it:

UPDATE 
    Main INNER JOIN [Excel 10.0;HDR=Yes;DATABASE=C:\YourFile.xls].[A:CH] Temp ON Main.[PackageNumber] = Temp.[PackageNumber]
    SET 
    Main.[Field1]=Temp.[Field1],
    Main.[Field2]=Temp.[Field2] ...

This tends to work way faster than DoCmd.TransferSpreadsheet , which should be avoided when possible in my opinion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM