Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

excuting sql costs a lots of time occasionally. #53

Open
prettygirl20231 opened this issue Nov 12, 2024 · 1 comment
Open

excuting sql costs a lots of time occasionally. #53

prettygirl20231 opened this issue Nov 12, 2024 · 1 comment

Comments

@prettygirl20231
Copy link

enviroment:
mysql8.0
pymysql
python3.13

metersphere_db_pool = PooledDB(
    creator=pymysql,  # 使用链接数据库的模块
    mincached=2,  # 初始化时,链接池中至少创建的链接,0表示不创建
    maxcached=10,
    maxconnections=10,  # 连接池允许的最大连接数,0和None表示不限制连接数
    blocking=False,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
    host='52.83.162.13',
    port=3306,
    ping=0,
    database='metersphere_dev'
)

def metersphere_query(sql: str, *args) -> list[dict]:
    """查询数据"""
    try:
        conn = metersphere_db_pool.connection()
        cursor = conn.cursor(pymysql.cursors.DictCursor)  # 表示读取的数据为字典类型
        start_time = time.time()
        cursor.execute(sql, args)
        end_time = time.time()
        print(f"execute耗时{end_time - start_time}")
        result = cursor.fetchall()
    finally:
        cursor.close()
        conn.close()
    return result

cursor.execute(sql, args) cost a lots of time(about 20s) occasionally.
This problem often occurs when the query has not been used for a period of time.

@prettygirl20231 prettygirl20231 changed the title excute sql cost a lots of time occasionally. excuting sql costs a lots of time occasionally. Nov 12, 2024
@Cito
Copy link
Member

Cito commented Nov 14, 2024

Does this happen for complex queries? I can imagine that if a connection is idle for a long time, the query cache and buffer pool needs to be re-fetched.

It could also happen when you are using too many connections and set blocking to True, but since you set it to False, you should get an error in this case.

It could be also some kind of MySQL quirk, like the ones reported here, here or here.

Without further information or reproducible code, I cannot help you with this.

Personally, I'm using PostgreSQL and never experienced problems like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants