Make software filter by counts faster (#3975)

* Make software filter by counts faster

* Sort only when it's for the agg table
This commit is contained in:
Tomas Touceda 2022-02-02 09:51:56 -03:00 committed by GitHub
parent a5b2e60075
commit a63c549f07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -314,12 +314,27 @@ func selectSoftwareSQL(hostID *uint, opts fleet.SoftwareListOptions) (string, []
}
if opts.WithHostCounts {
subSelectCounts := dialect.From(goqu.I("aggregated_stats").As("shc")).Select(
"shc.id", "shc.json_value", "shc.updated_at",
).Where(goqu.I("shc.type").Eq("software_hosts_count"), goqu.I("shc.json_value").Gt(0)).
SelectAppend(
goqu.I("shc.json_value").As("hosts_count"),
goqu.I("shc.updated_at").As("counts_updated_at"),
)
subSelectListOpts := opts.ListOptions
switch subSelectListOpts.OrderKey {
case "hosts_counts", "counts_updated_at":
// all good, known columns, so we sort
subSelectCounts = appendListOptionsToSelect(subSelectCounts, opts.ListOptions)
default:
// we don't sort if it's not a column from this table
}
ds = ds.Join(
goqu.I("aggregated_stats").As("shc"),
subSelectCounts.As("shc"),
goqu.On(
goqu.I("s.id").Eq(goqu.I("shc.id")),
),
).Where(goqu.I("shc.type").Eq("software_hosts_count"), goqu.I("shc.json_value").Gt(0)).
).
SelectAppend(
goqu.I("shc.json_value").As("hosts_count"),
goqu.I("shc.updated_at").As("counts_updated_at"),