Does JSONB make PostgreSQL arrays useless?

后端 未结 2 1946
醉话见心
醉话见心 2021-02-07 11:16

Suppose that you want to store \"tags\" on your object (say, a post). With release 9.4 you have 3 main choices:

  • tags as text[]
  • tags as jsonb
2条回答
  •  半阙折子戏
    2021-02-07 11:41

    I have used both a normalized schema and just a plain text field with CSV separated values instead of custom data types (instead of CSV you can use JSON or whatever other encoding like www-urlencoding or even XML attribute encoding). This is because many ORM's and database libraries are not very good at supporting custom datatypes (hstore, jsonb, array etc).

    @ErwinBrandstetter missed a couple of other benefits of normalized one being the fact that it is much quicker to query for all possible previously used tags in a normalized schema than the array option. This is a very common scenario in many tag systems.

    That being said I would recommend using Solr (or elasticsearch) for querying for tags as it deals with tag count and general tag prefix searching far better than what I could get Postgres to do if your willing to deal with the consistency aspects of synchronizing with a search engine. Thus the storage of the tags becomes less important.

提交回复
热议问题