如何在不读取 BLOB 内容的情况下获取 BLOB 的大小
我对 sqlite 中的 BLOB 有以下问题:
- sqlite 是否跟踪 BLOB 的大小?
- 我猜它确实如此,但是长度函数是否使用它,或者它是否读取 BLOB 的内容?
- 如果 sqlite 跟踪 BLOB 的大小并且 length 不使用它,那么可以通过其他功能访问该大小吗?
我问这个问题是因为我想知道是否应该实现在附加列中设置 BLOB 大小的触发器,或者是否可以动态获取大小而不会影响 sqlite 读取 BLOB 的性能。
I have the following questions regarding BLOBs in sqlite:
- Does sqlite keep track of sizes of BLOBs?
- I'm guessing that it does, but then, does the length function use it, or does it read the BLOB's content?
- If sqlite keeps track of the size of the BLOB and length doesn't use it, is the size accessible via some other functionality?
I'm asking this because I'm wondering if I should implement triggers that set BLOBs' sizes in additional columns, of if I can obtain the sizes dynamically without the performance hit of sqlite reading the BLOBs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从来源:
换句话说,blob 大小是在序列中,它的长度只是“(serial_type-12)/2”。
该序列存储在实际 blob 之前,因此您无需读取 blob 来获取其大小。
调用 sqlite3_blob_open 然后调用 sqlite3_blob_bytes 来获取该值。
From the source:
In other words, the blob size is in the serial, and it's length is simply "(serial_type-12)/2".
This serial is stored before the actual blob, so you don't need to read the blob to get its size.
Call sqlite3_blob_open and then sqlite3_blob_bytes to get this value.
在测试数据库中写入一个 1byte 和一个 10GB blob。如果
length()
对两个 blob 花费相同的时间,则可能会访问 blob 的长度。否则该 blob 可能会被读取。或者:下载源代码并通过它进行调试: http://www.sqlite.org/download.html< /a>.这些是一些相关位:
然后
您可以看到文本数据的长度是动态计算的。斑点的那个...好吧,我的 C 语言不够流利...:-)
Write a 1byte and a 10GB blob in a test database. If
length()
takes the same time for both blobs, the blob's length is probably accessed. Otherwise the blob is probably read.OR: download the source code and debug through it: http://www.sqlite.org/download.html. These are some relevant bits:
and then
You can see that the length of text data is calculated on the fly. That of blobs... well, I'm not fluent enough in C... :-)
如果您有权访问原始 c api sqlite3_blob_bytes 将为您完成这项工作。如果没有,请提供更多信息。
If you have access to the raw c api sqlite3_blob_bytes will do the job for you. If not please provide additional information.