timgit / pg-boss

Queueing jobs in Postgres from Node.js like a boss
MIT License
2.05k stars 157 forks source link

Consider json instead of jsonb #53

Closed bradleyayers closed 6 years ago

bradleyayers commented 6 years ago

I've found that in Postgres, json is both faster and more space efficient than jsonb. These are probably two more desirable traits than having Postgres validate that it's valid JSON.

Interested in your thoughts on this.

timgit commented 6 years ago

I originally decided on jsonb based on a coin toss. 😁 Actually, I wasn't sure if I was going to want to issue arbitrary json queries against the job table or not. However, I'm now of the persuation that I should discourage such use cases, as the job table can get pretty busy, and I want to limit ops against it whenever possible.

bradleyayers commented 6 years ago

That was the impression I gathered from reading through some of the discussions on different issues. For now I've gone with json instead of jsonb, since I'm managing schema migrations outside of the library anyway. I'll report back if I hit any issues.

pyrossh commented 6 years ago

Sorry to reopen this issue but isn't jsonb faster then json type. Since json is a binary format it has a better way to search faster than parsing the entire json in text form.

According to the postgres docs, https://www.postgresql.org/docs/9.4/static/datatype-json.html A quotation from there,

There are two JSON data types: json and jsonb. They accept almost identical sets of values as input. The major practical difference is one of efficiency. The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage.

Also this SO answer, https://stackoverflow.com/questions/22654170/explanation-of-jsonb-introduced-by-postgresql?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Both json & jsonb allows you to store a valid JSON value (defined in its spec). The only difference between json & jsonb is their storage: json is stored in its plain text format, while jsonb is stored in some binary representation There are 3 major consequences of this: jsonb usually takes more disk space to store than json (sometimes not) jsonb takes more time to build from its input representation than json json operations take significantly more time than jsonb (& parsing also needs to be done each time you do some operation at a json typed value) When jsonb will be available with a stable release, there will be two major use cases, when you can easily select between them: If you only work with the JSON representation in your application, PostgreSQL is only used to store & retrieve this representation, you should use json. If you do a lot of operations on the JSON value in PostgreSQL, or use indexing on some JSON field, you should use jsonb.

bradleyayers commented 6 years ago

json is faster if you're not using any processing functions on it (which is the case with pg-boss).

slightly slower to input due to added conversion overhead