【问题标题】:Optimize query of questions ordered by difficulty优化按难度排序的问题查询
【发布时间】:2011-05-20 16:08:11
【问题描述】:

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

我的想法是:

  • 先提出新问题(少于 10 个回答)
  • 第二个回答没有错误的问题
  • 最后是按难度排序的其他问题

计算难度的方式是(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

有什么想法吗?

【问题讨论】:

    标签: mysql view query-optimization


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      相关资源
      最近更新 更多