mysql - 按整数值分类和 COUNT() 的 sql 优化

标签 mysql ruby

我有这样的 table 。

table ppsscore
string name, int playlevel , int scoretotal, 
  • 游戏级别为 0-1
  • 总分取0-100

超过 100 万行。

我想按游戏级别和总得分对行进行分类,然后对行进行计数。

但是这需要太多时间。

首先,我对“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

最佳答案

在一个查询中完成:

select playlevel, concat('over',floor(scoretotal/10)*10) score, count(*) c
from ppsscore
group by playlevel, concat('over',floor(scoretotal/10)*10)

关于mysql - 按整数值分类和 COUNT() 的 sql 优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052363/

相关文章:

java - 最后一个数据包成功发送到服务器是在 > 70,400,003 毫秒之前。比服务器配置的长

ruby-on-rails - Order_by 多个字段,包括一个 bool 值

ruby - 如何使用 Faye Websockets 向特定客户端发送消息?

ruby : Array of single type elements

ruby-on-rails - 如何删除字符串中可能出现的子字符串?

mysql - SQL查询按表属性对分组进行计数

MySQL UNION 返回不存在的 id

Mysql查询查找手机号码

mysql - MySQL 是否优化相关表上的选定聚合以避免 N+1?

ruby - 将回形针与 Padrino 一起使用