先进的评级系统MySql数据库优化(Advanced rating system MySql dat

2019-10-17 05:21发布

我的工作与此条件的评级系统:

有一些用户和他们的信息都存储在用户表中,每个用户都有由游客(0〜10)额定一些图像,图像存储在图像表和利率表中的利率

现在,我想获得最受好评的某一天(平均利率)的图像,所以我写了这个查询:

SELECT
images.id AS iid,
images.id,
images.uid,
images.path,
images.sdate,
images.croped,
users.id,
users.provider,
users.twitterid,
users.username,
users.token,
users.secret,
users.`name`,
users.born,
users.height,
users.hair,
users.location,
users.eyes,
users.sdate,
COALESCE(x.cnt, 0) AS cnt,
COALESCE(x.arate, 0) AS arate,
x.lastdate
FROM images
LEFT OUTER JOIN (SELECT r.imageid,COUNT(r.id) as cnt,AVG(r.rate) AS arate,MAX(r.sdate) as lastdate  FROM rates r  GROUP BY r.imageid) x ON x.imageid = images.id
LEFT OUTER JOIN users on images.uid=users.id
ORDER BY
arate DESC, cnt DESC,x.lastdate DESC
LIMIT 10

但对执行并需要很长时间来执行一些问题! 我认为它的主要问题是在这里:

ORDER BY
arate DESC, cnt DESC,x.lastdate DESC

你能找到这个查询的问题?

你可以看到我的数据库的结构,在这里:

http://sqlfiddle.com/#!2/bc984/1

**编辑*

Expalin结果被添加:

1   PRIMARY images  ALL                 70195   Using temporary; Using filesort
1   PRIMARY <derived2>  ALL                 1743    
1   PRIMARY users   eq_ref  PRIMARY PRIMARY 4   twitter.images.uid  1   
2   DERIVED r   index       imageid 4       17776   
文章来源: Advanced rating system MySql database optimization