如何在不读取 BLOB 内容的情况下获取 BLOB 的大小

2022-01-01 00:00:00 sql blob sqlite triggers

关于 sqlite 中的 BLOB,我有以下问题:

I have the following questions regarding BLOBs in sqlite:

  • sqlite 是否会跟踪 BLOB 的大小?
  • 我猜是这样,但是,长度函数是使用它,还是读取 BLOB 的内容?
  • 如果 sqlite 跟踪 BLOB 的大小并且 length 不使用它,那么是否可以通过其他一些功能访问该大小?

我问这个是因为我想知道我是否应该实现在附加列中设置 BLOB 大小的触发器,或者我是否可以动态获取大小而不会影响 sqlite 读取 BLOB 的性能.

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.

推荐答案

来自:

** In an SQLite index record, the serial type is stored directly before
** the blob of data that it corresponds to. In a table record, all serial
** types are stored at the start of the record, and the blobs of data at
** the end. Hence these functions allow the caller to handle the
** serial-type and data blob seperately.
**
** The following table describes the various storage classes for data:
**
**   serial type        bytes of data      type
**   --------------     ---------------    ---------------
**      0                     0            NULL
**      1                     1            signed integer
**      2                     2            signed integer
**      3                     3            signed integer
**      4                     4            signed integer
**      5                     6            signed integer
**      6                     8            signed integer
**      7                     8            IEEE float
**      8                     0            Integer constant 0
**      9                     0            Integer constant 1
**     10,11                               reserved for expansion
**    N>=12 and even       (N-12)/2        BLOB
**    N>=13 and odd        (N-13)/2        text

换句话说,blob 大小是串行的,它的长度只是(serial_type-12)/2".
此序列存储在实际 blob 之前,因此您无需读取 blob 即可获取其大小.
调用 sqlite3_blob_open 然后调用 sqlite3_blob_bytes 来获取这个值.

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.

相关文章