获取问题的最后答案 ID
在 MCQ 测试中,当用户再次回到同一问题时,如何跟踪该问题的最后一个答案?
以下是表结构
Method: 1
temptblUserAnswer
id uid qid ansid
1 1 1 5
2 1 2 6
我应该更新该特定问题的表值吗?
或
我应该使用以下表结构吗?
Method: 2
temptblUserAnswer
id uid qid ansid timestamp
1 1 1 5 2011-09-28 11:54:32
2 1 2 12 2011-09-28 11:58:40
3 1 1 7 2011-09-28 12:02:50
在这里,借助时间戳,我可以找到任何特定问题的最后答案。
现在的问题是,该遵循哪种方法,哪种方法以性能为导向?
或者有更好的建议吗?我应该使用方法:1 并在 uid、ansid 列上应用索引吗?
In M.C.Q test, when user come back to the same question again how can I track the last answer given for the question?,
following is the table structure
Method: 1
temptblUserAnswer
id uid qid ansid
1 1 1 5
2 1 2 6
Should I update the table value for that particular Question?
OR
Should I use following table structure?
Method: 2
temptblUserAnswer
id uid qid ansid timestamp
1 1 1 5 2011-09-28 11:54:32
2 1 2 12 2011-09-28 11:58:40
3 1 1 7 2011-09-28 12:02:50
Here with the help of timestamp I can find the the last answer for any particular question.
Now the question is, which method to follow, and which will be performance oriented?
Or any better suggestion for the same? Should I go with Method:1 and apply the index on uid, ansid column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 Id 是自动递增的,您可以根据 Id 获得最后的答案:
关于性能:您应该在 Uid AND Qid AND ID 上放置非聚集索引并包含 AnsID
您可以避免在此表中使用时间戳列。
If your Id is autoincrementing, you can get last answer based on Id:
About performance:you should put non-clustered index on Uid AND Qid AND ID and Include AnsID
You can avoid of using timestamp column in this table.
1:对于第一种方法:
要获取给定问题 id 的最后一个答案 id,您可以使用此脚本:
在本例中,我假设最后一个答案是由最后一个
temptblUserAnswer.id
。我还假设temptblUserAnswer.id
是IDENTITY(1,1)
。问题:如果有人在
temptblUserAnswer
表上运行临时插入,则可能会得到错误的结果。2:对于第二种方法:
您应该使用更高精度的日期/时间数据类型(对于SQL版本<=SQL2005:DATETIME是唯一的选项,对于SQL版本>SQL2005您可以使用DATETIME2( 7) 数据类型和 SYSDATETIME() 函数而不是 GETDATE()/CURRENT_TIMESTAMP 函数)。即使如此,您也可以使用相同的
时间戳
得到两个或多个答案。在这种情况下,您可以使用“temptblUserAnswer.id”列作为第二个条件。1: For the first method:
To get the last answer id for a given question id, you can use this script:
In this case, I have assumed that the last answer is given by the last
temptblUserAnswer.id
. Also I have assumed thattemptblUserAnswer.id
isIDENTITY(1,1)
.Problem: if somebody runs an ad-hoc insert on
temptblUserAnswer
table it's possible to get wrong results.2: For the second method:
You should use a date/time data type with higher precision (for SQL version<=SQL2005: DATETIME is the only option, for SQL version>SQL2005 you can use DATETIME2(7) data type and SYSDATETIME() function instead of GETDATE()/CURRENT_TIMESTAMP functions). Even so, you could get two or more answers with the same
timestamp
. In this case, you could use 'temptblUserAnswer.id' column as the second criteria.