我实际上试图改进这些SQL查询。它们各持续5秒钟,因为表“admin_event_visits”的行数超过1.5十亿。
有没有办法优化这些查询?
1
SELECT COUNT(distinct ip) AS count_distinct_ip, (DATE_TRUNC('day', (created_at::timestamptz - INTERVAL '0 second')
AT TIME ZONE 'America/Chicago')+ INTERVAL '0 second') AT TIME ZONE 'America/Chicago' AS day FROM "admin_event_visits"
WHERE (created_at IS NOT NULL) GROUP BY (DATE_TRUNC('day', (created_at::timestamptz - INTERVAL '0 second')
AT TIME ZONE 'America/Chicago') + INTERVAL '0 second') AT TIME ZONE 'America/Chicago'
2
SELECT COUNT(*) AS count_all, (DATE_TRUNC('day', (created_at::timestamptz - INTERVAL '0 second')
AT TIME ZONE 'America/Chicago') + INTERVAL '0 second') AT TIME ZONE 'America/Chicago' AS day FROM "admin_event_visits"
WHERE (created_at IS NOT NULL) GROUP BY (DATE_TRUNC('day', (created_at::timestamptz - INTERVAL '0 second')
AT TIME ZONE 'America/Chicago') + INTERVAL '0 second') AT TIME ZONE 'America/Chicago'
3
SELECT COUNT(distinct ip) AS count_distinct_ip, (DATE_TRUNC('day', (created_at::timestamptz - INTERVAL '0 second')
AT TIME ZONE 'America/Chicago') + INTERVAL '0 second') AT TIME ZONE 'America/Chicago' AS day
FROM "admin_event_visits"
WHERE device = 'mobile' AND (created_at IS NOT NULL)
GROUP BY (DATE_TRUNC('day', (created_at::timestamptz - INTERVAL '0 second')
AT TIME ZONE 'America/Chicago') + INTERVAL '0 second') AT TIME ZONE 'America/Chicago'