Skip to content

Commit bc30536

Browse files
committed
fix abnormally slow query that was failing to use partial index
1 parent 2763d81 commit bc30536

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/UnidocQueries/Activity/Unidoc.PackagesCrawledQuery.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ extension Unidoc.PackagesCrawledQuery:Mongo.PipelineQuery
5959
$0[.foreignField] = Unidoc.PackageMetadata[.repo] / Unidoc.PackageRepo[.created]
6060
$0[.pipeline] = .init
6161
{
62+
/// This improves query performance enormously, as it gets MongoDB to use the
63+
/// partial index. But why?? The `localField` is always non-null!
64+
$0[.match] = .init
65+
{
66+
$0[Unidoc.PackageMetadata[.repo]] = .init { $0[.exists] = true }
67+
}
68+
6269
$0[.count] = count
6370
}
6471
$0[.as] = Date[.repos]

Sources/UnidocQueries/Packages/Unidoc.RealmQuery.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ extension Unidoc.RealmQuery:Unidoc.AliasingQuery
6363
}
6464
}
6565

66-
// This *should* be able to use the partial index even without `$exists` guards,
67-
// as `_id` is always present.
66+
// It’s not clear to me how this is able to use the partial index even without
67+
// `$exists` guards, but somehow it does.
6868
pipeline[.lookup] = .init
6969
{
7070
$0[.from] = UnidocDatabase.Packages.name

0 commit comments

Comments
 (0)