In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
SELECT
agg.event_date,
agg.country,
COUNT(*) AS sessions,
AVG(length) AS average_session_length
FROM
(SELECT
session.country,
session.event_date,
global_session_id,
(MAX(session.event_timestamp) - MIN(session.event_timestamp)) / (60 * 1000 * 1000) AS length
FROM
(SELECT
user_pseudo_id,
event_timestamp,
country,
event_date,
SUM(is_new_session) OVER (ORDER
BY
user_pseudo_id,
event_timestamp) AS global_session_id,
SUM(is_new_session) OVER (PARTITION
BY
user_pseudo_id
ORDER BY
event_timestamp) AS user_session_id
FROM
(SELECT
*,
CASE
WHEN last.event_timestamp - last_event >= (30 * 60 * 1000 * 1000)
OR last_event IS NULL THEN 1
ELSE 0 END AS is_new_session
FROM
(SELECT
`xxx.events*`.user_pseudo_id,
`xxx.events*`.event_timestamp,
geo.country,
`xxx.events*`.event_date,
LAG(`xxx.events*`.event_timestamp,
1) OVER (PARTITION
BY
user_pseudo_id
ORDER BY
`xxx.events*`.event_timestamp) AS last_event
FROM
`xxx.events*`) last) final) session
GROUP BY
global_session_id,
session.country,
session.event_date
ORDER BY
NULL) agg
WHERE
length >= (
10 / 60
)
GROUP BY
agg.country,
agg.event_date
ORDER BY
NULL