I am trying to get an optimised query to find the top 5 most visited wikipedia pages in each month in 2019 from the public dataset fh-bigquery.wikipedia_v3.pageviews_2019. I have come up with the below query but I need two things:
The query runs for about 2 minutes and processes 2.3 TB irrespective of whether I filter top 5 ranks or display them all in the outer query. Is there a better optimised query to process less volume and in less time - something by which we can limit the number of records fetched in the first place itself (using top 5 or limit 5 in the subquery itself)?
Sort the month based on calendar order rather than alphabetical order.
Query:
select *
from (
select Month_2019, title, tot_views,
rank() over (partition by Month_2019 order by tot_views desc) as view_rank
from (
select format_date("%B", Date(datehour)) as Month_2019,
title, sum(views) as tot_views
from `fh-bigquery.wikipedia_v3.pageviews_2019`
where wiki='en'
and title not in ('Main_Page','-','Special:Search','Special:CreateAccount','Special:Watchlist','Special:ElectronPdf','Special:Book','Special:CiteThisPage','Special:RecentChanges','Portal:Current_events','Wikipedia')
and datehour between '2019-01-01' and '2019-12-31'
group by Month_2019, title
))
where view_rank<6
order by 1,4
Expected Output:
January Louis_Tomlinson 5075908 1
January Deaths_in_2019 1832404 2
January TCP_delayed_acknowledgment 1238559 3
January Ted_Bundy 1190672 4
January Glass_(2019_film) 1018119 5
February Louis_Tomlinson 5504517
February Grover 4970493
February Rheology 2852186
February Deaths_in_2019
February Operating_system
March.... ....



