简体   繁体   English

在 postgres 中使用动态模式

[英]Using dynamic schema with postgres

I have to decide how I want to implement dynamic schema.我必须决定如何实现动态模式。 I will try to put some more description where my problem is.我将尝试在我的问题所在的地方添加更多描述。

I use the postgres DB in latest version and app is written in Ruby and Hanami.我使用最新版本的 postgres DB,应用程序是用 Ruby 和 Hanami 编写的。 I have requirement to implement view where I will display list of users in our application (table view).我需要实现视图,我将在我们的应用程序中显示用户列表(表视图)。 Here comes first requirement that it needs to contain dynamic data.这是它需要包含动态数据的第一个要求。 For example user has name and birth_date and I store this data in Users table.例如,用户有姓名和生日,我将此数据存储在用户表中。 In other table I store metrics related to user like number of views and comments.在其他表中,我存储了与用户相关的指标,例如查看次数和评论数。 This is only part of data.这只是数据的一部分。 I have couple more tables related to my User and I need to join them into one view.我有几个与我的用户相关的表,我需要将它们加入一个视图。 It doesn't sound very dynamic.听起来不是很动态。 I know.我知道。 But I have another requirement.但我还有一个要求。 I want to allow users to add custom column to my table view.我想允许用户将自定义列添加到我的表视图中。

At this point we have table with following predefined columns:此时,我们有包含以下预定义列的表:

name, birth_date, number_of_views, number_of_comments 

and we can add some custom ones.我们可以添加一些自定义的。

Now It becomes dynamic.现在它变得动态了。 It is not the end.这不是结束。 I need to add filtering on this columns.我需要在此列上添加过滤。 So for example if given user has columns I mentioned earlier I should allow to filter on all of them (predefined and custom).因此,例如,如果给定用户具有我之前提到的列,我应该允许过滤所有列(预定义的和自定义的)。 I am trying to investigate how I should implement this feature.我正在尝试调查我应该如何实现此功能。

Now the question part :) I am thinking about how I should store this data.现在是问题部分:) 我正在考虑如何存储这些数据。 I am thinking about storing it as a JSONB column in my postgres.我正在考虑将它作为 JSONB 列存储在我的 postgres 中。 Scale of this is about 5kk of users.这个规模大约是 5kk 的用户。 So this mean 5kk rows in this table.所以这意味着该表中有 5kk 行。 Not so much.没有那么多。 I am wondering about filtering this data using dynamic filters.我想知道使用动态过滤器过滤这些数据。 I tried to make some benchmarks for 5kk table and it worked well if I used pagination (without indexes).我试图为 5kk 表做一些基准测试,如果我使用分页(没有索引),它运行良好。 For querying whole dataset it took like 10sec so wasn't perfect.查询整个数据集需要 10 秒,所以并不完美。

Questions问题

  1. Is JSONB good idea? JSONB 是个好主意吗? I am not sure If I can define proper indexes if I will filter dynamically on this data (every query can be different).如果我将动态过滤此数据(每个查询可能不同),我不确定是否可以定义适当的索引。 How to define indexes for this scenario?如何为这种情况定义索引?
  2. Is a good idea to store predefined columns as normal columns in table and only custom ones in JSONB?将预定义的列存储为表中的普通列并且仅将自定义列存储在 JSONB 中是个好主意吗? Will it give me any profit?它会给我带来任何利润吗?
  3. I should I use postgres to that?我应该使用 postgres 吗? I don't believe that postgres isn't good enough for this scale and I prefer to not change tool.我不相信 postgres 对于这种规模不够好,我更喜欢不改变工具。

Thanks for all answers.感谢所有的答案。 I don't have huge experience with dynamic schemas so every tip can be usefull ;P我在动态模式方面没有丰富的经验,所以每个技巧都有用;P

I would recommend using NoSQL database like mongoDB.我建议使用像 mongoDB 这样的 NoSQL 数据库。 Your problem statement requires schema-less document store supporting fast reads ie.您的问题陈述需要支持快速读取的无模式文档存储,即。 MongoDB or Couchbase which has great support MongoDB 或 Couchbase 有很大的支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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