第一步是计算avg_num_votes
和avg_rating
:
SELECT
SUM(totalVotes)/COUNT(*) AS avg_num_votes,
SUM(avgVote)/COUNT(*) AS avg_rating
FROM voting;
如果您可以忍受一个小错误,那么偶尔计算一次可能就足够了。
现在使用您的公式和上面的值,您可以运行称重查询。作为一个小的优化,我预先计算avg_num_votes * avg_rating
并调用它avg_summand
SELECT
voting.*, -- or whatever fields you need
($avg_summand+totalVotes*avgVote)/($avg_num_votes+totalVotes) AS bayesian
FROM voting
ORDER BY bayesian DESC
LIMIT 1;
编辑
您可以将其作为连接运行:
SELECT
voting.*, -- or whatever fields you need
(avg_num_votes*avg_rating+totalVotes*avgVote)/(avg_num_votes+totalVotes) AS bayesian
FROM voting,
(
SELECT
SUM(totalVotes)/COUNT(*) AS avg_num_votes,
SUM(avgVote)/COUNT(*) AS avg_rating
FROM voting AS iv
) AS avg
ORDER BY bayesian DESC
LIMIT 1;
但这将计算每个查询的总和和平均值——称之为性能炸弹。