我有一个包含大约30k行数据的表,并且有几个表关联。问题是,有18列,每列需要1个db查询。
这是我正在做的事情的一个例子
if col_num == 0
tmp_string_text = finding_phase
elsif col_num == 1
tmp_string_text = ""
elsif col_num == 2
tmp_string_text = ""
elsif col_num == 3
tmp_string_text = finding.vuln_finding.severity
elsif col_num == 4
tmp_string_text = finding_node_ip
elsif col_num == 5
tmp_string_text = finding.node.host_name
elsif col_num == 6
tmp_string_text = finding.node.dns_name
elsif col_num == 7
tmp_string_text = finding.port
elsif col_num == 8
tmp_string_text = finding.pentest_finding.name
elsif col_num == 9
tmp_string_text = finding.vuln_finding.name
elsif col_num == 10
tmp_string_text = finding.vuln_finding.description
elsif col_num == 11
tmp_string_text = finding.vuln_finding.solution
elsif col_num == 12
tmp_string_text = finding.additional_output
elsif col_num == 13
tmp_string_text = finding.cve
elsif col_num == 14
tmp_string_text = finding.node.os
elsif col_num == 15
tmp_string_text = finding.node.device_type
elsif col_num == 16
tmp_string_text = finding.node.scan_time
end
结果,每1行将有15个查询到DB。有什么办法可以让我更有效率吗?当我有30k行时,这需要很长时间。
答案 0 :(得分:1)
我建议使用from collections import Counter
arr = [9,1,2,4,1,2,2]
unique = {k for k, v in Counter(arr).iteritems() if v == 1}
print [i for i in arr if i not in unique]
方法立即获取所有列,然后从内存中迭代结果。
includes
答案 1 :(得分:1)
您还应该定期为要查询的列添加索引。根据经验,您通常希望为以下内容添加索引: