在MYSQL中使用不同数据类型保存测验答案的最佳实践是什么? [英] What is the best practice to save a quiz answers with different data types in MYSQL?

查看:104
本文介绍了在MYSQL中使用不同数据类型保存测验答案的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:第1部分-开始:

我们想获取客户数据,但是要获取哪种数据?没有指定!实际上,我们希望有一个动态应用程序,管理员可以定义新问题(他可以设置答案的数据类型,长度和其他规则),还可以停用旧问题!

We want to get our customers data, but which kind of data? they are not specified! actually we want to have a dynamic application that the admin can define new questions (he can set the answer's data type, length, and other rules) and he also can deactivate old questions!

(我不想使用EAV模式设计,但我找不到其他方法)

(I don't want to use EAV schema design, but I can't find the alternative way)

更新:第1部分-结束:

所以我决定创建一个测验应用程序,管理员可以定义问题,这些问题的答案数据类型可以不一样!

so I decided to create a quiz application, admin can define questions which their answers data type can be different!

示例:

问题ID 1 :你叫什么名字? answer :约翰(varchar)

question ID 1 : what is you name? answer: John (varchar)

问题ID 2 :您多大了? answer :25 (整数)

question ID 2 : how old are you? answer: 25 (integer)

问题ID 3 :您的数量是多少每小时工资? answer :30.65 (十进制)

question ID 3 : how much is your salary per hour? answer: 30.65 (decimal)

问题ID 4 :描述自己? answer :我很好... (文本)

question ID 4 : describe yourself? answer: I'm so kind ... (text)

更新:第2部分-开始:

保存我想到的答案3个选择:

to save the answers it comes to my mind 3 choices:

UPDATE:第2部分-结束:


  1. 创建这样的表(EAV模式设计):



    Table profile_answers
    id int [pk, increment]
    question_id int
    profile_id int
    answer text

如您所见,我已将所有答案另存为文字
我知道它有效,但这是最好的方法吗?实际上,此应用将拥有数百万个答案,我们希望通过计算机分析答案(例如:获取客户的平均年龄,结婚的客户,薪水大于30.52的客户等等)来实现具有最佳性能的方式!

as you see I've save all answers as text! I know that it works, but is it the best way? actually this app is going to have millions of answers and we want to analyze the answers by machine (for example: getting customers average age, getting married customers, customers with salary > 30.52 and etc) so I want to implement the way with the best performance!


  1. 创建这样的表(EAV模式设计):



Table answers
id int [pk, increment]
question_id int
profile_id int
integer_value int
decimal_value decimal
varchar_value varchar
boolean_value boolean
longtext_value longtext

现在我可以将数据保存在适当的字段中,并在其他字段中放入 NULL (答案的数据类型为

now I can save data in the proper field and put NULL in the other fields (the answer's data type will be determined while defining it's question)

例如:

question ID 1 : what is you name? answer: John (varchar)

id 1
profile_question_id 1
profile_id 1
integer_value NULL
decimal_value NULL
varchar_value John
boolean_value NULL
longtext_value NULL

 ----------------
question ID 2 : how old are you? answer: 25 (integer)

id 2
profile_question_id 2
profile_id 1
integer_value 25 
decimal_value NULL
varchar_value NULL
boolean_value NULL
longtext_value NULL

 ----------------
question ID 3 : how much is your salary per hour? answer: 30.65 (decimal)

id 3
profile_question_id 3
profile_id 1
integer_value NULL
decimal_value 30.65
varchar_value NULL
boolean_value NULL
longtext_value NULL

更新:第3部分-开始:


  1. 当管理员添加新问题时,我可以在答案表中添加一列(更改表的模式)像这样

  1. when admin adds a new question, I can add a column to answers table (change table's schema) like this

更新:第3部分-结束:

这与性能有关!最好的方法是什么?
如果都不是最好的,并且我应该重新设计数据库什么是正确的数据库设计?

It's all about performance! what's the best way? If none of them is not the best one and I should redesign the database what is the correct database design?

推荐答案

当您在设计时知道架构时,关系数据库最有效。您的要求是在运行时仅知道模式(针对每个测验)。

Relational databases work best when you know the schema at design time. Your requirement is one where the schema (for each quiz) is known aonly at run time.

EAV似乎可以解决-并且确实可以使您存储数据。但是很快地查询它几乎是不可能的-试着寻找所有32岁以上,年薪在20至30K之间并结婚的客户。使用适当的数据类型会有所帮助,但是您的查询将迅速变得极其复杂且无法优化。

EAV seems like a work around - and it does, indeed, allow you to store the data. But querying it very quickly becomes almost impossible - imagine trying to find all customers over the age of 32 who earn between 20 and 30K and who are getting married. Using appropriate data type will help, a little, but your queries will rapidly become extremely complex and, and impossible to optimize.

好消息是,大多数数据库引擎都支持XML或JSON文档,具有相当好的查询性能。例如,MySQL的 JSON 很好。

The good news is that most database engines support either XML or JSON documents, with pretty good query performance. MySQL does JSON pretty well, for instance.

我将使用主要概念作为关系实体(客户,测验,问题)对系统进行建模,并将答案以JSON的形式存储在MySQL中。

I would model my system using the major concepts as relational entities (customer, quiz, question), and store the answers as JSON in MySQL.

这篇关于在MYSQL中使用不同数据类型保存测验答案的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆