我正在创建一个包含难以排序的问题的视图。
我的想法是:
计算难度的方法是(answerCorrect / answerIncorrect)
问题是我认为这个视图上的很多查询都会完成,我怀疑这个查询很慢,我不知道是否有办法更快地完成。
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
有什么想法吗?
答案 0 :(得分:0)
您可以将ordenacio = 2的第三部分联合重写为简单查询:
SELECT *, 2 AS ordenacio FROM question
WHERE statsAnswerCorrect + statsAnswerIncorrect >= 10
AND statsAnswerIncorrect > 0
另外请记住,虽然按除法排序,你的除数可以等于0,这意味着如果你ORDER BY ... DESC,mysql会给它分配NULL值并在最后放行,这可能不是你的想。请改用ORDER BY ( a/(b+1) ) DESC
或ORDER BY ( a/(a+b) ) DESC
。
如果你想大量使用这个视图,最好再对你的db进行非规范化,并添加保持问题类型和正确百分比的列,并在插入时更新。这实际上取决于你的表有多大以及你期望的插入操作数量。