sqlite缓存查询的结果以进行优化吗? [英] is sqlite caching the results of the query for optimization?
问题描述
我注意到sqlite中的这种行为.当我重新使用游标对象时,任务管理器中的工作集内存会不断增加,直到我的程序抛出内存不足异常为止.
I've noticed this behavior in sqlite. When I re-use the cursor object, the working set memory in the task manager keeps increasing until my program throws a out of memory exception.
我重构了代码,以便每次查询时都打开与sqlite文件的连接,查询我想要的内容,然后关闭该连接.
I refactored the code such that each time I query I open a connection to the sqlite file query what I want and then close the connection.
后者似乎不那么耗费内存.它不会增加超过特定的点.
The latter somehow seems to be not so memory-hungry. It doesn't increase beyond a certain point.
我对sqlite数据库所做的只是针对一个表的简单选择(包含两个聚合).
All I do with my sqlite db is simple select (which contains two aggregations) against a table.
这是我们可以控制的行为吗?我想重用我的游标对象,但又不想耗尽内存...
Is this a behavior we can somehow control? I'd want to reuse my cursor object, but not want memory to be eaten up...
推荐答案
默认情况下,缓存大小相当大(〜2MB),即每个连接一次.您可以使用SQL语句将其设置为较小:
By default the cache size is fairly big (~2MB) and that is per-connection. You can set it smaller with the SQL statement:
PRAGMA cache_size=-KB
使用负的-" KB值将其设置为千字节大小,否则设置要使用的页面数.
Use the negative '-' KB value to set it as a size in KiloBytes, otherwise it set the number of pages to use.
此外,如果使用多个连接,则可能要使用共享缓存来节省内存: SQLITE:共享缓存
Also, if using multiple connections you might want to employ a shared cache to save memory: SQLITE: Shared Cache
这篇关于sqlite缓存查询的结果以进行优化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!