Classifying counts of repeat visits per user for a website
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am trying to get number of times (or sessions) a user re-visited the website.
What I'm trying to find out-
Get the total number of users, number of sessions in the last 30days. I need to a pie chart ultimately where it shows people who revisited-
- more than 100 times,
- 50-100 times,
- less than 50 times
The problem with the current implementation I see is -
The whole set of data(like 100million rows) are being computed every time(for each query with different condition)
SELECT COUNT(A.sess) as acount, SUM(A.sess) as asum, AVG(A.sess) as aavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) >= 100) AS A
UNION ALL
SELECT COUNT(B.sess) as bcount, SUM(B.sess) as bsum, AVG(B.sess) as bavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 100 AND COUNT(*) >=50 ) AS B
UNION ALL
SELECT COUNT(C.sess) as ccount, SUM(C.sess) as csum, AVG(C.sess) as cavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 50 ) AS C
How do I optimize the query?
performance sql mysql database statistics
add a comment |Â
up vote
1
down vote
favorite
I am trying to get number of times (or sessions) a user re-visited the website.
What I'm trying to find out-
Get the total number of users, number of sessions in the last 30days. I need to a pie chart ultimately where it shows people who revisited-
- more than 100 times,
- 50-100 times,
- less than 50 times
The problem with the current implementation I see is -
The whole set of data(like 100million rows) are being computed every time(for each query with different condition)
SELECT COUNT(A.sess) as acount, SUM(A.sess) as asum, AVG(A.sess) as aavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) >= 100) AS A
UNION ALL
SELECT COUNT(B.sess) as bcount, SUM(B.sess) as bsum, AVG(B.sess) as bavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 100 AND COUNT(*) >=50 ) AS B
UNION ALL
SELECT COUNT(C.sess) as ccount, SUM(C.sess) as csum, AVG(C.sess) as cavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 50 ) AS C
How do I optimize the query?
performance sql mysql database statistics
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to get number of times (or sessions) a user re-visited the website.
What I'm trying to find out-
Get the total number of users, number of sessions in the last 30days. I need to a pie chart ultimately where it shows people who revisited-
- more than 100 times,
- 50-100 times,
- less than 50 times
The problem with the current implementation I see is -
The whole set of data(like 100million rows) are being computed every time(for each query with different condition)
SELECT COUNT(A.sess) as acount, SUM(A.sess) as asum, AVG(A.sess) as aavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) >= 100) AS A
UNION ALL
SELECT COUNT(B.sess) as bcount, SUM(B.sess) as bsum, AVG(B.sess) as bavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 100 AND COUNT(*) >=50 ) AS B
UNION ALL
SELECT COUNT(C.sess) as ccount, SUM(C.sess) as csum, AVG(C.sess) as cavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 50 ) AS C
How do I optimize the query?
performance sql mysql database statistics
I am trying to get number of times (or sessions) a user re-visited the website.
What I'm trying to find out-
Get the total number of users, number of sessions in the last 30days. I need to a pie chart ultimately where it shows people who revisited-
- more than 100 times,
- 50-100 times,
- less than 50 times
The problem with the current implementation I see is -
The whole set of data(like 100million rows) are being computed every time(for each query with different condition)
SELECT COUNT(A.sess) as acount, SUM(A.sess) as asum, AVG(A.sess) as aavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) >= 100) AS A
UNION ALL
SELECT COUNT(B.sess) as bcount, SUM(B.sess) as bsum, AVG(B.sess) as bavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 100 AND COUNT(*) >=50 ) AS B
UNION ALL
SELECT COUNT(C.sess) as ccount, SUM(C.sess) as csum, AVG(C.sess) as cavg
FROM
(SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
HAVING COUNT(*) < 50 ) AS C
How do I optimize the query?
performance sql mysql database statistics
edited Jan 17 at 12:51
200_success
123k14143401
123k14143401
asked Jan 17 at 11:11
bozzmob
1064
1064
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
MySQL version 8 supports common table expression. If you want to differentiate between the three sub queries, use a column containing literal:
WITH SESSIONS_THIS_MONTH AS (
SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
)
SELECT 'MORE THAN 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as acount,
SUM(SESSIONS_THIS_MONTH.sess) as asum,
AVG(SESSIONS_THIS_MONTH.sess) as aavg
FROM SESSIONS_THIS_MONTH
WHERE sess >= 100
UNION ALL
SELECT 'BETWEEN 50 AND 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as bcount,
SUM(SESSIONS_THIS_MONTH.sess) as bsum,
AVG(SESSIONS_THIS_MONTH.sess) as bavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 100 AND sess >=50
UNION ALL
SELECT 'LESS THAN 50' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as ccount,
SUM(SESSIONS_THIS_MONTH.sess) as csum,
AVG(SESSIONS_THIS_MONTH.sess) as cavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 50
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
MySQL version 8 supports common table expression. If you want to differentiate between the three sub queries, use a column containing literal:
WITH SESSIONS_THIS_MONTH AS (
SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
)
SELECT 'MORE THAN 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as acount,
SUM(SESSIONS_THIS_MONTH.sess) as asum,
AVG(SESSIONS_THIS_MONTH.sess) as aavg
FROM SESSIONS_THIS_MONTH
WHERE sess >= 100
UNION ALL
SELECT 'BETWEEN 50 AND 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as bcount,
SUM(SESSIONS_THIS_MONTH.sess) as bsum,
AVG(SESSIONS_THIS_MONTH.sess) as bavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 100 AND sess >=50
UNION ALL
SELECT 'LESS THAN 50' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as ccount,
SUM(SESSIONS_THIS_MONTH.sess) as csum,
AVG(SESSIONS_THIS_MONTH.sess) as cavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 50
add a comment |Â
up vote
1
down vote
MySQL version 8 supports common table expression. If you want to differentiate between the three sub queries, use a column containing literal:
WITH SESSIONS_THIS_MONTH AS (
SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
)
SELECT 'MORE THAN 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as acount,
SUM(SESSIONS_THIS_MONTH.sess) as asum,
AVG(SESSIONS_THIS_MONTH.sess) as aavg
FROM SESSIONS_THIS_MONTH
WHERE sess >= 100
UNION ALL
SELECT 'BETWEEN 50 AND 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as bcount,
SUM(SESSIONS_THIS_MONTH.sess) as bsum,
AVG(SESSIONS_THIS_MONTH.sess) as bavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 100 AND sess >=50
UNION ALL
SELECT 'LESS THAN 50' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as ccount,
SUM(SESSIONS_THIS_MONTH.sess) as csum,
AVG(SESSIONS_THIS_MONTH.sess) as cavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 50
add a comment |Â
up vote
1
down vote
up vote
1
down vote
MySQL version 8 supports common table expression. If you want to differentiate between the three sub queries, use a column containing literal:
WITH SESSIONS_THIS_MONTH AS (
SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
)
SELECT 'MORE THAN 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as acount,
SUM(SESSIONS_THIS_MONTH.sess) as asum,
AVG(SESSIONS_THIS_MONTH.sess) as aavg
FROM SESSIONS_THIS_MONTH
WHERE sess >= 100
UNION ALL
SELECT 'BETWEEN 50 AND 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as bcount,
SUM(SESSIONS_THIS_MONTH.sess) as bsum,
AVG(SESSIONS_THIS_MONTH.sess) as bavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 100 AND sess >=50
UNION ALL
SELECT 'LESS THAN 50' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as ccount,
SUM(SESSIONS_THIS_MONTH.sess) as csum,
AVG(SESSIONS_THIS_MONTH.sess) as cavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 50
MySQL version 8 supports common table expression. If you want to differentiate between the three sub queries, use a column containing literal:
WITH SESSIONS_THIS_MONTH AS (
SELECT ipaddress,
COUNT(*) AS sess
FROM analytics
WHERE trunc(datecreated) > getdate() - interval '30 days'
GROUP BY trunc(datecreated), ipaddress
)
SELECT 'MORE THAN 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as acount,
SUM(SESSIONS_THIS_MONTH.sess) as asum,
AVG(SESSIONS_THIS_MONTH.sess) as aavg
FROM SESSIONS_THIS_MONTH
WHERE sess >= 100
UNION ALL
SELECT 'BETWEEN 50 AND 100' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as bcount,
SUM(SESSIONS_THIS_MONTH.sess) as bsum,
AVG(SESSIONS_THIS_MONTH.sess) as bavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 100 AND sess >=50
UNION ALL
SELECT 'LESS THAN 50' AS CATEGORY,
COUNT(SESSIONS_THIS_MONTH.sess) as ccount,
SUM(SESSIONS_THIS_MONTH.sess) as csum,
AVG(SESSIONS_THIS_MONTH.sess) as cavg
FROM SESSIONS_THIS_MONTH
WHERE sess < 50
edited Jan 17 at 13:38
answered Jan 17 at 13:32
Sharon Ben Asher
2,073512
2,073512
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185296%2fclassifying-counts-of-repeat-visits-per-user-for-a-website%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password