我有一个在heroku上运行的python应用程序,它使用标准的postgresql heroku db($ 50版本)。 db中有4个表。我的应用根据我的应用的用户输入查询主表中的一个主键。我的heroku postgres psycopg2(python)查询每次执行都会变得越来越慢。任何见解?
查询一开始工作的很好,但现在我发现它变得太慢后大约40-50分钟没有重新启动我的动态。一段时间后查询将花费2,000毫秒,并需要几秒钟才能在用户面前加载。我更新编程,这是我的第二个应用程序。我想知道什么会使查询慢慢而不是不变。起初他们太快了。什么是应用程序中的psycopg2的最佳做法,以确保数据库不会挂断?这里是(所有其他在整个脚本类似的语法)的一个查询的例子:
if eventText=="Mc3 my champs":
user=event.source.user_id
profile= line_bot_api.get_profile(user)
name=str((profile.display_name))
cur=None
try:
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
# get the user's information if it exists
cur.execute("""SELECT lineid, summoner_name, champ_data FROM prestige_data WHERE lineid = %(lineid)s LIMIT 1""", {"lineid": user})
rows = cur.fetchall()
for row in rows:
champs = row[2]
prestige=(calculate_prestige(champs))
champs = json.loads(champs)
champsdict=dict.items(champs)
champs_sorted=sorted(champsdict, key=lambda student: student[1], reverse=True)
l=('\n'.join(map(str,champs_sorted)))
hello=str(l).replace('(', '').replace(')', '')
yay=str(hello).replace("'", "").replace("'", "")
msg=(yay+'\n'+"---------------------------"+'\n'+name+'\n'+"Prestige:"+(str(prestige)))
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=msg))
break # we should only have one result, but we'll stop just in case
# The user does not exist in the database already
else:
msg = "Oops! You need to add some champs first. Try 'Mc3 inputchamp'."
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=msg))
except BaseException:
if cur is not None:
conn.rollback()
finally:
if cur is not None:
cur.close()
_spiffy_是什么意思?你在'finally'块中做了什么? –
查询运行良好。 Checked pg:extras和缓存命中率为.98+,没有空闲事务,没有异常值,没有未使用的索引等。我现在的应用程序运行非常流畅。我从服务员/服务员变成了gunicorn。我听说过女服务员的很棒的事情,但我认为在我的烧瓶版本中运行它有一些问题。运行3名使用gunicorn的工作人员,每300次点击自动刷新一次。还将部分索引的查询移除到部署应用程序时加载的json文件。 –