Sample Header Ad - 728x90

Understanding SQL aggregation efficiency

0 votes
2 answers
177 views
How would the first query compete in terms of performance against the second alternative: Query 1: select count(*) page_views, count(distinct(session_id)) sessions, (count(*) / count(distinct(session_id))) pages_per_session from page_views_table Query 2: select page_views, sessions, (page_views / sessions) pages_per_session from ( select count(*) page_views, count(distinct(session_id)) sessions from page_views_table ) I basically want to know if by using the first query, the database would need to calculate page_views (count(*)) and sessions (count(distinct(session_id))) twice in order to get the pages_per_session field, because if that's the case then the second query should be faster. So is the second query in fact the better choice?
Asked by user1432193 (163 rep)
Nov 16, 2021, 02:07 PM
Last activity: Jul 22, 2025, 08:08 AM