gpt4 book ai didi

sql - Python/sqlite3:执行数千个“SELECT”查询时提高性能

转载 作者:行者123 更新时间:2023-12-03 18:52:30 27 4
gpt4 key购买 nike

这是简单的用例:

selectString = "SELECT MAX(shear_strain) FROM detailed_structure_info WHERE elementGroup = ? AND element = ? AND analysis = ?"
selectInputTuple = list(itertools.product(elementGroups, elementNumbers, analysisNumbers))

results = []
numExecutes = 0

for selectInputTuple in selectInputTuples:
c = ei_cursor.execute(selectString, selectInputTuple)
results.append(c.fetchone()[0])
numExecutes += 1

print "{} out of {} commands completed...".format(numExecutes, len(selectInputTuples))


目前,每次循环大约需要1秒钟,并且可能需要循环数百次。我已经考虑过如何执行尽可能大的SELECT语句,但是目前,我认为这是我可以做的最小的事情,因为在继续之前,我绝对需要聚合一个特定的列。那么,我还能怎么写我的SELECT语句来优化速度呢?

最佳答案

遍历元组以进行聚合似乎是浪费的。只需使用group by即可立即获得所有结果:

SELECT elementGroup, element, analysis, MAX(shear_strain) as max_shear_strain
FROM detailed_structure_info
GROUP BY elementGroup, element, analysis;


如果由于某种原因必须运行单独的查询,请创建一个索引:

create index idx_detailed_structure_info_4
on detailed_structure_info(elementGroup, element, analysis, shear_strain);

关于sql - Python/sqlite3:执行数千个“SELECT”查询时提高性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23974134/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com