What are some useful use-cases for this? I can already use JSON in Sqlite by doing parsing in the client outside of the Sqlite API. Any examples where I'd want to use this instead? I'm guessing for where clauses in queries, perhaps? Can I create an index on a field within a JSON tuple?
There is the possibility of storing a binary-encoded json object, which uses far less space than regular json, and since the server is doing the encoding you aren't going to need custom client side libraries to decode it. There is also the possibility of magic 'compression' by having the server automagically extract common schema elements of the stored data to probably cut the storage size in half again.
JSON with Sqlite
61–70 of 74 posts
Re: JSON with Sqlite
#62I recently wrote an SQLite extension that lets me query stored Protobuf messages. I was inspired by the JSON extension. Adding virtual tables to SQLite with an extension is tricky but kind of magical when you finally have the full query language to play with. https://github.com/rgov/sqlite_protobuf
Do you have performance numbers, especially in comparison to json1? Wondering how much benefit you get from a binary encoding scheme.
Any query is going to require deserializing every row in the table (JSON and Protobuf alike) which is basically a non-starter for any project with performance requirements. And I believe that Protobuf messages are decoded as a whole, rather than just the desired field, which is going to be slower.
Re: JSON with Sqlite
#63What are some useful use-cases for this? I can already use JSON in Sqlite by doing parsing in the client outside of the Sqlite API. Any examples where I'd want to use this instead? I'm guessing for where clauses in queries, perhaps? Can I create an index on a field within a JSON tuple?
Re: JSON with Sqlite
#64Earlier quoted context omitted.
According to [1], ISO/IEC 9075:2016 specifies JSON functionality in ISO SQL, though I don't have access to it/didn't buy the standard text. I also don't know whether Postgres etc. is designed to follow the standard, as Postgres' implementation predates it, but would expect sqlite to. [1]: https://modern-sql.com/blog/2017-06/whats-new-in-sql-2016
Postgres 11 doesn't follow the standard at all (as per your link). A third party implemented the standard [0] which was submitted on pghackers[1] very late in the Postgres 11 cycle[2] (at 75k gzipped the patch is pretty humongous). A quick googling doesn't show any information pertaining it being integrated (in part or full) into pg12 so IDK. edit: apparently https://commitfest.postgresql.org/17/1471/ has been "Waiti…
https://www.postgresql.org/message-id/a3be6a7a-77d3-0e88-4f9... https://www.postgresql.org/message-id/c2f32c9f-9a69-202b-a8a...
> I see no comment or requests or reviews so…
That happens on the mailing list...
Re: JSON with Sqlite
#65Earlier quoted context omitted.
Do you have performance numbers, especially in comparison to json1? Wondering how much benefit you get from a binary encoding scheme.
I don’t, sorry. I did not build this with performance in mind, but for querying packet captures of a protocol built on Protobuf. Any query is going to require deserializing every row in the table (JSON and Protobuf alike) which is basically a non-starter for any project with performance requirements. And I believe that Protobuf messages are decoded as a whole, rather than just the desired field, which is going to be…
> Any query is going to require deserializing every row in the table (JSON and Protobuf alike) which is basically a non-starter for any project with performance requirements.
Apparently you can create indexes on the json functions, so I assume on your proto ones too.
> I believe that Protobuf messages are decoded as a whole, rather than just the desired field, which is going to be slower.
With the official library, yes, but it's an implementation/API choice. The wire format [1] can be skimmed fairly efficiently. Most significantly, message/byte/string fields all are written as tag number, length, data. So if there are submessages (entire trees) you don't care about, you can just skip over them. I've seen custom proto decoding things do this.
I think the most efficient approach would be to parse the path once per SQL query into a tag number-based path. And then have the per-row function just follow those with custom decoding logic. Unfortunately from my quick skim, it looks like SQLite's extension API doesn't really support this. You'd want it to build some context object for a given path/proto, then call extract with it a bunch of times, then tear it down. Still, I suppose you could do a LRU cache of these or something.
btw, I see your code is compiling a regex [edit: originally wrote proto by mistake] in the per-row path. [2] I haven't profiled, but I'd bet that's slowing you down a fair bit. You could just make it a 'static const std::regex* kPathElementRegexp = new std::regex("...")' to avoid this. (static initialization is thread-safe in C++. The heap allocation is because it's good practice to ensure non-POD globals are never destructed. Alternatively, there's absl::NoDestructor for this.)
[1] https://developers.google.com/protocol-buffers/docs/encoding
[2] https://github.com/rgov/sqlite_protobuf/blob/0a148ac6a5a2c02...
Re: JSON with Sqlite
#66I've been using the JSON1 extension for some time now (in production projects) and it's truly remarkable. I usually just dump the JSON-response data from an API to a "raw_data" table (typically one "updated_at" column and a second "json_data" one). At that point you can somehow normalize your schema, but only if you really have to! That is because you can get away with a NoSQL-like denormalized schema performance wis…
> (Yes, my dear aspring data scientist, do not load everything in a huge DataFrame, go learn yourself some SQL :-) ) This hits a little bit too close to home for me. I am quite proficient at writing performant SQL queries and recently started using Pandas. I find the data frame abstraction better for certain data manipulation tasks. Assuming there is enough RAM available is it still better to offload everything to th…
Re: JSON with Sqlite
#67Earlier quoted context omitted.
I don’t, sorry. I did not build this with performance in mind, but for querying packet captures of a protocol built on Protobuf. Any query is going to require deserializing every row in the table (JSON and Protobuf alike) which is basically a non-starter for any project with performance requirements. And I believe that Protobuf messages are decoded as a whole, rather than just the desired field, which is going to be…
Interesting library, thanks! > Any query is going to require deserializing every row in the table (JSON and Protobuf alike) which is basically a non-starter for any project with performance requirements. Apparently you can create indexes on the json functions, so I assume on your proto ones too. > I believe that Protobuf messages are decoded as a whole, rather than just the desired field, which is going to be slower.…
Re: JSON with Sqlite
#68Earlier quoted context omitted.
I see your point, but I'm very hesitant to change any configuration of SQLite. It kinda feels like one step too close to doing devops - which one wants to avoid by using SQLite I guess. Having said that, I do play around with PRAGMA statements when it's really needed, but usually tweaking the code usually works fine - even increasing the timeout is probably enough :D
I read that first sentence like 3 times and still don't get it.
Re: JSON with Sqlite
#69Earlier quoted context omitted.
I read that first sentence like 3 times and still don't get it.
Often people use SQLite for sheer convenience instead of perceived performance or resource efficiency i.e. because SQLite is zero config, works out of the box. Sure spinning up a docker container with PostgreSQL is easy enough but why do that when you can use the default standard library with SQLite already embedded and linker configed?
And it’s performance and reliability are a huge part of why it runs on millions of devices everywhere.
Your browser was never going to embed a build of PostgreSQL and Docker.
Enabling WAL can hardly be called config, it’s a one liner in every driver I’ve ever seen.
It’s like saying specifying the file directory SQLite uses is config.
Re: JSON with Sqlite
#70Earlier quoted context omitted.
One thing I like is when getting data from multiple tables. Let's say you have a product with multiple labels: usually you'll get one line per (product, label) tuple so you'll have to do some job application side if you what to get one [product, labels] object per product. With the json aggregate function you can get a (product, json array of labels) tuple per product and just have to do some json_decode in your appl…
> "Let's say you have a product with multiple labels: usually you'll get one line per (product, label) tuple so you'll have to do some job application side if you what to get one [product, labels] object per product." What's the payoff of doing [product, labels], particularly if that means a json array of labels? I briefly investigated doing that for my application but I found that it would increase disk space (not r…
When querying you do something like
select p.name, json_agg(l.value) as labels
from product p, label l