问题
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)
回答1:
Your question didn't make it clear how cursor
comes from db
.
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)
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.
来源:https://stackoverflow.com/questions/65117339/new-to-connection-pooling