我有这样的表格。
table ppsscore
string name, int playlevel , int scoretotal,
行数超过100万。
我想按playlevel和scoretotal对行进行分类,然后计算行数。
然而,这需要太多时间。
起初,我制作了索引“playlevel”和“scoretotal”,但没有那么多不同。
还有其他优化方法可以优化计数吗?
hash['level1']['over100'] = Ppscore.where("playlevel = '1' AND scoretotal = '100'").count
hash['level1']['over90'] = Ppscore.where("playlevel = '1' AND scoretotal >= '90' AND scoretotal != '100'").count
hash['level1']['over80'] = Ppscore.where("playlevel = '1' AND scoretotal >= '80' AND scoretotal < '90'").count
hash['level1']['over70'] = Ppscore.where("playlevel = '1' AND scoretotal >= '70' AND scoretotal < '80'").count
hash['level1']['over60'] = Ppscore.where("playlevel = '1' AND scoretotal >= '60' AND scoretotal < '70'").count
hash['level1']['over50'] = Ppscore.where("playlevel = '1' AND scoretotal >= '50' AND scoretotal < '60'").count
hash['level1']['over40'] = Ppscore.where("playlevel = '1' AND scoretotal >= '40' AND scoretotal < '50'").count
hash['level1']['over30'] = Ppscore.where("playlevel = '1' AND scoretotal >= '30' AND scoretotal < '40'").count
hash['level1']['over20'] = Ppscore.where("playlevel = '1' AND scoretotal >= '20' AND scoretotal < '30'").count
hash['level1']['over10'] = Ppscore.where("playlevel = '1' AND scoretotal >= '10' AND scoretotal < '20'").count
hash['level1']['over0'] = Ppscore.where("playlevel = '1' AND scoretotal >= '0' AND scoretotal < '10'").count
hash['level0']['over100'] = Ppscore.where("playlevel = '0' AND scoretotal = '100'").count
hash['level0']['over90'] = Ppscore.where("playlevel = '0' AND scoretotal >= '90' AND scoretotal != '100'").count
hash['level0']['over80'] = Ppscore.where("playlevel = '0' AND scoretotal >= '80' AND scoretotal < '90'").count
hash['level0']['over70'] = Ppscore.where("playlevel = '0' AND scoretotal >= '70' AND scoretotal < '80'").count
hash['level0']['over60'] = Ppscore.where("playlevel = '0' AND scoretotal >= '60' AND scoretotal < '70'").count
hash['level0']['over50'] = Ppscore.where("playlevel = '0' AND scoretotal >= '50' AND scoretotal < '60'").count
hash['level0']['over40'] = Ppscore.where("playlevel = '0' AND scoretotal >= '40' AND scoretotal < '50'").count
hash['level0']['over30'] = Ppscore.where("playlevel = '0' AND scoretotal >= '30' AND scoretotal < '40'").count
hash['level0']['over20'] = Ppscore.where("playlevel = '0' AND scoretotal >= '20' AND scoretotal < '30'").count
hash['level0']['over10'] = Ppscore.where("playlevel = '0' AND scoretotal >= '10' AND scoretotal < '20'").count
hash['level0']['over0'] = Ppscore.where("playlevel = '0' AND scoretotal >= '0' AND scoretotal < '10'").count
答案 0 :(得分:1)
在一个查询中执行此操作:
select playlevel, concat('over',floor(scoretotal/10)*10) score, count(*) c
from ppsscore
group by playlevel, concat('over',floor(scoretotal/10)*10)