检查满足特定条件的连续天数的最大长度

时间:2012-04-04 23:09:53

标签: mysql sql

我有一个结构的MySQL表:

beverages_log(id,users_id,beverages_id,timestamp)

我正在尝试计算用户(ID为1)每天至少记录5次饮料(ID为1)的连续天数的最大连续数。我很确定这可以使用以下视图完成:

CREATE or REPLACE VIEW daycounts AS
SELECT count(*) AS n, DATE(timestamp) AS d FROM beverages_log
WHERE users_id = '1' AND beverages_id = 1 GROUP BY d;

CREATE or REPLACE VIEW t AS SELECT * FROM daycounts WHERE n >= 5;

SELECT MAX(streak) AS current FROM ( SELECT DATEDIFF(MIN(c.d), a.d)+1 AS streak
FROM t AS a LEFT JOIN t AS b ON a.d = ADDDATE(b.d,1)
LEFT JOIN t AS c ON a.d <= c.d
LEFT JOIN t AS d ON c.d = ADDDATE(d.d,-1)
WHERE b.d IS NULL AND c.d IS NOT NULL AND d.d IS NULL GROUP BY a.d) allstreaks;

但是,每次运行此检查时,为不同的用户重复创建视图似乎效率很低。 MySQL是否有办法在单个查询中执行此计算,而无需创建视图或反复多次调用相同的子查询?

3 个答案:

答案 0 :(得分:6)

只要在users_id和beverages_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 t
ORDER BY streak DESC LIMIT 1;

答案 1 :(得分:0)

为什么不将user_id包含在daycounts视图中,并按user_id和date分组。

还在视图中包含user_id。

然后,当你正在反对时,将user_id添加到where子句。

然后,您不必为您需要记住包含在where子句中的每个用户重新创建视图。

答案 2 :(得分:0)

这有点棘手。我开始的观点是白天总结事件:

CREATE VIEW BView AS
    SELECT UserID, BevID, CAST(EventDateTime AS DATE) AS EventDate, COUNT(*) AS NumEvents
    FROM beverages_log
    GROUP BY UserID, BevID, CAST(EventDateTime AS DATE)

然后我会使用一个Dates表(只是一个每天有一行的表;非常方便)来检查所有可能的日期范围并抛出任何有差距的表。这可能会很慢,但这是一个开始:

SELECT
    UserID, BevID, MAX(StreakLength) AS StreakLength
FROM
    (
    SELECT
        B1.UserID, B1.BevID, B1.EventDate AS StreakStart, DATEDIFF(DD, StartDate.Date, EndDate.Date) AS StreakLength
    FROM
        BView AS B1
        INNER JOIN Dates AS StartDate ON B1.EventDate = StartDate.Date
        INNER JOIN Dates AS EndDate ON EndDate.Date > StartDate.Date
    WHERE
            B1.NumEvents >= 5
        -- Exclude this potential streak if there's a day with no activity
        AND NOT EXISTS (SELECT * FROM Dates AS MissedDay WHERE MissedDay.Date > StartDate.Date AND MissedDay.Date <= EndDate.Date AND NOT EXISTS (SELECT * FROM BView AS B2 WHERE B1.UserID = B2.UserID AND B1.BevID = B2.BevID AND MissedDay.Date = B2.EventDate))
        -- Exclude this potential streak if there's a day with less than five events
        AND NOT EXISTS (SELECT * FROM BView AS B2 WHERE B1.UserID = B2.UserID AND B1.BevID = B2.BevID AND B2.EventDate > StartDate.Date AND B2.EventDate <= EndDate.Date AND B2.NumEvents < 5)
    ) AS X
GROUP BY
    UserID, BevID