我有一个带有多个子查询的查询,以计算涉及某笔交易的用户数量取决于状态。
SELECT * FROM (
SELECT
DATE(changes.created_at) AS `date`,
SUM(IF(changes.status = 15, 1, 0)) AS installed_daily,
SUM(IF(changes.status = 3, 1, 0)) AS port_reserved,
(
SELECT COUNT(DISTINCT created_by) FROM applicant_state_changes
WHERE DATE(created_at) = DATE(changes.created_at) AND status = 3
) AS user_port_reserved,
SUM(IF(changes.status = 5, 1, 0)) AS document_validated,
(
SELECT COUNT(DISTINCT created_by) FROM applicant_state_changes
WHERE DATE(created_at) = DATE(changes.created_at) AND status = 5
) AS user_document_validated,
SUM(IF(changes.status = 7, 1, 0)) AS account_created,
(
SELECT COUNT(DISTINCT created_by) FROM applicant_state_changes
WHERE DATE(created_at) = DATE(changes.created_at) AND status = 7
) AS user_account_created,
SUM(IF(changes.status = 11, 1, 0)) AS jo_created,
(
SELECT COUNT(DISTINCT created_by) FROM applicant_state_changes
WHERE DATE(created_at) = DATE(changes.created_at) AND status = 11
) AS user_jo_created
FROM applicant_state_changes AS changes
GROUP BY DATE(changes.created_at)
LIMIT 100 OFFSET 0
) a
ORDER BY date ASC;
这大约需要130秒。没有子查询,我的查询最多只需要0.5秒。
答案 0 :(得分:4)
您可以使用表达式时的大小写条件条件聚合
SELECT
DATE(changes.created_at) AS `date`,
SUM(IF(changes.status = 15, 1, 0)) AS installed_daily,
SUM(IF(changes.status = 3, 1, 0)) AS port_reserved,
count(distinct case when status = 3 then created_by end) as user_port_reserved
,
SUM(IF(changes.status = 5, 1, 0)) AS document_validated,
count(distinct case when status = 5 then created_by end) AS user_document_validated,
SUM(IF(changes.status = 7, 1, 0)) AS account_created,
count(distinct case when status = 7 then 1 end) AS user_account_created,
SUM(IF(changes.status = 11, 1, 0)) AS jo_created,
count(distinct case when status = 11 then created_by end) AS user_jo_created
FROM applicant_state_changes AS changes where
GROUP BY DATE(changes.created_at)
LIMIT 100 OFFSET 0