优化按难度排序题的查询
我正在创建一个视图,其中包含必须按难度排序的问题。
我的想法是:
- 首先放入新的问题(少于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 ordenacio=2 的联合的第三部分重写为一个简单的查询:
另请记住,在按除法排序时,您的除数可以等于 0,这意味着 mysql 将为它分配 NULL 值,并在以下情况下将其放在末尾:您按 ... DESC 排序,这可能不是您想要的。请改用
ORDER BY ( a/(b+1) ) DESC
或ORDER BY ( a/(a+b) ) DESC
。如果您想经常使用此视图,最好对数据库进行非规范化,并添加列来保留问题类型和正确百分比,并在插入时更新。这实际上取决于您的表应该有多大以及您期望的插入操作数量。
You can rewrite your third part of union for ordenacio=2 as a simple query:
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
orORDER 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.