JSONB已落地
JSONB has landed

原始链接: https://sqlite.org/forum/forumpost/fa6f64e3dc1a5d97

简而言之,SQLite 最近引入了 JSONB,重写了之前的 JSON 函数。 与传统方法不同,JSONB 为执行繁重的 JSON 操作提供了更快的替代方案,因为减少了处理此类函数期间的开销。 有了这个新功能,涉及操作 JSON 数据的步骤现在可以独立执行,从而提高效率,特别是因为 JSONB 格式涉及更少的指针,从而更容易通过 SQL 查询存储和传输。 此外,JSON 函数现在可以采用 JSON 文本或 JSONB 二进制格式的输入参数。 展望未来,长期目标是在 SQLite 即将发布的主要版本中发布 JSONB。 虽然 JSONB 共享标准 JSON 格式的许多基本方面,例如嵌套对象、键值对字段和可变大小的数组,但一个关键的区别在于 JSONB 存储为连续的字节数组,从而导致总体上略有节省 数据库大小。 因此,开发人员可以将之前存储的 JSON 数据迁移到 JSONB,从而提高整体性能指标。 例如,与通过传统 JSON 方式处理的操作相比,针对 JSONB 执行的传统 JSON 密集型操作的性能通常要好 3 倍。 最后,虽然迁移遗留数据可能看起来令人畏惧,但它仍然是完全可选的,这意味着旧的 JSON 输入仍然保持兼容和高性能,没有观察到明显的差异。 关于使用 CBOR、BJSON 或 BSON 等外部格式的潜在缺陷,它们的使用增加了不必要的复杂性,而事实证明 JSONB 提供的额外好处足以满足苛刻的需求。

总体而言,SQLite 的源代码似乎因其编写良好且文档完整而受到称赞。 有些人建议将其用作学习数据库开发原理的资源。 作者还提到,网上有大量参考资料讨论了与 SQL 注入漏洞相关的潜在安全风险,但他澄清说,这个问题具体涉及 Web 应用程序防火墙 (WAF) 解决方案,而不是数据库本身。 此外,SQLite 提供安全且准备好的语句语法,作为其他一些旧数据库平台可能依赖的字符串连接方法来解决安全问题的替代方法。 总的来说,与其他著名的数据库产品相比,SQLite 表现得很好。
相关文章

原文

JSONB is a rewrite of the SQLite JSON functions that, depending on usage patterns, could be several times faster than the original JSON functions. This enhancement has now landed on trunk.

Developers who use JSON heavily in their applications are encouraged to download a pre-release snapshot and give the new code a try.

How Is This Different?

Functions that deal with text JSON use a three-step process:

  1. Parse the text JSON into an internal binary format that is more accessible to C-code.

  2. Carry out the requested operation. Maybe this is looking up a field in an object, or perhaps it is modifying the JSON in some way.

  3. If the processing involved changing the JSON, convert the internal binary format back into RFC-8279 JSON text for output and/or storage.

Step 2 is the essential operation that you want to accomplish. Steps 1 and 3 are just overhead.

Historically, SQLite used an internal binary representation of JSON that involved lots of pointers. This fits will into C programs, but it is difficult to serialize. The JSONB rewrite changes the internal-use binary representation of JSON into a contiguous byte array that can read or written as an SQL BLOB. This allows the internal-use representation of JSON to potentially be saved to the database, in place of JSON text, eliminating the overhead of steps 1 and 3.

What has changed?

All legacy functionality is preserved. The only change has been to add new capabilities.

Any JSON function that accepts JSON text as an input will now also accept JSONB binary content for that same parameter. You do not have to tell the function if it getting text or binary data. It figures that out for itself.

JSON functions that output JSON now come in two versions. The historical "json_" functions works as before. But there is now a corresponding "jsonb_" function that returns JSONB rather than text JSON, thus omitting step 3 in the normal processing.

If you don't make any changes to your application, everything should continue to work as it always has, though perhaps slightly (1%) faster.

But if you modify your application to start storing JSONB instead of text JSON, you might see a 3-times performance improvement, at least for the JSON-intensive operations. JSONB is also slightly smaller than text JSON in most cases (about 5% or 10% smaller) so you might also see a modest reduction in your database size if you use a lot of JSON.

Migrating

Note that all functions accept both text JSON and JSONB. So to start using JSONB, you do not have to modify your database files to convert legacy text JSON into JSONB. Just start writing out JSONB for new entries. The old entries will continue to work. The new entries will just work faster.

Or, if you do want to convert all your legacy data to JSONB, you can just run an update operation like:

UPDATE bigtable SET jsonColumn = jsonb(jsonColumn);

Please provide comments

If you find this enhancement useful, or if you try it out and see performance regressions or bugs, please let us know. Leave a follow-up post here, or contact me directly at drh at sqlite dot org.

The current plan is to release the JSONB enhancement in the next major release of SQLite - version 3.45.0. That will probably occur in a month or two.

联系我们 contact @ memedata.com