简体   繁体   English

用于存储问卷和多项选择答案的数据库模式

[英]database schema for storing questionnaires and multiple choice answers

We need some help in designing database schema for questionnaires and multiple choice answers. 在设计用于问卷和多项选择答案的数据库架构时,我们需要一些帮助。

The first options is to design a question table and an answer table like below 第一种选择是设计一个问题表和一个答案表,如下所示

 * Question Table
   - question_id    auto integer
   - question       varchar

 * Answer Table
   - user_id        integer
   - question_id    integer
   - answer         integer 

However, the issue with this design is that, when a user submit answers to questionnaires, multiple rows need to be inserted and hence both write as well retrieving will be slower. 但是,这种设计的问题在于,当用户提交对问卷的答案时,需要插入多行,因此写入和检索都将变慢。 Also, the table will grow very big. 而且,桌子会很大。 However, the advantage is that it's expandable and new questions can be added easily. 但是,这样做的好处是它可以扩展并且可以轻松添加新问题。

Another approach is to have all the answers in one row but in different column, like this 另一种方法是将所有答案放在同一行但在另一列中,如下所示

 * Answer Table
   - user_id        integer
   - answer_1       integer 
   - answer_2       integer 
   ...
   - answer_n       integer 

Advantage is that, at a time only one row to write or retrieve and hence it will be much faster than the first approach. 优点是,一次只写入或检索一行,因此比第一种方法快得多。 However, schema will be rigid and if any new questions is added, db schema will have to be changed to accomodate a new column. 但是,模式将是严格的,并且如果添加任何新问题,则必须更改数据库模式以适应新列。

We have over 3 million users and multiple questionnaires per user. 我们有超过300万用户,每个用户有多个调查表。 Hence the speed is definitely a criterion. 因此,速度绝对是一个标准。 Based on this criterion, which one do you prefer? 根据这一标准,您更喜欢哪一个? Any other alternatives? 还有其他选择吗?

Thanks 谢谢

The intended denormalization you propose in the second case will certainly yield some performance gains. 您在第二种情况下建议的预期非规范化肯定会带来一些性能提升。 The big question is if you need to be able to search through data efficiently - you may not be able to, let's say, collect advanced statistics on answers (ie how many people gave between 3 and 6 answers). 最大的问题是,您是否需要能够高效地搜索数据-可能无法收集关于答案的高级统计信息(即,有多少人给出3至6个答案)。

If you don't need statistics, the second option is better (performance-wise). 如果您不需要统计信息,则第二种选择更好(在性能方面)。 If you do, maybe you should stick to the normal form. 如果这样做,也许您应该坚持常规形式。

A compromise is using JSON instead of columns to store data. 一种折衷方法是使用JSON而不是列来存储数据。 Results may be stored in something like PostgreSQL's jsonb column, which may be queried easily. 结果可能存储在PostgreSQL的jsonb列之类的jsonb ,可以轻松查询。 Your question is tagged mysql , though, so I don't know if it's an option for you. 但是,您的问题被标记为mysql ,所以我不知道它是否适合您。

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

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