You have a couple of interesting questions here.
1. Query logic in the database vs. query logic in Java
It depends. First off, in PostgreSQL specifically, jsonb
has better index support. I'm no expert on the topic, but I'm pretty sure you will come to this conclusion once you benchmark 1-2 things. Here's an interesting blog post comparing different data types for performance in PostgreSQL:
http://www.databasesoup.com/2015/01/tag-all-things.html
... which brings me to the point of whether to put predicates into your SQL query, or execute them later in Java. In general: Put it in the database. If your predicate is very selective, then you can:
- Avoid a lot of unnecessary I/O in the database
- Avoid a lot of unnecessary I/O with the database
Both of which result in much lower latency of your queries. If your predicate is not very selective, then this is hardly ever a problem, except under extreme load. But still, if your system is under extreme load and your predicates are selective, you will still greatly reduce that load if you run the predicate in the database.
2. jOOQ support for JSON predicates
jOOQ currently doesn't offer any out-of-the-box support for JSON(B) predicates, but you can easily build a utility yourself using jOOQ's plain SQL support:
http://www.jooq.org/doc/latest/manual/sql-building/plain-sql
In essence, just write:
public static Condition someJsonPredicate(Field<?> someJsonColumn, String someValue) {
return DSL.condition("some_json_predicate({0}, {1})",
someJsonColumn, DSL.val(someValue));
}
3. org.json vs Gson
I won't answer this part here as your benchmark may differ from mine.