我的查询非常简单,但它需要太多的时间才能结束。我有一个数据库,其中有几个表,我需要检查名称和一个包含日期时间信息的变量。但对于每一张表,我花费的时间超过100秒。
query = "show tables"
cursor.execute(query)
tables_info = cursor.fetchall()
tables_info = [x[0] for x in tables_info]
time_month_year = []
for index in tqdm(range(len(tables_info))):
monthyearquery = "select tempo from {}".format(tables_info[index])
cursor.execute(monthyearquery)
tables_time_info = cursor.fetchall()
r = tables_time_info[0][0].strftime('%b %Y')
time_month_year.append(r)有什么方法可以改进这个查询吗?我找不到任何有用的东西。
发布于 2019-07-25 23:05:14
获取整个表fetchall()只是为了获取第一行tables_time_info[0][0]的第一列,在select中使用limit 1使其只返回一行。
time_month_year = []
for table_name in tables_info:
query = "select tempo from {} limit 1".format(tables_info)
cursor.execute(query)
tables_time_info = cursor.fetchall()
r = tables_time_info[0][0].strftime('%b %Y')
time_month_year.append(r)注意,您也可以使用fetchone,但它不会带来性能提升,因为您只获取了1行,它只是更优雅而已
tables_time_info = cursor.fetchone()
r = tables_time_info[0].strftime('%b %Y')https://stackoverflow.com/questions/57204922
复制相似问题