gpt4 book ai didi

python - pandas read_sql_table 永远不会解析

转载 作者:行者123 更新时间:2023-11-29 16:32:40 25 4
gpt4 key购买 nike

我正在尝试使用 SQL 和 Pandas。遵循 sql queries 的 pandas 指南以及相关的sqlalchemy engine specification guide

from sqlalchemy import create_engine
'''
example from sqlalchemy guide, engine_spec has the form of:

dialect+driver://user:password@host:port/name

where leaving out "+driver" defaults to whatever the SQLAlchemy choose for dialect
'''

my_engine_spec = 'mysql://user:password@host:port/name'
engine = create_engine(my_engine_spec)

data = pd.read_sql_table(table_name, engine, chunksize=10)

注意事项:

  • 我限制chunksize到10,表中有超过10条记录。这是为了测试以确保代码可以在不等待数据转储的情况下工作...但它仍然无法解决

  • 我正在使用mysql使用默认驱动程序,我经历了使用 python3+ 在 macOS 上安装它的痛苦

  • 我等了 5 分钟多才终止脚本。

  • 我可以使用具有相同规范的所选 SQL DB 可视化工具应用程序(例如 Sequel Pro)立即查看数据,因此假设变量 my_engine_spec是正确的。

为什么这个问题需要这么长时间甚至没有解决?我怎样才能加快速度?

最佳答案

I limit chunksize to 10 and there are more than 10 records in the table.

Pandas 以 block 的形式读取整个表,每个 block 有 10 条或更少的记录。当您指定 chunksize 时,pandas.read_sql_table 将返回一个生成器。

data = pd.read_sql_table(table_name, engine, chunksize=10)
[data.__next__() for _ in range(1)] # only read one chunk, 10 records or less

I waited over 5 minutes before terminating the script.

read_sql_table 将读取整个表的所有记录。这意味着如果你想使用read_sql_table一个非常大的表然后选择特殊列,与在mysql中完成它相比,你将花费更多的时间和内存。 为什么你花了这么长时间,这与你通过代码所做的事情相关,如果你只运行你的示例代码显示的内容,它应该很快(我已经尝试阅读0.5M条记录,但只需要几秒钟)

Why does this take so long or not even resolve?

如果您不需要所有记录,请使用 read_sqlread_sql_query 运行查询。因为MySQL做得很好。

关于python - pandas read_sql_table 永远不会解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53786513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com