【发布时间】:2020-01-14 18:58:38
【问题描述】:
我有一个类似的查询:
query = HistoryLogs.query()
query = query.filter(HistoryLogs.exec_id == exec_id)
iter = query.iter()
for ent in iter:
# write log to file, nothing memory intensive
我在 for 循环中添加了日志,读取 10K 行会增加 200MB 的内存使用量,然后读取接下来的 10K 行会增加额外的 200MB,依此类推。读取 100K 需要 2GB,超过了 highmem 内存限制。
我尝试在 for 循环中清除内存缓存,在读取 10K 行后,添加:
# clear ndb cache in order to reduce memory footprint
context = ndb.get_context()
context.clear_cache()
在 for 循环中,在每 10K 次迭代中,但导致查询超时,引发错误 BadRequestError: The requested query has expired. Please restart it with the last cursor to read more results. ndb。
我最初的期望是,通过使用query.iter() 而不是query.fetch(),我不会遇到任何内存问题并且内存几乎是恒定的,但事实并非如此。有没有办法在不超过时间和内存限制的情况下使用迭代器读取数据?通过清除上下文缓存,我发现内存消耗几乎是恒定的,但我遇到了检索所有行所需的时间。
顺便说一句,有很多行要检索,最多 150K。是否可以通过一些简单的调整来完成这项工作,或者我需要一个更复杂的解决方案,例如一个会使用一些并行化的?
【问题讨论】:
标签: python python-2.7 performance google-cloud-datastore app-engine-ndb