如何解决从 ClickHouse 获取数据到 Pandas 数据帧时减少 RAM 消耗
在从 ClickHouse 获取数据到 Pandas 数据帧时,我试图减少 RAM 消耗。现在我有:
Filename: full.py
Line # Mem usage Increment Occurences Line Contents
============================================================
37 89.1 MiB 89.1 MiB 1 @profile
38 def full():
39 754.0 MiB 664.9 MiB 1 df = pd.read_sql(sql=query,con=conn)
40 754.0 MiB 0.0 MiB 1 return df
cpu times: user 1.25 s,sys: 280 ms,total: 1.53 s
Wall time: 1min 8s
和:
Filename: it.py
Line # Mem usage Increment Occurences Line Contents
============================================================
37 89.4 MiB 89.4 MiB 1 @profile
38 def it():
39 89.4 MiB 0.0 MiB 1 df = pd.DataFrame()
40 661.8 MiB 572.4 MiB 1 iterator = pd.read_sql(sql=query,con=conn,chunksize=100_000)
41 813.1 MiB 41.4 MiB 9 for chunk in iterator:
42 813.1 MiB 109.9 MiB 8 df = df.append(chunk)
43 813.1 MiB 0.0 MiB 1 return df
cpu times: user 1.37 s,sys: 367 ms,total: 1.73 s
Wall time: 1min 12s
由 memory-profiler 测量。 conn 是 SQLAlchemy create_engine。行数为 712 300。
正如您在时间上看到的那样,它们或多或少是相等的。但根据记忆,它们不是。我希望 pandas.read_sql 结果作为 iterator (当 chunksize 被定义时)的大小会小得多。或者这是在意料之中的正常现象?
更新。 pandahouse 显示了这样的结果:
Filename: ph.py
Line # Mem usage Increment Occurences Line Contents
============================================================
30 78.9 MiB 78.9 MiB 1 @profile
31 def ph():
32 290.1 MiB 211.2 MiB 1 df = read_clickhouse(query,connection={...})
33 290.1 MiB 0.0 MiB 1 return df
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。