2012-09-15 55 views
1

我編寫了一個在Access數據庫中運行的VBA腳本。該腳本在各種表格上查找值,並根據值的組合將屬性分配給主表格。模擬MS Access中的多線程(VBA)

該腳本按預期工作,但是,我正在處理數百萬條記錄,因此需要很長的時間。

我想將進程分解成更小的部分,並在獨立的線程上同時運行腳本。

之前,我開始試圖建立一個解決方案,我想知道:根據您的經驗

  1. ,這會提高性能?或者這個過程會持續多久?
  2. 我正在尋找使用Powershell或VBScript來實現這一點。有什麼障礙需要注意?

請注意:由於客戶端會運行,我不得不使用Access作爲後端,如果我使用Powershell,它必須是版本1.0。

我知道這些都是非常含糊的問題,但任何基於以往經驗的反饋意見。由於

+3

首先判斷任務是CPU綁定還是IO綁定。如果他們是CPU綁定的,多線程可能會有所幫助(但只有當機器有多個核心時,顯然)。如果它們是IO綁定的,那麼在更多的內核上運行並沒有太大的區別 - 除了線程管理的額外開銷之外。 – driis

+3

我對這個問題的經驗是,通過修復SQL和表結構,你可以加快速度。通過使用csv導入而不是SQL插入來編寫新記錄也會極大地提高速度。 – Pynner

+1

也許你應該按照Pynner的建議寫出CSV或表的更新,然後將所有更新作爲單個查詢運行。 – Fionnuala

回答

0

只是想回來後我在這最後的解決方案......

我嘗試以下方法來分配基於對60,000記錄樣本來自其他表值的組合到主表中的屬性大小:

Solution 1: Used a combination of SQL queries and FSO Dictionary objects to assign attribute 
Result: 60+ minutes to update 60,000 records 

Solution 2: Ran script from Solution 1 concurrently from 3 separate instances of Excel 
Result: CPU was maxed out (Instance 1 - 50% of CPU, Instances 2 and 3 - 25% each); stopped the code after an hour since it wasn't a viable solution 

Solution 3: Tried using SQL UPDATE queries to update main table with the attribute 
Result: This failed because apparently Access does not allow for a join on an UPDATE sub-query (or I just stink at writing SQL) 

Solution 4 (Best Result): Selected all records from main table that matched the criteria for each attribute, 
output the records into csv and assigned the attribute to all records in the csv file. 
This created a separate file for each attribute, all in the same format. I then 
imported and appended all of the records from the csv files into a new main table. 
Result: 2.5 minutes to update 60,000 records 

特別感謝Pynner和Remou建議將數據寫入csv。

我從來不會想到這是用屬性更新記錄的最快方法。如果你沒有提出這個建議,我可能會放棄這個項目,認爲用Access和VBA完成是不可能的。非常感謝你分享你的智慧!