V
V
Vitaly Stolyarov2016-12-04 22:55:51
PostgreSQL
Vitaly Stolyarov, 2016-12-04 22:55:51

JSONB search in Postgresql?

The administrator creates fields by directly specifying their type string, int, etc. (this is a separate Fields table), but the user must fill in the same fields with data, so the UserData table is needed, in which data can be presented in two versions:

id:int | value:string | field_id:int

or
id:int | data:jsonb

where the key can be field ID
Fields
| id | type |
| 1 | integer |
| 2 | varchar |
| 3 | real |
user data
| data |
| {1:23, 2:"test string", 3:45.56} |
That is,
in the first case, each record contains string data and refers to a specific field,
in the second case: data for all filled fields is contained in one record.
Which method is preferable if you need to take into account the speed of data sampling for a particular field?
The value for this field can be integer or real, which does not allow for correct sorting in the first method, more precisely, it will be sorted as a string

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Shelemetiev, 2016-12-05
@zoroda

If you understand your task correctly, selecting from one table on a JSONB field indexed by GIN using the @> operator will be much faster than selecting from several related tables (EAV model).
Proof
Read more here in the comments to the answers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question