优化按难度排序题的查询

发布于 2024-11-09 06:26:55 字数 806 浏览 0 评论 0原文

我正在创建一个视图,其中包含必须按难度排序的问题。

我的想法是:

  • 首先放入新的问题(少于10个答案)
  • 其次放入没有任何错误答案的问题
  • 最后按难度排序的其他问题

计算难度的方法是 (answerCorrect / answerIn Correct)

问题是我认为对此视图的大量查询将会完成,并且我怀疑这个查询非常慢,而且我不知道是否有办法做得更快。

alter view questionLevel as select *, 0 as ordenacio from question where statsAnswerCorrect + statsAnswerIncorrect < 10
union
select *, 1 as ordenacio from question where 
statsAnswerCorrect + statsAnswerIncorrect >= 10
and statsAnswerIncorrect = 0
union
select *, 2 as ordenacio
from question where questionId IN (
select questionId from question where statsAnswerCorrect + statsAnswerIncorrect >= 10 
and  statsAnswerIncorrect > 0 )
order by ordenacio asc, ( statsAnswerCorrect / statsAnswerIncorrect) desc

有什么想法吗?

I'm creating a View that contains questions that have to be ordered by difficulty.

My idea is:

  • First put the new questions (less than 10 responses)
  • Second put the questions that don't have any wrong response
  • Finally the other questions ordered by difficulty

The way to calculate the difficulty is (answerCorrect / answerIncorrect)

The problem is that I think that a lot of queries over this view will be done and I suspect this query is quite slow, and I don't know if there is a way to do faster.

alter view questionLevel as select *, 0 as ordenacio from question where statsAnswerCorrect + statsAnswerIncorrect < 10
union
select *, 1 as ordenacio from question where 
statsAnswerCorrect + statsAnswerIncorrect >= 10
and statsAnswerIncorrect = 0
union
select *, 2 as ordenacio
from question where questionId IN (
select questionId from question where statsAnswerCorrect + statsAnswerIncorrect >= 10 
and  statsAnswerIncorrect > 0 )
order by ordenacio asc, ( statsAnswerCorrect / statsAnswerIncorrect) desc

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

平生欢 2024-11-16 06:26:55

您可以将 ordenacio=2 的联合的第三部分重写为一个简单的查询:

SELECT *, 2 AS ordenacio FROM question 
WHERE statsAnswerCorrect + statsAnswerIncorrect >= 10 
AND  statsAnswerIncorrect > 0

另请记住,在按除法排序时,您的除数可以等于 0,这意味着 mysql 将为它分配 NULL 值,并在以下情况下将其放在末尾:您按 ... DESC 排序,这可能不是您想要的。请改用 ORDER BY ( a/(b+1) ) DESCORDER BY ( a/(a+b) ) DESC

如果您想经常使用此视图,最好对数据库进行非规范化,并添加列来保留问题类型和正确百分比,并在插入时更新。这实际上取决于您的表应该有多大以及您期望的插入操作数量。

You can rewrite your third part of union for ordenacio=2 as a simple query:

SELECT *, 2 AS ordenacio FROM question 
WHERE statsAnswerCorrect + statsAnswerIncorrect >= 10 
AND  statsAnswerIncorrect > 0

Also keep in mind that while ordering by division your divisor can be equal 0, which means mysql will assign it NULL value and put rows with it at the end if you ORDER BY ... DESC, which is probably not what you want. Use ORDER BY ( a/(b+1) ) DESC or ORDER BY ( a/(a+b) ) DESC instead.

If you want to use this view a lot it might be better to denormalize your db a bit more and add columns keeping question type and correct percentage, updated on insert. It really depends on how big your tables are supposed to be and how many insert operations you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文