我有一个PyTables文件,该文件的单个表位于一个组中,总共约1.1亿行。列为:
In [14]: table.description
Out[14]: {
"Epp": Float32Col(shape=(), dflt=0.0, pos=0),
"S1200": Float64Col(shape=(), dflt=0.0, pos=1),
"S400": Float64Col(shape=(), dflt=0.0, pos=2),
"S800": Float64Col(shape=(), dflt=0.0, pos=3),
"Sw": Float64Col(shape=(), dflt=0.0, pos=4),
"nu": Float64Col(shape=(), dflt=0.0, pos=5)}
我有索引:
In [15]: table.indexedcolpathnames
Out[15]: ['S1200', 'S400', 'S800', 'Sw', 'nu']
在创建HDF5文件时,我也将expectedrows=110420854
传递给了create_tables
。
但是,即使是对索引列的直接查询也非常慢。该脚本:
from datetime import datetime
import tables as pyt
h5file = pyt.open_file("HITEMP_H2O.h5", mode="r")
table = h5file.root.lbl
query = """(nu > 50) & (nu < 2000) & (S800 > 1.e-26)"""
start = datetime.now()
print(start)
with open('test.out', 'w') as fo:
for x in table.where(query):
print(x['nu'], x['Sw'], file=fo)
end = datetime.now()
print(end-start)
在40秒钟内产生65503行,查询大量数据的时间过长。
我还能做些什么来优化我的PyTables查询?