[英]Should I use separate tables for a flashcards app?
I'm designing a MySQL database for a flashcards app. 我正在为抽认卡应用程序设计MySQL数据库。 I have a table called "courses", where i keep courses' ids, names and descriptions and a table called items, where I keep all the flashcards.
我有一个名为“课程”的表,其中保存了课程的ID,名称和说明,还有一个名为“项目”的表,其中保存了所有抽认卡。 The table items keeps a unique id, id of the course, as well as, questions, answers and more details.
表格项保留唯一的ID,课程ID以及问题,答案和更多详细信息。 There is also a table called repetitions, where I store data which flashcards a user has memorised.
还有一个称为重复的表,我在其中存储用户已记忆的抽认卡的数据。 If I want to get a flashcard a user hasn't memorised yet, I query the database with LEFT JOIN so I get all data from flashcards and rows from repetitions that match.
如果我想获得用户尚未记忆的抽认卡,则使用LEFT JOIN查询数据库,以便从抽认卡获取所有数据,并从匹配的重复项获取行。 However, I have to specify what course a certain flashcard belongs to, so my query looks more o less like that:
但是,我必须指定某个抽认卡所属的课程,因此我的查询看起来像这样:
SELECT *
FROM (SELECT *
FROM flashcards
WHERE course='coursename') AS flashcards
LEFT JOIN repetitions
ON flashcards.id=repetitions.flashcardid
WHERE repetitions.lastrepetition IS NULL
LIMIT 1;
Would it be better to have separare tables for each course so I wouldn't have to specify what course I mean? 最好为每门课程都使用单独的表格,这样我就不必指定我的意思了? Would that speed up the database?
这样可以加快数据库速度吗? There won't be many courses (about 10-20) and each course would consist of about 5k-10k flashcards.
不会有很多课程(大约10至20个),每个课程将包含大约5k至10k的抽认卡。
Sorry, I forgot to say that, if a user hasn't memorise a flashcard, there won't be any equivalent record in the table repetitions. 抱歉,我忘记说了,如果用户没有记住抽认卡,则表重复中将没有任何等效记录。 What I actually do is: 1. I choose all flashards WHERE coursename = 'coursename', 2. I choose all repetitions WHERE user = 'user' AND coursename = 'coursname, 3. and then I LEFT JOIN the records from the first table with the records from the second table.
我实际要做的是:1.在所有课程表=课程名称='课程名称'的情况下,选择2.在用户='用户'和课程名称='课程名称'的所有重复项中选择3.然后从第一个表中保留记录与第二个表中的记录。 I get all flashcards from a certain course and some of these records have their equivalents in the repetitions table, 4. then I use WHERE repetitions IS NULL and I get only those flashcards that don't have their uqivalents in the repetitions table.
我从某个特定的课程中获取了所有抽认卡,其中一些记录在重复表4中具有其等效项。然后,我使用WHERE重复IS NULL,并且仅获得那些在重复表中没有其等价物的抽认卡。
SELECT * FROM
(SELECT * FROM flashcards WHERE course='coursename') AS f
LEFT JOIN
(SELECT * FROM repetitions WHERE user='user' AND course='coursename') AS r
ON f.id=r.flashcardid
WHERE repetitions IS NULL LIMIT 1)
The above query works fine, but I wonder whether it won't require too much RAM/memory, if there are more people using the app. 上面的查询工作正常,但是我想知道是否有更多的人在使用该应用,它是否不需要太多的RAM /内存。
The only better solution I have atm is to choose only ids from the flashcards table and set the LIMIT to the user's limit (let's say 30 flashcards per session). 我有atm的唯一更好的解决方案是仅从抽认卡表中选择ID,并将LIMIT设置为用户的限制(例如,每个会话30张抽认卡)。 Then instead of 30 times I'll use this query only once.
然后,我将只使用一次此查询,而不是30次。 And I can pass the id's in a GET variable.
而且我可以在GET变量中传递ID。 (The only problem with this solution is, that if a user studies flashcards on his PC and then moves to a mobile and forgets to switch off the PC's session and comes back to the PC, some repetitions might get overwritten. Anyway, this would be a very rare case, but still possible.)
(此解决方案的唯一问题是,如果用户在PC上学习闪存卡,然后转到移动设备,却忘记关闭PC的会话并返回PC,则某些重复操作可能会被覆盖。无论如何,这将是一种非常罕见的情况,但仍然有可能。)
Thanks for your help! 谢谢你的帮助! Your suggestions actually happend to be right.
您的建议实际上恰好是对的。 I've just figured out where I made a mistake - in my original query, I unnecessarily added 'WHERE repetitions.course='coursename' condition.
我只是想出我在哪里犯了一个错误-在我的原始查询中,我不必要地添加了“ WHEREpetitions.course ='coursename'”条件。 However, I still have to use brackets for the right table to determine the user.
但是,我仍然必须在右表中使用方括号来确定用户。
SELECT * FROM flashcards AS f
LEFT JOIN (SELECT * FROM repetitions WHERE user='username') AS r
ON f.id=r.flashcardid
WHERE f.course='coursename' AND r IS NULL
You should not split up the courses into there own tables. 您不应将课程拆分为自己的表格。
couldn't you do something like this instead? 你不能做这样的事情吗?
select * from flashcards
left join repititions
on flashcards.id = repititions.flashcardid
where course = 'coursename'
and repititions.lastrepitition is null
limit 1
I see no reason to add the overhead and headaches of a separate table for each course. 我认为没有理由为每个课程增加单独的表格的开销和麻烦。 Also, there's no need for that subquery.
此外,也不需要该子查询。 Your query can be written as:
您的查询可以写为:
SELECT *
FROM flashcards f
LEFT JOIN repetitions r
ON f.id = r.flashcardid
WHERE f.course = 'coursename'
AND r.lastrepetition IS NULL
LIMIT 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.