[英]Effectively querying DBF files with Python
我需要從舊版 VFP DBF
數據庫中讀取數據並收集本周內所有具有etd
的行。
我正在使用dbf
但是似乎在查詢表時,它從表中的第一條記錄開始查詢。 這會在嘗試查找上周內的數據時導致性能問題,因為它每次運行時都必須遍歷數據庫中的每一行 (60k+)。
table = dbf.Table(r'\\server\file.dbf')
table.open()
for row in table:
if (self.monday < row.etd < self.friday) and ('LOC' not in row.route):
self.datatable.Rows.Add(row.manifest, row.route, row.etd, row.eta, row.inst, row.subname)
else:
continue
我試圖用for row in table[::-1]:
但是,這與我認為在[::-1]
之前將數據庫加載到 memory 所需的時間相同
查詢這些DBF
文件的更有效方法是什么?
如您所知, dbf
不支持索引文件。 但是,它確實有一些類似於 VFP 的方法可以提供幫助:
# untested
table = ...
potental_records = []
with table: # auto opens and closes
table.bottom() # goes to end of table
while True:
table.skip(-1) # move to previous record
row = table.current_record
if self.monday > row.etd:
# gone back beyond range
break
elif row.etd < self.friday:
potential_records.append(row)
# at this point the table is closed and potential_records should have all
# records in the etd range.
僅當記錄由etd
物理訂購時,上述方法才有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.