我们常见给产品进行5星评价的系统,我初步的构想如下,
SELECT avg(vote) FROM votes WHERE gid = $gid; SELECT count(uid) FROM votes WHERE gid = $gid; CREATE TABLE IF NOT EXISTS `games` ( `id` int(8) NOT NULL auto_increment, `title` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `users` ( `id` int(8) NOT NULL auto_increment, `username` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `votes` ( `uid` int(8) unsigned NOT NULL, `gid` int(8) unsigned NOT NULL, `vote` int(1) NOT NULL, PRIMARY KEY (`gid`, `uid`) ) ;
如何修改更高效?
例如,
当数据上万时,把平均值存储下来,而不是每次都进行计算?
在games表中进行适当的冗余,增加一个评论加1?
我现在有点拿不定主意。
CREATE TABLE IF NOT EXISTS `games` ( `id` int(8) NOT NULL auto_increment, `title` varchar(50) NOT NULL, `vote_1` int(8) unsigned NOT NULL, `vote_2` int(8) unsigned NOT NULL, `vote_3` int(8) unsigned NOT NULL, `vote_4` int(8) unsigned NOT NULL, `vote_5` int(8) unsigned NOT NULL, `avgrate` int(8) unsigned NOT NULL, PRIMARY KEY (`id`) ) AUTO_INCREMENT=1 ;