Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve get stats db function performance #3558

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions database/migrations/functions/stats/get_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ returns setof json as $$
get_package_summary(jsonb_build_object('package_id', package_id)) as package,
sum(total) as total
from package_views
where date_trunc('year', day) = date_trunc('year', current_date)
and date_trunc('month', day) = date_trunc('month', current_date)
where date_trunc('year', day::timestamp) = date_trunc('year', current_timestamp)
and date_trunc('month', day::timestamp) = date_trunc('month', current_timestamp)
and package_id is not null
group by package_id
order by total desc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create index if not exists package_views_year_month_idx on package_views (date_trunc('year', day::timestamp), date_trunc('month', day::timestamp));

---- create above / drop below ----

drop index if exists package_views_year_month_idx;
3 changes: 2 additions & 1 deletion database/tests/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ select indexes_are('package_category', array[
'package_category_pkey'
]);
select indexes_are('package_views', array[
'package_views_package_id_version_day_key'
'package_views_package_id_version_day_key',
'package_views_year_month_idx'
]);
select indexes_are('package__maintainer', array[
'package__maintainer_pkey'
Expand Down
Loading