年後,我仍然有同樣的問題,但索引和查詢這個問題的力量只是稍微痛苦,這取決於你的表的大小。通過使用readWhere,或getListWhere的,我認爲這個問題是大約爲O(n)
這裏是我做過什麼...... 1.我創建了一個表,有兩個indicies..you 可以使用多個indicies在PyTables:
http://pytables.github.com/usersguide/optimization.html#indexed-searches
一旦你的表是indexed,我也用LZO壓縮你可以做到以下幾點:
import tables
h5f = tables.openFile('filename.h5')
tbl = h5f.getNode('/data','data_table') # assumes group data and table data_table
counter += 0
for row in tbl:
ts = row['date'] # timestamp (ts) or date
uid = row['userID']
query = '(date == %d) & (userID == "%s")' % (ts, uid)
result = tbl.readWhere(query)
if len(result) > 1:
# Do something here
pass
counter += 1
if counter % 1000 == 0: print '%d rows processed'
現在我在這裏寫的代碼實際上很慢。我確信有一些PyTables專家可以給你一個更好的答案。但這裏是我的想法對性能:
如果你知道你開始用乾淨的數據,即(不重複),那麼所有你需要做的就是查詢表,一旦你有興趣在尋找的鑰匙,這意味着你只需要做到:
ts = row['date'] # timestamp (ts) or date
uid = row['userID']
query = '(date == %d) & (userID == "%s")' % (ts, uid)
result = tbl.getListWhere(query)
if len(result) == 0:
# key pair is not in table
# do what you were going to do
pass
elif len(result) > 1:
# Do something here, like get a handle to the row and update instead of append.
pass
如果你有大量的時間去重複檢查已經創建了一個後臺進程,用您的文件搜索目錄並搜索重複項。
我希望這可以幫助別人。