S
Supanote
Sign in Sign up

SQLite 3.50+ Binary JSONB, STRICT Table & Generated Indexes

plain (sql) 1 day ago · 46 lines · 4 views
Verified Blueprint SQLite 3.50+

Cut SQLite JSON CPU overhead by 50%+ using binary JSONB BLOB storage, STRICT tables, VIRTUAL generated columns, and jsonb_set updates.

  • ✓ Stores pre-parsed binary JSONB inside a STRICT table with CHECK(json_valid(attrs, 4)) validation
  • ✓ Indexes hot JSON attributes via zero-storage VIRTUAL generated columns for sub-millisecond lookups
  • ✓ Uses SQLite 3.50+ localized jsonb_set / jsonb_replace to mutate nested keys without re-parsing text
Quick Fetch
1-- SQLite 3.45 - 3.50+ High-Speed Binary JSONB + STRICT Table + Generated Index Pattern
2-- Binary JSONB avoids repeated text parsing and enables localized in-place mutations in 3.50+
3
4PRAGMA journal_mode = WAL;
5PRAGMA synchronous = NORMAL;
6
7-- 1. STRICT Table Storing Binary JSONB Payload + Zero-Disk VIRTUAL Generated Columns
8CREATE TABLE IF NOT EXISTS user_sessions (
9 session_id TEXT PRIMARY KEY,
10 user_id INTEGER NOT NULL,
11 -- Store binary JSONB BLOB; flag '4' in json_valid enforces RFC-8259 OR SQLite JSONB format
12 metadata BLOB NOT NULL DEFAULT (jsonb('{}')) CHECK (json_valid(metadata, 4)),
13 -- Virtual generated columns compute on read & power standard B-tree indexes with 0 byte storage overhead
14 plan_tier TEXT GENERATED ALWAYS AS (metadata ->> '$.plan') VIRTUAL,
15 region_code TEXT GENERATED ALWAYS AS (metadata ->> '$.geo.region') VIRTUAL,
16 updated_at INTEGER NOT NULL DEFAULT (unixepoch())
17) STRICT;
19-- 2. Partial & Composite B-Tree Indexes Over Virtual JSONB Fields
20CREATE INDEX IF NOT EXISTS idx_sessions_plan_user
21 ON user_sessions (plan_tier, user_id);
23CREATE INDEX IF NOT EXISTS idx_sessions_eu_region
24 ON user_sessions (region_code, updated_at DESC)
25 WHERE region_code IS NOT NULL;
27-- 3. Insert Converting Incoming Text JSON Into Compact Binary JSONB Once on Write
28INSERT INTO user_sessions (session_id, user_id, metadata)
29VALUES (
30 'sess_0195f410a9c2',
31 1042,
32 jsonb('{"plan":"pro","mfa":true,"geo":{"region":"eu-central","asn":24940},"quotas":{"runs":12}}')
33);
35-- 4. Localized In-Place Mutation (SQLite 3.50+ optimizes jsonb_replace / jsonb_set internal byte shifts)
36UPDATE user_sessions
37SET metadata = jsonb_set(metadata, '$.quotas.runs', (metadata ->> '$.quotas.runs') + 1),
38 updated_at = unixepoch()
39WHERE session_id = 'sess_0195f410a9c2';
41-- 5. Read Back Clean Text JSON When Returning to HTTP/API Layer (Uses idx_sessions_plan_user!)
42EXPLAIN QUERY PLAN
43SELECT session_id, user_id, json(metadata) AS metadata_json
44FROM user_sessions
45WHERE plan_tier = 'pro';

No replies yet

Every reply is a note. Start a discussion, ask a question, or attach a code snippet.

Related SQLite 3.50+ & Database, Cache & SQL Blueprints

Production-ready configurations and diagnostics from the verified Supanote library.

All Database, Cache & SQL →
Interactive Developer Workbench: Online JSON Formatter, Validator & Viewer

Validate, pretty-print, minify, and inspect JSON API responses with exact syntax error pinpointing.

Open JSON Formatter →

Share Note

Open
Download SVG
Social Card Preview
Open on mobile
Point your phone camera to open this note directly

Report this note

Notification