python - If there is a way to pre-calculate SQL View in order to speed-up queries from it?
问题描述
I perform numerous different SELECTs from an SQLite VIEW (dynamic table) using a cycle in Python. The underlying SQL query for the VIEW takes about 5 seconds to complete. According to my current understanding, the VIEW is recalculated every time, when I perform a SELECT from it. Since within the Python cycle, I do not update any tables, I wonder if there is a way to "freeze" (precalculate) the VIEW as a static table, perform fast SELECTs from it and after "release" it.
Before I tried to optimize the underlying query code of the VIEW and managed to reduce the execution time from 16 to 5 seconds. I tried also to find in Internet commands similar to "BEGIN;" and "COMMIT;" which do a great job for updating the tables, but could not find any.
My query which I perform from the Python loop looks like that:
SELECT date, spread FROM futures_spreads_close
WHERE commodity_id=? AND exp_month_id=? AND exp_year=?
ORDER BY date;
where the questions marks are the loop indexes.
If I re-implement all the logic from the VIEW into the Python code, I can have spped-up for about 100 times. However, I would like to keep all the logic inside the SQL. I believe that there is a more efficient way to perform queries on a VIEW.
解决方案
如果您使用慢速视图运行多个查询,其行不会在不同查询之间更改,您可以通过将视图的行缓存在临时表中来实现它,并在查询中使用该表:
CREATE TEMP TABLE mat_view AS SELECT * FROM actual_view;
-- Create indexes if needed
关闭数据库连接时会删除临时表,或者您可以DROP
在使用它的查询完成后显式删除它。
推荐阅读
- bootstrap-4 - 为什么添加填充时引导表堆栈DIV
- r - 如何用省略ggplot2的箭头连接多个点
- r - 如何从同一个变量创建不同的文件?
- android - 使用 WorkManager 安排重复工作时出现 IllegalStateException
- airflow - 如何指向气流unittest.cfg?
- python - 在 Python 中枚举按两个字段排序的列表
- reactjs - React hooks 函数有旧版本的 state var
- microsoft-graph-api - Chat.Read 和 Chat.ReadWrite 权限使用
- python - 在 Tensorflow 急切模式下计算模型输入的梯度
- php - 取消链接不会删除文件