简体   繁体   中英

Is it a good practice to store data in json format in the postgresql?

In my latest project, I store historical restaurant orders with fields such as total price, vat of the transaction, items bought in the order, price of those items.

I also store offers made by the restaurants with discount, items that are included in the offers, the audience that is eligible for the audience. Once fetched orders or offers data is not going to be edited or updated.

I chose to store such information in PostgreSql. I have a table Order with relations to an OrderTransaction, to an OrderItem Table relation which itself links to Item Table I have a table Offer which is linked to multiple Table: OfferDiscount (which represents the type (Buy 1 Get 1, Flat Discount on the whole order....) and value of discount), OfferAudience, which represents

Most queries would need to load the items for the Order and all the relations for the Offer. There are not high risk of concurrrent writings to those tables.

Should i serialize information from those relations directly in the main table (Order, Offer) in postgre Json format for faster access and simplification. This would also help the fact that if for instance OfferDiscount rows are deleted, that would not affect past data of Offers that would contain also some fields from OfferDiscount directly in its row.

Currently I have:

@Entity()
class Order {
  @PrimaryGeneratedColumn()
  public id?: number;

  @Column()
  public total: number;

  @Column()
  public discount: number;

  // TODO : Serialize ?
  @OneToMany(() => OrderItem, (orderItem) => orderItem.order)
  public items: OrderItem[];

  // TODO : Serialize ?
  @OneToMany(
    () => OrderTransaction,
    (orderTransaction) => orderTransaction.order
  )
  public transactions: OrderTransaction[];

}

I have been suggested to serialize the data as following as the order is immutable once created:

@Entity()
class Order {
  @PrimaryGeneratedColumn()
  public id?: number;

  @Column()
  public total: number;

  @Column()
  public discount: number;

  @OneToMany(() => OrderItem, (orderItem) => orderItem.order)
  public items: OrderItem[];

  @Column({type: 'jsonb', array: true, nullable: true})
  contentItems: object[];

  @OneToMany(
    () => OrderTransaction,
    (orderTransaction) => orderTransaction.order
  )
  public transactions: OrderTransaction[];

  @Column({type: 'jsonb', array: true, nullable: true})
  contentTransactions: object[];

}

Is that a good practice to store data as such, as it will indeed simplify query fetching only one table without all the relations as well as prevent loss of the orginal data if the relation row is changed.

Thanks!

No, that is not good practice.

Create several normalized tables with foreign key relationships and regular data types and store the data that way. That will make for simpler SQL statements, which will lead to better performance.

Relational database are optimized for joining tables with many row in them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM