Skip to content

Create a smaller index to be used on the summary page #1328

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP FUNCTION refresh_todays_recent_crate_downloads();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE FUNCTION refresh_todays_recent_crate_downloads() RETURNS void AS $$
BEGIN
DROP INDEX IF EXISTS todays_recent_crate_downloads;
EXECUTE 'CREATE INDEX todays_recent_crate_downloads
ON crate_downloads (date)
WHERE date > date(''' || CURRENT_DATE::text || '''::date - INTERVAL ''90 days'')';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the WHERE clause in the query need to match to ensure the index is used? Especially regarding CURRENT_TIMESTAMP vs CURRENT_DATE. The query is currently:

WHERE crate_downloads.date > date(CURRENT_TIMESTAMP - INTERVAL '90 days')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that the query as it is written today will use this index before opening the PR

ANALYZE crate_downloads;
END;
$$ LANGUAGE plpgsql;
23 changes: 23 additions & 0 deletions src/bin/update-daily-indices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Creates any partial indexes that need to be updated daily
//
// Should be run at just after midnight UTC on a daily basis

#![deny(warnings)]

extern crate cargo_registry;
#[macro_use]
extern crate diesel;

use diesel::prelude::*;
use diesel::select;

fn main() {
let conn = cargo_registry::db::connect_now().unwrap();
make_indices(&conn).unwrap();
}

no_arg_sql_function!(refresh_todays_recent_crate_downloads, ());
fn make_indices(conn: &PgConnection) -> QueryResult<()> {
select(refresh_todays_recent_crate_downloads).execute(conn)?;
Ok(())
}