你能帮我让这个查询工作吗?
我有一个日志查询,它按天(表:,包括没有行的天数log
)为每个活动用户(表:user
,状态:(用于活动))计算记录的项目(表:)。1
calendar
以下查询需要 10 分钟才能运行!我怎样才能在几秒钟而不是几分钟内运行它?
SELECT
c.day, COUNT(u.id) AS count
FROM calendar c
LEFT JOIN log l
ON c.day = DATE_FORMAT(l.db_timestamp , '%Y-%m-%d')
LEFT JOIN user u
ON l.user_id = u.id
AND u.user_status_type_id = 1
WHERE
c.day > '2012-12-01'
AND c.day < '2013-01-01'
GROUP BY
c.day
表结构:
calendar (~3,000 rows)
day
===============================
2012-01-01
2012-01-02
2012-01-03
...
2020-01-01
log (~30,000 rows)
id user_id db_timestamp
================================
1 1 2012-01-01 01:01:01
1 2 2012-01-01 01:01:01
1 1 2012-01-01 01:01:01
user (~3,000,000 rows)
id user_status_type_id
================================
1 1
1 0
结果应如下所示:
Sample Expected Results
day count
=================
2012-12-01 1
2012-12-02 0
2012-12-03 4
...
2012-12-31 0
不幸的是,它需要永远运行。接下来我该怎么办?