所以我有一个SQL查询,使用Django加载需要很长时间,10000行大约需要30秒。如果我使用python直接运行完全相同的代码,它会在2秒内完成。出于某种原因,我建立的循环在Django运行代码时执行的时间非常长,有人知道为什么会这样吗?我可以做些什么来提高性能并摆脱这种不便吗?
import psycopg2
def doQuery( conn ) :
cur = conn.cursor()
cur.execute("SELECT * FROM table WHERE substring(addr from 0 for 5)
= '\\x82332355'::bytea")
return cur.fetchall()
myConnection = psycopg2.connect( host=hostname, user=username,
password=password, dbname=database )
results = doQuery( myConnection )
def lists(t):
if type(t) == list or type(t) == tuple:
return [lists(i) for i in t]
return t
results = lists(results)
for result in results:
result[1] = str(result[1]).encode("hex"))
result[3] = datetime.datetime.fromtimestamp(int(result[3])).strftime('%Y-%m-%d %H:%M:%S')
result[6] = "Not Avaliable"
print result
for 循环^^^^^^^^在Django中需要很长时间,在python中快速
myConnection.close()