SQLite Blob读写的局限性 博客分类: DB2|PostgreSQL|SQLite|Others
程序员文章站
2024-03-22 15:35:46
...
在使用SQLite在Windows Mobile上的大容量BLOB读写时,遇到一个OOM的问题,让我们都觉得不可思议:
executeStmt(db, stmt, "create table foo(id integer not null primary key, content blob null, content2 blob null)");
sqlite3_stmt* stmt2 = NULL;
executeStmt(db, stmt, "insert into foo(id) values(10)");
sqlite3_stmt* stmt3 = NULL;
executeStmt(db, stmt3, "update foo set content=zeroblob(10*1024*1024) where rowid=10");
sqlite3_stmt* stmt4 = NULL;
executeStmt(db, stmt4, "update foo set content2=zeroblob(1024) where rowid=10"); // OOM here
不过是0单列的0填充而已,怎么会OOM呢?
我直接给它的作者D. Richard Hipp和他的team发邮件询问,得到的答复是:
每次更新,sqlite都会读取该行的数据,加载到内存里。难怪,第一列可是要10M内存。
Richard建议:使用incremental BLOB I/O mechanism机制进行读写。
可是这个机制局限性依然明显:
e.g.
first write, 20K, then append 10K. If we want to append it, we need to zeroblob 30K first. Otherwise, sqlite_blob_write will fail to write the chunk.
???
他的答复是:
Unfortunately, no; there is no way to increase the size of a row without allocating memory sufficient to hold the entire row.
除非要更改sqlite的文件格式。一旦更改,将会打破与以前sqlite文件的兼容性。那是不可容忍的。
看来,使用小内存逐步更改blob字段,在目前来看不太现实。
executeStmt(db, stmt, "create table foo(id integer not null primary key, content blob null, content2 blob null)");
sqlite3_stmt* stmt2 = NULL;
executeStmt(db, stmt, "insert into foo(id) values(10)");
sqlite3_stmt* stmt3 = NULL;
executeStmt(db, stmt3, "update foo set content=zeroblob(10*1024*1024) where rowid=10");
sqlite3_stmt* stmt4 = NULL;
executeStmt(db, stmt4, "update foo set content2=zeroblob(1024) where rowid=10"); // OOM here
不过是0单列的0填充而已,怎么会OOM呢?
我直接给它的作者D. Richard Hipp和他的team发邮件询问,得到的答复是:
每次更新,sqlite都会读取该行的数据,加载到内存里。难怪,第一列可是要10M内存。
Richard建议:使用incremental BLOB I/O mechanism机制进行读写。
可是这个机制局限性依然明显:
e.g.
first write, 20K, then append 10K. If we want to append it, we need to zeroblob 30K first. Otherwise, sqlite_blob_write will fail to write the chunk.
???
他的答复是:
Unfortunately, no; there is no way to increase the size of a row without allocating memory sufficient to hold the entire row.
除非要更改sqlite的文件格式。一旦更改,将会打破与以前sqlite文件的兼容性。那是不可容忍的。
看来,使用小内存逐步更改blob字段,在目前来看不太现实。
推荐阅读
-
设置timestamp 显示格式 博客分类: 数据库
-
Linux 新添加非root用登录后为-bash-4.1$而不是 [xxx@sensor~]模式 博客分类: linux-bash-4.1$ linux-bash-4.1$
-
rails实战--(四)FCKEditor与rails的集成 RailsfckeditorFPJavaScript应用服务器
-
ubuntu 15.10安装mysql数据库 开启远程端口 博客分类: VMware ubuntumysql
-
Java多线程总结之线程安全队列Queue 博客分类: 多线程QueueBlockingQueueConcurrentLinkedQueue 多线程QueueBlockingQueueConcurrentLinkedQueue
-
ibatis的两个小问题 iBATIS
-
在线词典业务 博客分类: 综合技术 金山Google
-
Oracle11.2.0非安装版(简装版)制作完成(仅供开发人员参考使用) 博客分类: OracleDatabase GeneralSoftware Architecture|OO
-
万恶的sqlserver 博客分类: 数据库 Oracle
-
看看这个笑死我的帖子:"说一说编程恶习" 博客分类: 程序员的生活 编程JavaScriptEclipseSwingIDEA