使用生成列以完全索引速度进行 SQLite JSON 处理
SQLite JSON at Full Index Speed Using Generated Columns

原始链接: https://www.dbpro.app/blog/sqlite-json-virtual-columns-indexing

## SQLite 的隐藏 JSON 力量 DB Pro 最近重新发现 SQLite 中一项强大的功能:其强大的 JSON 处理能力。虽然 SQLite 存在局限性,但它是一款令人惊讶的强大数据库,即使在生产环境中也是如此,并且由于 libSQL 和 PocketBase 等项目而正在复兴。 一项关键技术是将 JSON 文档“原始”存储在列中,然后使用 `json_extract` 创建**虚拟生成列**以提取特定数据点。 重要的是,您可以**索引这些虚拟列**,从而实现极快的查询——表现得像标准关系数据。 这种方法提供了巨大的灵活性。 与需要预先进行模式决策的传统 JSON 索引不同,您可以*按需*添加新的虚拟列和索引,以适应不断变化的数据需求**而无需数据迁移或模式重写**。 这种模式结合了无模式 JSON 数据的优势和关系数据库的性能,为在 SQLite 中处理 JSON 提供了一种强大而灵活的解决方案。 作者感谢 Hacker News 用户“bambax”阐明了这项技术,并计划在未来的帖子中分享更多 SQLite 的发现。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 SQLite 使用生成列以完全索引速度处理 JSON (dbpro.app) 5 分,来自 upmostly 35 分钟前 | 隐藏 | 过去 | 收藏 | 1 条评论 upmostly 30 分钟前 [–] 受到 bambax 在 2023 年 HN 帖子中的评论启发,我写了这篇博文:https://news.ycombinator.com/item?id=37082941 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

We absolutely love SQLite here at DB Pro. You’d be hard-pressed to find anyone who actively dislikes it. Sure, it has limitations, and I do mean limitations, not weaknesses. SQLite can absolutely be used in production when it’s deployed properly and tuned with care.

SQLite has also seen something of a resurgence over the past few years. From being forked into projects like libSQL and Turso, to powering popular backend frameworks such as PocketBase, it’s clearly having a moment again.

As I said though, we love it. It even powers the local database inside DB Pro itself. For our use case, there really isn’t a better alternative.

Because we’ve been using SQLite in anger over the past three months, we’ve learnt a huge amount about it, including plenty of things we didn’t know before.

So I’m planning to write a short series of blog posts covering some of the cooler, more interesting features and nuances of SQLite that we’ve discovered along the way. This is the first of those posts.

First of all. Did you know SQLite has JSON functions and operators? I didn't until recently! I came across this Hacker News comment when researching SQLite's JSON operators.

The cool thing for working with json is to store each json document as is in one column, then make virtual columns that store some specific information you want to query, using some combination of json_extract, then index those columns.
This makes for super-fast search, and the best part is you don't have to choose what to index at insert time; you can always make more virtual columns when you need them.

(You can still also search non-indexed, raw json, although it may take a long time for large collections).

I love SQLite so much - bambax

SQLite GIF

I read that, then read it again, then again until I understood what bambax was saying. I was sort of in disbelief.

So I had to give it a try to see if it works. We've got an embedded SQLite-in-the-browser component on our blog and so I wanted to throw together some working examples for you guys (but mostly for me).

Let's break down what bambax is saying:

  1. Store the JSON document raw
  2. Create virtual generated columns using json_extract
  3. Add indexes to those generated columns
  4. Query JSON at full B-tree index speed

This means you never have to choose your indexing strategy up front. If you later realise you need to query on a new JSON field, you simply add another generated column, add an index, and you're done.

No data migration. No schema rewrite. No ETL. Just pure flexibility.

1. Store Raw JSON

First, create a simple table with a JSON column: