只要在users_id和brinkles_id上有一个复合索引,此解决方案的效果似乎就很好-
SELECt *FROM ( SELECt t.*, IF(@prev + INTERVAL 1 DAY = t.d, @c := @c + 1, @c := 1) AS streak, @prev := t.d FROM ( SELECt DATE(timestamp) AS d, COUNT(*) AS n FROM beverages_log WHERe users_id = 1 AND beverages_id = 1 GROUP BY DATE(timestamp) HAVINg COUNT(*) >= 5 ) AS t INNER JOIN (SELECT @prev := NULL, @c := 1) AS vars) AS tORDER BY streak DESC LIMIT 1;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)