获取问题的最后答案 ID

发布于 2024-12-07 02:12:11 字数 607 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的新欢旧爱 2024-12-14 02:12:11

如果您的 Id 是自动递增的,您可以根据 Id 获得最后的答案:

SELECT TOP 1 AnsID
WHERE Uid=@UserId AND Qid=@QuestionID
ORDER BY Id DESC

关于性能:您应该在 Uid AND Qid AND ID 上放置非聚集索引并包含 AnsID

您可以避免在此表中使用时间戳列。

If your Id is autoincrementing, you can get last answer based on Id:

SELECT TOP 1 AnsID
WHERE Uid=@UserId AND Qid=@QuestionID
ORDER BY Id DESC

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.

三生殊途 2024-12-14 02:12:11

1:对于第一种方法:

要获取给定问题 id 的最后一个答案 id,您可以使用此脚本:

   --index creation
   CREATE UNIQUE INDEX IUN_temptblUserAnswer_qid_id ON temptblUserAnswer(uid,qid,id DESC);

   --query
    DECLARE @QuestionID INT, @UserID INT;
    SELECT @QuestionID=123, @UserID = 456;

    SELECT TOP 1 ansid
    FROM temptblUserAnswer
    WHERE qid = @QuestionID
    AND uid = @UserID
    ORDER BY id DESC;

在本例中,我假设最后一个答案是由最后一个 temptblUserAnswer.id。我还假设 temptblUserAnswer.idIDENTITY(1,1)
问题:如果有人在 temptblUserAnswer 表上运行临时插入,则可能会得到错误的结果。

2:对于第二种方法:

您应该使用更高精度的日期/时间数据类型(对于SQL版本<=SQL2005:DATETIME是唯一的选项,对于SQL版本>SQL2005您可以使用DATETIME2( 7) 数据类型和 SYSDATETIME() 函数而不是 GETDATE()/CURRENT_TIMESTAMP 函数)。即使如此,您也可以使用相同的时间戳得到两个或多个答案。在这种情况下,您可以使用“temptblUserAnswer.id”列作为第二个条件。

   --index creation
   CREATE UNIQUE INDEX IUN_temptblUserAnswer_qid_id ON temptblUserAnswer(uid,qid,timestamp DESC, id DESC);

   --query
    DECLARE @QuestionID INT, @UserID INT;
    SELECT @QuestionID=123, @UserID = 456;

    SELECT TOP 1 ansid
    FROM temptblUserAnswer
    WHERE qid = @QuestionID
    AND uid = @UserID
    ORDER BY timestamp DESC, id DESC;

1: For the first method:

To get the last answer id for a given question id, you can use this script:

   --index creation
   CREATE UNIQUE INDEX IUN_temptblUserAnswer_qid_id ON temptblUserAnswer(uid,qid,id DESC);

   --query
    DECLARE @QuestionID INT, @UserID INT;
    SELECT @QuestionID=123, @UserID = 456;

    SELECT TOP 1 ansid
    FROM temptblUserAnswer
    WHERE qid = @QuestionID
    AND uid = @UserID
    ORDER BY id DESC;

In this case, I have assumed that the last answer is given by the last temptblUserAnswer.id. Also I have assumed that temptblUserAnswer.id is IDENTITY(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.

   --index creation
   CREATE UNIQUE INDEX IUN_temptblUserAnswer_qid_id ON temptblUserAnswer(uid,qid,timestamp DESC, id DESC);

   --query
    DECLARE @QuestionID INT, @UserID INT;
    SELECT @QuestionID=123, @UserID = 456;

    SELECT TOP 1 ansid
    FROM temptblUserAnswer
    WHERE qid = @QuestionID
    AND uid = @UserID
    ORDER BY timestamp DESC, id DESC;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文