连接池新手 [英] New to Connection Pooling
问题描述
所以我是连接池的新手.我试图确定如何使用池以加快查询速度.我有一个有效的查询,但我认为我使用的池不正确.这是语法,如果您发现我可以提高效率的任何地方,请告诉我.
So I am new to connection pooling. I am trying to determine how to use a pool in order to speed up my query. I have a query that works, but I dont think I am using the pool correct. Here is the syntax, if you see anywhere I could be more efficient please let me know.
try:
db=mysql.connector.connect(poolname="mypool", pool_size=10, **config)
cursor.execute(query1)
df1=create_df(cursor)
cursor.execute(query2)
df2=create_df(cursor)
cursor.execute(query3)
df3=create_df(cursor)
推荐答案
您的问题不清楚, cursor
是如何从 db
来的.
Your question didn't make it clear how cursor
comes from db
.
考虑使用 sqlalchemy .然后,您将免费获得自动池化.
Consider using sqlalchemy. Then you get automatic pooling for free.
import pandas as pd
import sqlalchemy as sa
engine = sa.create_engine(your_local_mysql_url_with_credentials)
with engine.connect() as con:
df1 = pd.read_sql(query1, con)
df2 = pd.read_sql(query2, con)
df3 = pd.read_sql(query3, con)
该池最终成为 engine
的属性.实际上,您很少关心检查它,既然可以正常工作,就可以跨查询挂接到服务器的TCP连接上.
The pool winds up being an attribute of engine
.
In practice you'll seldom care about inspecting it,
since it Just Works, hanging onto a server TCP connection across queries.
这篇关于连接池新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!