【发布时间】:2013-08-03 14:37:07
【问题描述】:
我需要从 PostgreSQL 数据库中读取和连接很多行(约 500k)并将它们写入 MySQL 数据库。
我的幼稚做法是这样的
entrys = Entry.query.yield_per(500)
for entry in entrys:
for location in entry.locations:
mysql_location = MySQLLocation(entry.url)
mysql_location.id = location.id
mysql_location.entry_id = entry.id
[...]
mysql_location.city = location.city.name
mysql_location.county = location.county.name
mysql_location.state = location.state.name
mysql_location.country = location.country.name
db.session.add(mysql_location)
db.session.commit()
每个Entry 大约有1 到100 个Locations。
这个脚本现在运行了大约 20 个小时,并且已经消耗了超过 4GB 的内存,因为所有内容都保存在内存中直到会话提交。
通过我之前提交的尝试,我遇到了this 之类的问题。
如何提高查询性能?它需要变得更快,因为在接下来的几个月中行数将增长到大约 2500k。
【问题讨论】:
-
有什么理由不能使用Extract, Transform, Load 方法?
-
基本上
pg_dump dbname | mysql dbname -
@JochenRitzel,我将多个表中的多行合并为 MySQL 的一行。我不明白
pg_dump有什么帮助。 -
您是否尝试过将 Postgres 中的数据提取到 CSV 并将 CSV 加载到 MySQL 中?
标签: python mysql postgresql sqlalchemy flask-sqlalchemy