Skip to content

Improve the performance of reverse dependencies using the default_versions #8737

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

Merged
merged 3 commits into from
May 29, 2024
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
60 changes: 28 additions & 32 deletions src/models/krate_reverse_dependencies.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
WITH filtered_default_versions as (
-- Get all `default_versions` that are depending on the crate $1
SELECT default_versions.*
FROM default_versions
WHERE version_id IN (
SELECT dependencies.version_id
FROM dependencies
WHERE dependencies.crate_id = $1
) AND NOT EXISTS (
-- Filter out yanked crates
-- (if the default version is yanked, then the whole crate is yanked)
SELECT 1
FROM versions
WHERE id = version_id and yanked
)
)
SELECT
dependencies.*, crate_downloads, crate_name, total
FROM (
-- Apply pagination to the crates
SELECT *, COUNT(*) OVER () as total FROM (
SELECT
crate_downloads.downloads AS crate_downloads,
crates.name AS crate_name,
versions.id AS version_id
FROM
-- We only want the crates whose *max* version is dependent, so we join on a
-- subselect that includes the versions with their ordinal position
(
SELECT DISTINCT ON (crate_id)
crate_id, semver_no_prerelease, id
FROM versions
WHERE NOT yanked
ORDER BY
crate_id,
semver_no_prerelease DESC NULLS LAST,
id DESC
) versions
INNER JOIN crates
ON crates.id = versions.crate_id
INNER JOIN crate_downloads
ON crate_downloads.crate_id = crates.id
WHERE versions.id IN (SELECT version_id FROM dependencies WHERE crate_id = $1)
) c
ORDER BY
crate_downloads DESC,
crate_name ASC
) crates
dependencies.*,
crate_downloads.downloads as crate_downloads,
crates.name as crate_name,
(SELECT COUNT(*) from filtered_default_versions) as total
FROM filtered_default_versions
INNER JOIN crates
ON crates.id = filtered_default_versions.crate_id
INNER JOIN crate_downloads using (crate_id)
-- Multiple dependencies can exist, we only want first one
CROSS JOIN LATERAL (
SELECT dependencies.*
FROM dependencies
WHERE dependencies.crate_id = $1 AND dependencies.version_id = crates.version_id
WHERE dependencies.crate_id = $1 AND dependencies.version_id = filtered_default_versions.version_id
ORDER BY id ASC
LIMIT 1
) dependencies
ORDER BY
crate_downloads DESC,
crate_name ASC
OFFSET $2
LIMIT $3
3 changes: 3 additions & 0 deletions src/tests/builders/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crates_io::{
};

use chrono::NaiveDateTime;
use crates_io::models::update_default_version;
use crates_io::schema::crate_downloads;
use diesel::prelude::*;

Expand Down Expand Up @@ -169,6 +170,8 @@ impl<'a> CrateBuilder<'a> {
.get_result(connection)?;
}

update_default_version(krate.id, connection)?;

Ok(krate)
}

Expand Down