Skip to content

Commit 86cd1d8

Browse files
committed
Switch most recently downloaded summary query to be faster
Because we only want to show the top 10 for /api/v1/summary, move the LIMIT within the query for the top downloads rather than joining on everything and then limiting.
1 parent a2eb1f9 commit 86cd1d8

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/controllers/krate/metadata.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use models::krate::ALL_COLUMNS;
1616

1717
/// Handles the `GET /summary` route.
1818
pub fn summary(req: &mut Request) -> CargoResult<Response> {
19-
use diesel::dsl::*;
20-
use diesel::sql_types::{BigInt, Nullable};
19+
use diesel::sql_query;
2120
use schema::crates::dsl::*;
2221

2322
let conn = req.db_conn()?;
@@ -57,17 +56,23 @@ pub fn summary(req: &mut Request) -> CargoResult<Response> {
5756
.limit(10)
5857
.load(&*conn)?;
5958

60-
let recent_downloads = sql::<Nullable<BigInt>>("SUM(crate_downloads.downloads)");
61-
let most_recently_downloaded = crates
62-
.left_join(
63-
crate_downloads::table.on(id.eq(crate_downloads::crate_id)
64-
.and(crate_downloads::date.gt(date(now - 90.days())))),
65-
)
66-
.group_by(id)
67-
.order(recent_downloads.desc().nulls_last())
68-
.limit(10)
69-
.select(ALL_COLUMNS)
70-
.load::<Crate>(&*conn)?;
59+
// This query needs to be structured in this way to have the LIMIT
60+
// happen before the joining/sorting for performance reasons.
61+
// It needs to use sql_query because Diesel doesn't have a great way
62+
// to join on subselects right now :(
63+
let most_recently_downloaded = sql_query(
64+
"SELECT crates.* \
65+
FROM crates \
66+
JOIN ( \
67+
SELECT crate_downloads.crate_id, SUM(crate_downloads.downloads) \
68+
FROM crate_downloads \
69+
WHERE crate_downloads.date > date(CURRENT_TIMESTAMP - INTERVAL '90 days') \
70+
GROUP BY crate_downloads.crate_id \
71+
ORDER BY SUM(crate_downloads.downloads) DESC NULLS LAST \
72+
LIMIT 10 \
73+
) cd ON crates.id = cd.crate_id \
74+
ORDER BY cd.sum DESC NULLS LAST",
75+
).load::<Crate>(&*conn)?;
7176

7277
let popular_keywords = keywords::table
7378
.order(keywords::crates_cnt.desc())

src/models/krate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pub struct CrateDownload {
3030
pub date: NaiveDate,
3131
}
3232

33-
#[derive(Debug, Clone, Queryable, Identifiable, Associations, AsChangeset)]
33+
#[derive(Debug, Clone, Queryable, Identifiable, Associations, AsChangeset, QueryableByName)]
34+
#[table_name = "crates"]
3435
pub struct Crate {
3536
pub id: i32,
3637
pub name: String,

0 commit comments

Comments
 (0)