我正在3000次循环中调用一个函数。我正在调用该函数并从该函数获取一些字典,但这需要时间。我正在从数据库中获取数据,这给了我大约3000行,并且正在循环这些行并在该循环中调用从数据库中获取数据并返回字典的函数,但这需要时间。
代码:
def test(request, uni_id):
try:
Obj = get_object_or_404(tabl_name, id=uni_id)
except:
Obj = None
dict = {}
if Obj:outlet_info
dict['data1'] = Obj.id
dict['data2'] = Obj.name
dict['data3'] = Obj.eg
dict['data4'] = Obj.access
return dict
cursor.execute('''SELECT cd.name, cd.no,ofk.demo_id
FROM `main_table` as myo
LEFT JOIN `table1` as emt ON emt.some_id = myo.some1_id
LEFT JOIN `table2` as ofk ON ofk.id = myo.kit_id
LEFT JOIN `table3` as cd ON cd.eg_id = myo.eg_id
WHERE emt.type='test''''
result = dictfetchall(cursor)
tmp_list, tmp_dict = [], {}
for res in result:
tmp_dict['name'] = res['name']
tmp_dict['no'] = res['no']
info = test(request,res['demo_id'])
tmp_dict['data1'] = info['data1']
tmp_list.append(tmp_dict.copy())
在这里,我从查询中获取demo_id
,并使用函数test
将其传递给另一个查询以获取数据,因此这花费了太多时间。
有人可以告诉我如何提高速度或将demo_id
包含在主查询中并从主查询中获取数据吗?
答案 0 :(得分:2)
您正在进行3000次db调用(通过调用get_object_or_404
3k次),这肯定会变慢。相反,您应该尝试通过tablemanager.filter(id__in=id_list)
获取对象。循环准备id_list并将该列表传递给您的测试函数。
答案 1 :(得分:1)
我在查询中包括了所有内容。现在我不需要调用函数test
cursor.execute('''SELECT (SELECT CONCAT_WS(',',id,name,eg,access)
from tabl_name where id=ofk.demo_id) AS result
,cd.name, cd.no,ofk.demo_id
FROM `main_table` as myo
LEFT JOIN `table1` as emt ON emt.some_id = myo.some1_id
LEFT JOIN `table2` as ofk ON ofk.id = myo.kit_id
LEFT JOIN `table3` as cd ON cd.eg_id = myo.eg_id
WHERE emt.type="test" '''
result = dictfetchall(cursor) # function to get data in form of dictionary
tmp_list, tmp_dict = [], {}
for res in result:
try:
info = res['result'].decode("utf-8").split(",") # converting res['result'] from bytes to string
except:
info = res['result'].split(",") # res['result'] is in string form
tmp_dict['name'] = res['name']
tmp_dict['no'] = res['no']
tmp_dict['data1'] = info[0] # id
tmp_list.append(tmp_dict.copy())
这件事使我的代码快了10倍