gpt4 book ai didi

python - MySQL 取出数据很慢

转载 作者:行者123 更新时间:2023-11-29 17:51:34 24 4
gpt4 key购买 nike

我需要将 MySQL 工作台中的一些数据导入 Python,以便制作图表。但是当我尝试插入它并尝试打印值时,这是使用 pymysql (参见代码)。运行速度非常慢。

所以我认为当我做实时图表和更多东西时程序会卡住......

有人有解决办法吗?

我的代码:

import pymysql

conn= pymysql.connect(host='127.0.0.1',
port= 3306,
user= 'root',
passwd= '******',
db= 'energie'
)
a = conn.cursor()

sql1 = 'SELECT `VarValue`, `VarName` from `gasmeter1`;'
a.execute(sql1)

gasmeter1 = a.fetchone()

sql2 = 'SELECT `VarValue` from `gasmeter2`;'
a.execute(sql2)

gasmeter2 = a.fetchone()

sql3 = 'SELECT `VarValue` from `gasmeter349`;'
a.execute(sql3)

gasmeter349 = a.fetchone()

sql4 = 'SELECT `VarValue` from `binnentemp ahu3010`;'
a.execute(sql4)

Binnentemperatuur = a.fetchone()

sql5 = 'SELECT `VarValue` from `buiten temp241`;'
a.execute(sql5)

Buitentemperatuur = a.fetchone()

print (gasmeter1[0] , gasmeter2[0] , gasmeter349[0] , Buitentemperatuur[0] , Binnentemperatuur[0])

这只是一个简单的代码,但是速度很慢。有人建议吗?

时间:

代码:

import pymysql
import time

conn= pymysql.connect(host='127.0.0.1',
port= 3306,
user= 'root',
passwd= 'ohdecg',
db= 'energie'
)

a = conn.cursor()

t1 = time.time()

sql1 = 'SELECT `VarValue` from `gasmeter1`;'
a.execute(sql1)

gasmeter1 = a.fetchone ()

sql2 = 'SELECT `VarValue` from `gasmeter2`;'
a.execute(sql2)

gasmeter2 = a.fetchone ()

sql3 = 'SELECT `VarValue` from `gasmeter349`;'
a.execute(sql3)

gasmeter349 = a.fetchone ()

sql4 = 'SELECT `VarValue` from `binnentemp ahu3010`;'
a.execute(sql4)

Binnentemperatuur = a.fetchone()

sql5 = 'SELECT `VarValue` from `buiten temp241`;'
a.execute(sql5)

Buitentemperatuur = a.fetchone()

t2 = time.time()
print("The SQL statement took : {} s".format(t2 - t1))
#print (gasmeter1[0] , gasmeter2[0], gasmeter349[0])

>>>
========================= RESTART: C:\Python27\j.py =========================
The SQL statement took : 32.0269999504 s
>>>

最佳答案

你们的 table 有多大?您每次都会检索整个表,然后只获取第一个元素。你可以尝试用这样的东西来计时:

import time
...

t1 = time.time()
sql4 = 'SELECT `VarValue` from `binnentemp ahu3010`;'
a.execute(sql4)
t2 = time.time()
print("The SQL statement took : {} s".format(t2 - t1))

关于python - MySQL 取出数据很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49280281/

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