在 postgres 和 rails 中的数组、json 和 hstore 数据类型之间进行选择

choosing between arrays, json and hstore datatypes in postgres and rails

我有一些字段与其他字段相关,我认为将其放在一个数据库列中会更清晰。

例如,假设我有这些字段: parking_available:boolean paid_parking:boolean free_parking:boolean parking_price:integerreservation_required:boolean

因此,如果我使用 arraysjsonhstore,那么我可能 storefetch 上述参数的值按字符串顺序排列考虑数据类型。但是我不知道哪个更好,还是坚持使用各个数据库字段。有人可以解释 arraysjsonhstore 的用例以及它们的性能优势吗?

虽然,它没有直接回答你的问题,How to persist hashes in rails application with postgresql 说明了 hstore,JSON 和 JSONB

Although it may not be a common case to store other types than string in a dictionary, it is still worth to have in mind that hstore supports only string data.

...

it (JSON) supports nested objects and more datatypes. It also has more operators that are very well described in documentation. If you are using JSON somewhere in your application already and want to store it directly in database, then the JSON datatype is perfect choice.

...

If you still wonder whether MongoDB will be better choice for you needs, check JSONB support introduced in 9.4 version of postgres, which is real data type with binary storage and indexing, a structured format for storing json

根据我对上面源码的解释,你应该使用JSON或者JSONB(如果你已经在使用PostgreSQL 9.4+或者没有改变到9.4+的抑制因素)