问题描述
在从 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 (将#修改为@)